Spelix is a webapplication using PHP on the server, HTML5 and JS on the client. PHP is running as a FastCGI process serving requests via Nginx. This presentations describes some of the key-takeaways that I've learned with that project, the essentials of running Nginx with PHP, starting from the basics, but also covering techniques like Memcached or leveraging the FastCGI cache. It contains several examples and performance comparison charts.
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
Nginx, PHP, Apache and Spelix
1. Nginx,
PHP,
Apache
and
Spelix
Harald
Zeitlhofer
October
2014
COMPANY CONFIDENTIAL 1 – DO NOT DISTRIBUTE #Dynatrace
2. Harald
Zeitlhofer
• Technology
Strategist
at
Dynatrace
• PHP
for
more
than
15
years
• Married,
2
kids
• Love
to
discover
new
things
COMPANY CONFIDENTIAL 2 – DO NOT DISTRIBUTE #Dynatrace
6. Agenda
• MoNvaNon
• Nginx
• PHP-‐FPM
• IntegraNon
• Caching
• Load
balancing
• How
this
runs
in
Spelix
COMPANY CONFIDENTIAL 6 – DO NOT DISTRIBUTE #Dynatrace
11. Nginx
SeTngs
• /etc/nginx/nginx.conf
# max_clients = worker_processes * worker_connections
worker_processes [number of CPUs];
worker_connections 1024;
COMPANY CONFIDENTIAL 11 – DO NOT DISTRIBUTE #Dynatrace
12. Nginx
High
Load
• /etc/sysctl.conf
fs.file-max = 80000
• /etc/security/limits.conf
•
www-data soft nofile 40000
www-data hard nofile 70000
/etc/pam.d/su
session required pam_limits.so
• /etc/default/nginx
ULIMIT="-n 40000"
COMPANY CONFIDENTIAL 12 – DO NOT DISTRIBUTE #Dynatrace
13. Nginx
High
Load
worker_processes 8;
worker_rlimit_nofile 40000;
events {
worker_connections 4096;
multi_accept on;
}
• 32768
concurrent
requests
COMPANY CONFIDENTIAL 13 – DO NOT DISTRIBUTE #Dynatrace
14. PHP
FastCGI
Process
Manager
COMPANY CONFIDENTIAL 14 – DO NOT DISTRIBUTE #Dynatrace
15. PHP-‐FPM
• FastCGI
Process
Manager
• Available
since
5.3.3,
stable
since
5.4.1
• Run
mulNple
PHP
worker
processes
to
serve
CGI
requests
• MulNple
connecNon
pools
• Different
php.ini
files
possible
• Very
helpful:
fastcgi_finish_request()
COMPANY CONFIDENTIAL 15 – DO NOT DISTRIBUTE #Dynatrace
16. PHP-‐FPM
• InstallaNon
sudo apt-get install php5-fpm
• Pool
configuraNon
/etc/php5/fpm/pool.d/www.conf
[www]
user = www-data
group = www-data
listen = 127.0.0.1:9000 # for Unix socket: unix:/var/run/php5-fpm.sock;
root@hzvm01:/etc/nginx/sites-enabled# ps -ef | grep php
root 6435 1 0 14:39 ? 00:00:32 php-fpm: master process (/etc/php5/fpm/php-fpm.conf)
spelix 6439 6435 0 14:39 ? 00:00:00 php-fpm: pool batch
spelix 6440 6435 0 14:39 ? 00:00:00 php-fpm: pool batch
www-data 10576 6435 1 18:45 ? 00:00:48 php-fpm: pool www
www-data 10920 6435 1 18:47 ? 00:00:47 php-fpm: pool www
www-data 10927 6435 1 18:47 ? 00:00:46 php-fpm: pool www
COMPANY CONFIDENTIAL 16 – DO NOT DISTRIBUTE #Dynatrace
17. PHP-‐FPM
• Process
Manager
[pool_name]
...
pm = [dynamic/static]
pm.max_children = 5
; only used for dynamic:
pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 3
pm.max_children
=
total
available
memory
/
memory
used
by
1
PHP
process
COMPANY CONFIDENTIAL 17 – DO NOT DISTRIBUTE #Dynatrace
18. PHP-‐FPM
• Security
/etc/php5/fpm/php.ini
expose_php = Off
• Use
mulNple
configuraNons
/etc/init.d/php-fpm start
or
php-fpm -y /path/to/your/php-fpm.conf -c /path/to/your/php.ini
COMPANY CONFIDENTIAL 18 – DO NOT DISTRIBUTE #Dynatrace
19. Nginx
and
PHP
/
IntegraNon
COMPANY CONFIDENTIAL 19 – DO NOT DISTRIBUTE #Dynatrace
20. Nginx
and
PHP
• CommunicaNon
via
sockets
• TCP
vs
Unix
• Unix
slightly
faster
when
used
on
localhost
• Use
TCP
for
high
load
location ~* .php$ {
fastcgi_index index.php;
fastcgi_pass 127.0.0.1:9000;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
}
fastcgi_pass unix:/var/run/php5-fpm.sock;
COMPANY CONFIDENTIAL 20 – DO NOT DISTRIBUTE #Dynatrace
21. IntegraNon
• StaNc
content
to
be
served
by
Nginx
• Dynamic
requests
to
be
sent
to
PHP-‐FPM
server {
listen 80;
root /var/www/test;
index index.php index.html index.htm;
server_name test.whateveryourdomain.is;
location / {
try_files $uri $uri/ @notfound;
}
location ~ .(html|js|css|gif|jpg|jpe|jpeg|png|bmp|tif|pdf|ico)$ {
try_files $uri @notfound;
}
location ~* .php$ {
fastcgi_index index.php;
fastcgi_pass php;
include fastcgi_params;
}
locaation @notfound {
...
}
}
COMPANY CONFIDENTIAL 21 – DO NOT DISTRIBUTE #Dynatrace
22. Nginx
–
PHP
transacNon
flow
COMPANY CONFIDENTIAL 22 – DO NOT DISTRIBUTE #Dynatrace
23. PHAR
–
PHP
Archives
• Easy
way
to
deploy
your
applicaNon
• All
source
files
packed
into
one
*.phar
file
location ~* .(php|phar)$ {
fastcgi_index index.php;
fastcgi_pass 127.0.0.1:9000;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
}
COMPANY CONFIDENTIAL 23 – DO NOT DISTRIBUTE #Dynatrace
24. PHAR
example
root@hzvm01:/var/www/app/src# ls -lrtR
.:
total 8
drwxrwxr-x 2 root root 4096 Oct 10 16:27 lib
-rw-r--r-- 1 root root 27 Oct 10 16:27 index.php
./lib:
total 4
-rw-r--r-- 1 root root 87 Oct 10 16:27 App.php
root@hzvm01:/var/www/app/src# cat index.php
<?php
$app = new App();
?>
root@hzvm01:/var/www/app/src# cat lib/App.php
<?php
class App {
function __construct() {
echo "Starting Applicationn";
}
}
COMPANY CONFIDENTIAL 24 – DO NOT DISTRIBUTE #Dynatrace
25. PHAR
Example
root@hzvm01:/var/www/app# phar pack -f myapp.phar src
lib/App.php
index.php
root@hzvm01:/var/www/app# l myapp.phar
-rw-r--r-- 1 root root 6923 Oct 10 19:51 myapp.phar
location /myapp {
fastcgi_index index.php;
fastcgi_pass 127.0.0.1:9000;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root/myapp.phar;
}
COMPANY CONFIDENTIAL 25 – DO NOT DISTRIBUTE #Dynatrace
28. Benchmarking
Nginx
vs
Apache
• Nginx
running
with
default
seTngs
• Apache
• AllowOverride
None
• MulN-‐process
mode
to
allow
usage
of
mod_php
COMPANY CONFIDENTIAL 28 – DO NOT DISTRIBUTE #Dynatrace
29. StaNc
HTML,
10k
requests,
concurrency
10
Server Software: Apache/2.4.7
Server Hostname: test.hzvm01
Server Port: 88
Document Path: /index.html
Document Length: 21 bytes
Concurrency Level: 10
Time taken for tests: 6.848 seconds
Complete requests: 10000
Failed requests: 0
Total transferred: 2660000 bytes
HTML transferred: 210000 bytes
Requests per second: 1460.25 [#/sec] (mean)
Time per request: 6.848 [ms] (mean)
Time per request: 0.685 [ms] (mean, across all concurrent requests)
Transfer rate: 379.32 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 2 0.9 2 24
Processing: 1 4 1.5 4 26
Waiting: 0 3 1.3 3 24
Total: 2 7 1.7 6 32
Percentage of the requests served within a certain time (ms)
50% 6
66% 7
75% 7
80% 7
90% 8
95% 9
98% 10
99% 12
100% 32 (longest request)
Server Software: nginx/1.4.6
Server Hostname: test.hzvm01
Server Port: 80
Document Path: /index.html
Document Length: 21 bytes
Concurrency Level: 10
Time taken for tests: 4.991 seconds
Complete requests: 10000
Failed requests: 0
Total transferred: 2600000 bytes
HTML transferred: 210000 bytes
Requests per second: 2003.41 [#/sec] (mean)
Time per request: 4.991 [ms] (mean)
Time per request: 0.499 [ms] (mean, across all concurrent requests)
Transfer rate: 508.68 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 2 1.1 2 31
Processing: 1 3 2.0 3 38
Waiting: 0 2 1.7 2 34
Total: 1 5 2.2 5 41
Percentage of the requests served within a certain time (ms)
50% 5
66% 5
75% 5
80% 6
90% 6
95% 7
98% 9
99% 12
100% 41 (longest request)
COMPANY CONFIDENTIAL 29 – DO NOT DISTRIBUTE #Dynatrace
30. StaNc
HTML,
10k
requests,
concurrency
100
Server Software: Apache/2.4.7
Server Hostname: test.hzvm01
Server Port: 88
Document Path: /index.html
Document Length: 21 bytes
Concurrency Level: 100
Time taken for tests: 5.329 seconds
Complete requests: 10000
Failed requests: 0
Total transferred: 2660000 bytes
HTML transferred: 210000 bytes
Requests per second: 1876.35 [#/sec] (mean)
Time per request: 53.295 [ms] (mean)
Time per request: 0.533 [ms] (mean, across all concurrent requests)
Transfer rate: 487.41 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 8 4.5 8 54
Processing: 15 45 18.5 51 120
Waiting: 12 42 18.7 49 108
Total: 26 53 18.2 58 132
Percentage of the requests served within a certain time (ms)
50% 58
66% 60
75% 62
80% 64
90% 69
95% 87
98% 99
99% 105
100% 132 (longest request)
Server Software: nginx/1.4.6
Server Hostname: test.hzvm01
Server Port: 80
Document Path: /index.html
Document Length: 21 bytes
Concurrency Level: 100
Time taken for tests: 4.908 seconds
Complete requests: 10000
Failed requests: 0
Total transferred: 2600000 bytes
HTML transferred: 210000 bytes
Requests per second: 2037.64 [#/sec] (mean)
Time per request: 49.076 [ms] (mean)
Time per request: 0.491 [ms] (mean, across all concurrent requests)
Transfer rate: 517.37 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 9 23 4.4 23 40
Processing: 8 25 5.1 25 48
Waiting: 2 19 4.6 18 41
Total: 22 49 6.4 47 75
Percentage of the requests served within a certain time (ms)
50% 47
66% 50
75% 52
80% 54
90% 58
95% 61
98% 64
99% 65
100% 75 (longest request)
COMPANY CONFIDENTIAL 30 – DO NOT DISTRIBUTE #Dynatrace
31. StaNc
HTML,
10k
requests,
concurrency
500
Server Software: Apache/2.4.7
Server Hostname: test.hzvm01
Server Port: 88
Document Path: /index.html
Document Length: 21 bytes
Concurrency Level: 500
Time taken for tests: 7.628 seconds
Complete requests: 10000
Failed requests: 0
Total transferred: 2660000 bytes
HTML transferred: 210000 bytes
Requests per second: 1310.89 [#/sec] (mean)
Time per request: 381.421 [ms] (mean)
Time per request: 0.763 [ms] (mean, across all concurrent requests)
Transfer rate: 340.52 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 2 67 263.3 16 3050
Processing: 28 246 814.5 95 6605
Waiting: 21 240 814.9 90 6599
Total: 70 312 870.2 113 7556
Percentage of the requests served within a certain time (ms)
50% 113
66% 130
75% 147
80% 158
90% 364
95% 1111
98% 3100
99% 6637
100% 7556 (longest request)
Server Software: nginx/1.4.6
Server Hostname: test.hzvm01
Server Port: 80
Document Path: /index.html
Document Length: 21 bytes
Concurrency Level: 500
Time taken for tests: 4.306 seconds
Complete requests: 10000
Failed requests: 0
Total transferred: 2600000 bytes
HTML transferred: 210000 bytes
Requests per second: 2322.27 [#/sec] (mean)
Time per request: 215.307 [ms] (mean)
Time per request: 0.431 [ms] (mean, across all concurrent requests)
Transfer rate: 589.64 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 47 100 32.0 97 185
Processing: 50 110 36.1 108 231
Waiting: 20 82 30.3 76 188
Total: 119 210 57.8 220 334
Percentage of the requests served within a certain time (ms)
50% 220
66% 248
75% 265
80% 271
90% 285
95% 293
98% 303
99% 306
100% 334 (longest request)
COMPANY CONFIDENTIAL 31 – DO NOT DISTRIBUTE #Dynatrace
32. StaNc
HTML,
10k
requests,
concurrency
1k
Server Software: Apache/2.4.7
Server Hostname: test.hzvm01
Server Port: 88
Document Path: /index.html
Document Length: 21 bytes
Concurrency Level: 1000
Time taken for tests: 9.778 seconds
Complete requests: 10000
Failed requests: 0
Total transferred: 2660000 bytes
HTML transferred: 210000 bytes
Requests per second: 1022.74 [#/sec] (mean)
Time per request: 977.769 [ms] (mean)
Time per request: 0.978 [ms] (mean, across all concurrent requests)
Transfer rate: 265.67 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 28 179 403.5 69 3106
Processing: 39 452 1262.7 117 6677
Waiting: 22 427 1267.6 91 6672
Total: 107 631 1447.3 182 9619
Percentage of the requests served within a certain time (ms)
50% 182
66% 203
75% 258
80% 422
90% 1178
95% 3190
98% 7622
99% 7643
100% 9619 (longest request)
Server Software: nginx/1.4.6
Server Hostname: test.hzvm01
Server Port: 80
Document Path: /index.html
Document Length: 21 bytes
Concurrency Level: 1000
Time taken for tests: 5.302 seconds
Complete requests: 10000
Failed requests: 0
Total transferred: 2600000 bytes
HTML transferred: 210000 bytes
Requests per second: 1886.22 [#/sec] (mean)
Time per request: 530.160 [ms] (mean)
Time per request: 0.530 [ms] (mean, across all concurrent requests)
Transfer rate: 478.92 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 138 238 50.5 229 370
Processing: 72 269 59.7 257 514
Waiting: 62 195 58.3 184 377
Total: 338 507 56.0 518 651
Percentage of the requests served within a certain time (ms)
50% 518
66% 541
75% 547
80% 551
90% 573
95% 580
98% 623
99% 638
100% 651 (longest request)
COMPANY CONFIDENTIAL 32 – DO NOT DISTRIBUTE #Dynatrace
33. Apache
vs
Nginx
(StaNc
HTML)
12
10
8
6
4
2
0
Apache/2.4.7
nginx/1.4.6
10
100
500
1000
COMPANY CONFIDENTIAL 33 – DO NOT DISTRIBUTE #Dynatrace
34. Image
841
bytes,
10k
requests,
concurrency
500
Server Software: Apache/2.4.7
Server Hostname: test.hzvm01
Server Port: 88
Document Path: /images/plus.gif
Document Length: 841 bytes
Concurrency Level: 500
Time taken for tests: 8.036 seconds
Complete requests: 10000
Failed requests: 0
Total transferred: 10880000 bytes
HTML transferred: 8410000 bytes
Requests per second: 1244.35 [#/sec] (mean)
Time per request: 401.817 [ms] (mean)
Time per request: 0.804 [ms] (mean, across all concurrent requests)
Transfer rate: 1322.12 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 63 273.3 7 3046
Processing: 19 229 815.9 80 6596
Waiting: 16 227 816.0 78 6596
Total: 54 293 881.4 88 7561
Percentage of the requests served within a certain time (ms)
50% 88
66% 95
75% 104
80% 109
90% 318
95% 1094
98% 3289
99% 6623
100% 7561 (longest request)
Server Software: nginx/1.4.6
Server Hostname: test.hzvm01
Server Port: 80
Document Path: /images/plus.gif
Document Length: 841 bytes
Concurrency Level: 500
Time taken for tests: 5.239 seconds
Complete requests: 10000
Failed requests: 0
Total transferred: 10820000 bytes
HTML transferred: 8410000 bytes
Requests per second: 1908.76 [#/sec] (mean)
Time per request: 261.950 [ms] (mean)
Time per request: 0.524 [ms] (mean, across all concurrent requests)
Transfer rate: 2016.87 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 55 119 22.0 118 183
Processing: 32 137 27.8 132 260
Waiting: 29 101 25.4 96 202
Total: 156 255 28.1 258 351
Percentage of the requests served within a certain time (ms)
50% 258
66% 268
75% 275
80% 281
90% 288
95% 298
98% 310
99% 317
100% 351 (longest request)
COMPANY CONFIDENTIAL 34 – DO NOT DISTRIBUTE #Dynatrace
35. PHP,
1k
requests,
concurrency
500
Server Software: Apache/2.4.7
Server Hostname: test.hzvm01
Server Port: 88
Document Path: /index.php
Document Length: 12 bytes
Concurrency Level: 500
Time taken for tests: 13.298 seconds
Complete requests: 1000
Failed requests: 0
Total transferred: 199000 bytes
HTML transferred: 12000 bytes
Requests per second: 75.20 [#/sec] (mean)
Time per request: 6649.092 [ms] (mean)
Time per request: 13.298 [ms] (mean, across all concurrent requests)
Transfer rate: 14.61 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 25 30.0 1 68
Processing: 70 2344 4333.5 167 13205
Waiting: 69 2343 4333.4 165 13204
Total: 124 2369 4352.0 171 13264
Percentage of the requests served within a certain time (ms)
50% 171
66% 211
75% 415
80% 7240
90% 13156
95% 13208
98% 13249
99% 13257
100% 13264 (longest request)
Server Software: nginx/1.4.6
Server Hostname: test.hzvm01
Server Port: 80
Document Path: /index.php
Document Length: 12 bytes
Concurrency Level: 500
Time taken for tests: 3.230 seconds
Complete requests: 1000
Failed requests: 0
Total transferred: 178000 bytes
HTML transferred: 12000 bytes
Requests per second: 309.56 [#/sec] (mean)
Time per request: 1615.213 [ms] (mean)
Time per request: 3.230 [ms] (mean, across all concurrent requests)
Transfer rate: 53.81 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 45 44.7 71 106
Processing: 72 643 708.7 204 3057
Waiting: 69 643 708.8 203 3057
Total: 163 688 732.1 227 3133
Percentage of the requests served within a certain time (ms)
50% 227
66% 1147
75% 1215
80% 1249
90% 1380
95% 1433
98% 3128
99% 3131
100% 3133 (longest request)
COMPANY CONFIDENTIAL 35 – DO NOT DISTRIBUTE #Dynatrace
36. PHP,
1k
requests,
concurrency
100
Server Software: Apache/2.4.7
Server Hostname: test.hzvm01
Server Port: 88
Document Path: /index.php
Document Length: 12 bytes
Concurrency Level: 100
Time taken for tests: 1.233 seconds
Complete requests: 1000
Failed requests: 0
Total transferred: 199000 bytes
HTML transferred: 12000 bytes
Requests per second: 810.72 [#/sec] (mean)
Time per request: 123.348 [ms] (mean)
Time per request: 1.233 [ms] (mean, across all concurrent requests)
Transfer rate: 157.55 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 3 7.4 1 34
Processing: 34 112 23.2 107 250
Waiting: 29 111 22.8 106 249
Total: 49 116 22.6 109 250
Percentage of the requests served within a certain time (ms)
50% 109
66% 115
75% 126
80% 133
90% 151
95% 157
98% 171
99% 192
100% 250 (longest request)
Server Software: nginx/1.4.6
Server Hostname: test.hzvm01
Server Port: 80
Document Path: /index.php
Document Length: 12 bytes
Concurrency Level: 100
Time taken for tests: 1.628 seconds
Complete requests: 1000
Failed requests: 0
Total transferred: 178000 bytes
HTML transferred: 12000 bytes
Requests per second: 614.15 [#/sec] (mean)
Time per request: 162.826 [ms] (mean)
Time per request: 1.628 [ms] (mean, across all concurrent requests)
Transfer rate: 106.76 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 5 10.3 1 76
Processing: 15 149 35.4 158 210
Waiting: 15 147 34.7 157 208
Total: 39 154 29.0 160 211
Percentage of the requests served within a certain time (ms)
50% 160
66% 168
75% 173
80% 177
90% 185
95% 193
98% 200
99% 204
100% 211 (longest request)
COMPANY CONFIDENTIAL 36 – DO NOT DISTRIBUTE #Dynatrace
37. Apache
vs
Nginx
-‐
conclusion
• Nginx
faster
for
serving
staNc
files
• Apache
is
faster
for
serving
PHP
only
(low
load)
COMPANY CONFIDENTIAL 37 – DO NOT DISTRIBUTE #Dynatrace
38. Nginx,
Apache
and
PHP
COMPANY CONFIDENTIAL 38 – DO NOT DISTRIBUTE #Dynatrace
39. Nginx,
Apache
and
PHP
COMPANY CONFIDENTIAL 39 – DO NOT DISTRIBUTE #Dynatrace
40. Nginx
–
more
performance
tweaks
• Setup
caching
and
disable
access
logging
location ~ .(html|js|css|gif|jpg|jpe|jpeg|png|bmp|tif|pdf|ico)$ {
expires 30d;
access_log off;
error_log off;
try_files $uri $uri/ =404;
}
• Change
Nmeouts
http {
keepalive_timeout 15;
client_body_timeout 12;
client_header_timeout 12;
send_timeout 10;
}
COMPANY CONFIDENTIAL 40 – DO NOT DISTRIBUTE #Dynatrace
42. Caching
• Server
Side
• Opcode
cache
• Full
page
cache
• Pagelet
cache
• Data
cache
• Client
Side
• Browser
cache
• HTML5
applicaNon
cache
• HTML5
data
storage
(LocalStorage,
SessionStorage)
COMPANY CONFIDENTIAL 42 – DO NOT DISTRIBUTE #Dynatrace
43. PHP
Opcode
Cache
• Integrated
Zend
OpCache
in
PHP
5.5
• Other
technologies
available
hjp://en.wikipedia.org/wiki/List_of_PHP_accelerators
COMPANY CONFIDENTIAL 43 – DO NOT DISTRIBUTE #Dynatrace
47. Full
page
cache
with
Nginx
and
Memcached
• ngx_hjp_memcached_module
server {
location / {
set $memcached_key "$uri";
memcached_pass host:11211;
error_page 404 502 504 = @fallback;
}
location @fallback {
proxy_pass http://backend;
}
}
COMPANY CONFIDENTIAL 47 – DO NOT DISTRIBUTE #Dynatrace
48. Full
page
cache
with
Nginx
and
Memcached
• PHP
$ sudo apt-get install php5-memcached
<?php
…
function cachePage($content) {
$c = new Memcached();
$c->addServer('localhost',11211);
$c->set($_SERVER[”REQUEST_URI"], $content);
}
...
$content = $this->renderPage();
$this->cachePage($content);
...
?>
COMPANY CONFIDENTIAL 48 – DO NOT DISTRIBUTE #Dynatrace
49. PHP,
5k
requests,
concurrency
100
Server Software: Nginx + PHP
Server Hostname: test.hzvm01
Server Port: 80
Document Path: /index.php
Document Length: 10 bytes
Concurrency Level: 100
Time taken for tests: 4.514 seconds
Complete requests: 5000
Failed requests: 0
Total transferred: 625000 bytes
HTML transferred: 50000 bytes
Requests per second: 1107.62 [#/sec] (mean)
Time per request: 90.284 [ms] (mean)
Time per request: 0.903 [ms] (mean, across all concurrent requests)
Transfer rate: 135.21 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 2 3.2 1 38
Processing: 22 88 13.1 87 148
Waiting: 21 87 13.1 86 148
Total: 42 89 12.5 88 148
Percentage of the requests served within a certain time (ms)
50% 88
66% 94
75% 97
80% 98
90% 104
95% 109
98% 125
99% 132
100% 148 (longest request)
COMPANY CONFIDENTIAL 49 – DO NOT DISTRIBUTE #Dynatrace
50. PHP,
5k
requests,
concurrency
100
Server Software: Nginx + Apache + PHP
Server Hostname: test.hzvm01
Server Port: 81
Document Path: /index.php
Document Length: 10 bytes
Concurrency Level: 100
Time taken for tests: 9.207 seconds
Complete requests: 5000
Failed requests: 0
Total transferred: 905000 bytes
HTML transferred: 50000 bytes
Requests per second: 543.05 [#/sec] (mean)
Time per request: 184.146 [ms] (mean)
Time per request: 1.841 [ms] (mean, across all concurrent requests)
Transfer rate: 95.99 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 10 17.0 2 138
Processing: 20 173 73.7 156 1254
Waiting: 19 168 72.3 152 1239
Total: 55 182 72.4 168 1254
Percentage of the requests served within a certain time (ms)
50% 168
66% 197
75% 214
80% 224
90% 256
95% 287
98% 337
99% 427
100% 1254 (longest request)
COMPANY CONFIDENTIAL 50 – DO NOT DISTRIBUTE #Dynatrace
51. PHP,
5k
requests,
concurrency
100
Server Software: Apache/2.4.7
Server Hostname: test.hzvm01
Server Port: 88
Document Path: /index.php
Document Length: 10 bytes
Concurrency Level: 100
Time taken for tests: 7.296 seconds
Complete requests: 5000
Failed requests: 0
Total transferred: 985000 bytes
HTML transferred: 50000 bytes
Requests per second: 685.31 [#/sec] (mean)
Time per request: 145.920 [ms] (mean)
Time per request: 1.459 [ms] (mean, across all concurrent requests)
Transfer rate: 131.84 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 6 6.5 4 56
Processing: 30 138 57.2 120 938
Waiting: 30 135 56.4 117 937
Total: 55 144 55.8 126 938
Percentage of the requests served within a certain time (ms)
50% 126
66% 151
75% 168
80% 178
90% 218
95% 239
98% 267
99% 299
100% 938 (longest request)
COMPANY CONFIDENTIAL 51 – DO NOT DISTRIBUTE #Dynatrace
52. PHP,
5k
requests,
concurrency
100
Server Software: nginx + Memcached
Server Hostname: test.hzvm01
Server Port: 82
Document Path: /index.php
Document Length: 23 bytes
Concurrency Level: 100
Time taken for tests: 3.058 seconds
Complete requests: 5000
Failed requests: 0
Total transferred: 865000 bytes
HTML transferred: 115000 bytes
Requests per second: 1634.92 [#/sec] (mean)
Time per request: 61.165 [ms] (mean)
Time per request: 0.612 [ms] (mean, across all concurrent requests)
Transfer rate: 276.21 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 7 24 6.0 24 59
Processing: 8 36 7.8 36 75
Waiting: 6 28 7.4 29 69
Total: 32 60 9.3 60 101
Percentage of the requests served within a certain time (ms)
50% 60
66% 63
75% 64
80% 66
90% 71
95% 80
98% 87
99% 92
100% 101 (longest request)
COMPANY CONFIDENTIAL 52 – DO NOT DISTRIBUTE #Dynatrace
53. Nginx
FastCGI
cache
• Part
of
the
Nginx
FastCGI
module
fastcgi_cache_path /etc/nginx/cache levels=1:2 keys_zone=APPKEY:100m inactive=60m;
fastcgi_cache_key "$scheme$request_method$host$request_uri";
location ~* .php$ {
fastcgi_index index.php;
fastcgi_pass 127.0.0.1:9000;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_cache APPKEY;
fastcgi_cache_valid 200 60m;
}
COMPANY CONFIDENTIAL 53 – DO NOT DISTRIBUTE #Dynatrace
59. Pagelet
/
Data
cache
• Served
by
webserver
or
applicaNon
• Technologies
• Memcached
• FastCGI
cache
• APC
(alternaNve
PHP
cache)
• Redis
COMPANY CONFIDENTIAL 59 – DO NOT DISTRIBUTE #Dynatrace
60. Client
Side
Caching
location ~ .(html|js|css|gif|jpg|jpe|jpeg|png|bmp|tif|pdf|ico)$ {
expires 30d;
access_log off;
error_log off;
try_files $uri $uri/ =404;
}
• HTML5
• applicaNon
cache
• LocalStorage
• SessionStorage
COMPANY CONFIDENTIAL 60 – DO NOT DISTRIBUTE #Dynatrace
61. PHP,
5k
requests,
concurrency
100
Server Software: Nginx + FastCGI cache
Server Hostname: test.hzvm01
Server Port: 83
Document Path: /index.php
Document Length: 32 bytes
Concurrency Level: 100
Time taken for tests: 2.545 seconds
Complete requests: 5000
Failed requests: 0
Total transferred: 735000 bytes
HTML transferred: 160000 bytes
Requests per second: 1964.62 [#/sec] (mean)
Time per request: 50.900 [ms] (mean)
Time per request: 0.509 [ms] (mean, across all concurrent requests)
Transfer rate: 282.03 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 11 23 5.3 22 46
Processing: 8 27 6.7 25 59
Waiting: 6 20 6.0 18 56
Total: 29 50 7.6 50 85
Percentage of the requests served within a certain time (ms)
50% 50
66% 53
75% 55
80% 56
90% 61
95% 64
98% 67
99% 71
100% 85 (longest request)
COMPANY CONFIDENTIAL 61 – DO NOT DISTRIBUTE #Dynatrace
62. Load
balancing
• ngx_hjp_upstream_module
upstream php_loadbalancer {
server unix:/var/run/php5-fpm.sock weight=5;
server 192.168.56.12:7777 weight=2;
server 192.168.56.13:7777;
}
server {
listen 80;
root /home/www/test;
server_name test.hzvm01;
location / {
try_files $uri =405;
}
location ~ .php$ {
fastcgi_pass php_loadbalancer;
fastcgi_index index.php;
include fastcgi_params;
}
}
COMPANY CONFIDENTIAL 62 – DO NOT DISTRIBUTE #Dynatrace
79. Used
Tools
• Apache
Benchmark
• Dynatrace
ApplicaNon
Monitoring
• Free
Trial
for
30
days
• Free
for
developers
on
local
machine
• hjp://www.dynatrace.com
hjp://bitly.com/djrial
COMPANY CONFIDENTIAL 79 – DO NOT DISTRIBUTE #Dynatrace