Successfully reported this slideshow.

Nginx + PHP

18,374 views

Published on

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

Published in: Technology

Nginx + PHP

  1. 1. Nginx + PHP2012-09-15 @wokamoto
  2. 2. デジタルキューブ で WordPress とか、サイトのパフォーマンスチューニングとかやってます。 twitter : @wokamoto facebook : wokamoto http://profiles.wordpress.org/wokamoto https://github.com/wokamoto
  3. 3. http://nginx.org/ja/
  4. 4. WordPress高速化&スマート運用必携ガイド
  5. 5. Nginx ?
  6. 6. http://nginx.com/ , http://nginx.org/
  7. 7. W3Techs の調査では上位 1,000,000 サイトの 12.5%、上位 100,000 サイトの 19.4%で採用されている http://w3techs.com/technologies/ cross/web_server/ranking
  8. 8. How to use Nginx with PHP?
  9. 9. Apache の mod_php のようにNginx に直接組み込むことは できない →fast CGI ( php-fpm ) で
  10. 10. 【 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; }}
  11. 11. 【 php-fpm.conf の例 】[www]listen = /var/run/php-fpm.socklisten.owner = nginxlisten.group = nginxlisten.mode = 0666user = nginxgroup = nginxpm = dynamicpm.max_children = 15pm.start_servers = 5pm.min_spare_servers = 5pm.max_spare_servers = 7
  12. 12. Nginxsperformance
  13. 13. Apache, mod_php, WordPress
  14. 14. $ ab -n 100 -c 10 AWS t1.micro Apache, php with APC(WordPress トップページ) Requests per second: 4.26 [#/sec] (mean) Time per request: 2346.174 [ms] (mean) Connection Times Total: 2309 [ms] (mean)
  15. 15. Nginx, php-fpm, WordPress 1617 1617
  16. 16. $ ab -n 100 -c 10 AWS t1.micro Nginx, php with APC(WordPress トップページ) Requests per second: 5.79 [#/sec] (mean) Time per request: 1726.535 [ms] (mean) Connection Times Total: 1617 [ms] (mean)
  17. 17. Reverseproxy Cache
  18. 18. $ ab -n 1000 -c 100 AWS t1.micro Nginx, php with APC(WordPress トップページ) Requests per second: 141.24 [#/sec] (mean) Time per request: 708.007 [ms] (mean) Connection Times Total: 636 [ms] (mean)
  19. 19. 【 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; } :}
  20. 20. 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; }}
  21. 21. 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"; }}
  22. 22. Reverse Proxy にキャッシュ させる時間を PHP で制御<?phpheader(X-Accel-Expires: . 60 * 60 * 24);?>
  23. 23. Easy to Use
  24. 24. http://ja.megumi-cloud.com/
  25. 25. Twitter : @wokamoto

×