27
Nginx + PHP 2012-09-15 @wokamoto

Nginx + PHP

Embed Size (px)

DESCRIPTION

2012-09-15 PHP カンファレンス

Citation preview

Nginx+

PHP2012-09-15 @wokamoto

twitter : @wokamoto

facebook : wokamoto

http://profiles.wordpress.org/wokamoto

https://github.com/wokamoto

デジタルキューブ で WordPress とか、サイトのパフォーマンスチューニングとかやってます。

http://nginx.org/ja/

Nginx ?

http://nginx.com/ , http://nginx.org/

W3Techs の調査では

上位 1,000,000 サイトの 12.5%、上位 100,000 サイトの 19.4%

で採用されている

http://w3techs.com/technologies/cross/web_server/ranking

How to use Nginx

with PHP?

Apache の mod_php のようにNginx に直接組み込むことは

できない

→fast CGI ( php-fpm ) で

【 nginx.conf の例 】server { listen 80 default; server_name _; root /path/to/app; index index.php index.html index.htm; charset utf-8;

location ~ \.php$ { fastcgi_pass unix:/var/run/php-fpm.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; }}

【 php-fpm.conf の例 】[www]listen = /var/run/php-fpm.socklisten.owner = nginxlisten.group = nginxlisten.mode = 0666

user = nginxgroup = nginx

pm = dynamicpm.max_children = 15pm.start_servers = 5pm.min_spare_servers = 5pm.max_spare_servers = 7

Nginx'sperformance

Apache, mod_php, WordPress

Requests per second: 4.26 [#/sec] (mean)

Time per request: 2346.174 [ms] (mean)

Connection Times Total: 2309 [ms] (mean)

$ ab -n 100 -c 10AWS t1.micro

Apache, php with APC(WordPress トップページ)

Nginx, php-fpm, WordPress

16171617

Requests per second: 5.79 [#/sec] (mean)

Time per request: 1726.535 [ms] (mean)

Connection Times Total: 1617 [ms] (mean)

$ ab -n 100 -c 10AWS t1.micro

Nginx, php with APC(WordPress トップページ)

Reverseproxy Cache

Requests per second: 141.24 [#/sec] (mean)

Time per request: 708.007 [ms] (mean)

Connection Times Total: 636 [ms] (mean)

$ ab -n 1000 -c 100AWS t1.micro

Nginx, php with APC(WordPress トップページ)

【 nginx.conf の例 】http { : proxy_cache_path /var/cache/nginx/proxy_cache levels=1:2 keys_zone=czone:32m max_size=256m inactive=1440m; proxy_temp_path /var/cache/nginx/proxy_temp; proxy_cache_key "$scheme://$host$request_uri"; proxy_set_header Host $host; proxy_set_header Remote-Addr $remote_addr; proxy_set_header X-Forwarded-Host $host; proxy_set_header X-Forwarded-Server $host; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Accept-Encoding ""; proxy_connect_timeout 5; proxy_send_timeout 10; proxy_read_timeout 120; proxy_cache_lock on; proxy_cache_lock_timeout 5s;

upstream backend { server unix:/var/run/nginx-backend.sock; } :}

server { listen 80 default; server_name _; root /path/to/app; index index.php index.html index.htm;

location ~* \.(js|css|html?|xml|jpe?g|gif|png|swf|wmv|flv|ico)$ { expires 365d; }

location / { set $do_not_cache 0; if ($request_method = POST) { set $do_not_cache 1; } proxy_no_cache $do_not_cache; proxy_cache_bypass $do_not_cache; proxy_redirect off; proxy_cache czone; proxy_cache_key "$scheme://$host$request_uri$is_args$args$mobile"; proxy_cache_valid 200 0m; proxy_pass http://backend; }}

server { listen unix:/var/run/nginx-backend.sock; server_name _; root /path/to/app; index index.php index.html index.htm; charset utf-8;

gzip off; gzip_vary off;

location ~ \.php$ { fastcgi_pass unix:/var/run/php-fpm.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; fastcgi_pass_header "X-Accel-Redirect"; fastcgi_pass_header "X-Accel-Buffering"; fastcgi_pass_header "X-Accel-Charset"; fastcgi_pass_header "X-Accel-Expires"; fastcgi_pass_header "X-Accel-Limit-Rate"; }}

<?phpheader('X-Accel-Expires: '. 60 * 60 * 24);?>

Reverse Proxy にキャッシュさせる時間を PHP で制御

Easy to Use

Twitter : @wokamoto