SlideShare a Scribd company logo
Nginx
  +
 PHP
2012-09-15 @wokamoto
デジタルキューブ で WordPress とか、
サイトのパフォーマンスチューニングとか
やってます。
 twitter : @wokamoto
 facebook : wokamoto
 http://profiles.wordpress.org/wokamoto
 https://github.com/wokamoto
http://nginx.org/ja/
WordPress
高速化&スマート運用
必携ガイド
Nginx + PHP
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
Nginx + PHP
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.sock
listen.owner = nginx
listen.group = nginx
listen.mode = 0666

user = nginx
group = nginx

pm = dynamic
pm.max_children = 15
pm.start_servers = 5
pm.min_spare_servers = 5
pm.max_spare_servers = 7
Nginx's
performance
Apache, mod_php, WordPress
$ 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)
Nginx, php-fpm, WordPress




          1617
           1617
$ 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)
Reverse
proxy Cache
$ 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)
【 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";
    }
}
Reverse Proxy にキャッシュ
    させる時間を PHP で制御
<?php
header('X-Accel-Expires: '. 60 * 60 * 24);
?>
Easy to Use
http://ja.megumi-cloud.com/
Twitter : @wokamoto

More Related Content

Nginx + PHP

  • 1. Nginx + PHP 2012-09-15 @wokamoto
  • 2. デジタルキューブ で WordPress とか、 サイトのパフォーマンスチューニングとか やってます。 twitter : @wokamoto facebook : wokamoto http://profiles.wordpress.org/wokamoto https://github.com/wokamoto
  • 8. W3Techs の調査では 上位 1,000,000 サイトの 12.5%、 上位 100,000 サイトの 19.4% で採用されている http://w3techs.com/technologies/ cross/web_server/ranking
  • 10. How to use Nginx with PHP?
  • 11. Apache の mod_php のように Nginx に直接組み込むことは できない →fast CGI ( php-fpm ) で
  • 12. 【 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; } }
  • 13. 【 php-fpm.conf の例 】 [www] listen = /var/run/php-fpm.sock listen.owner = nginx listen.group = nginx listen.mode = 0666 user = nginx group = nginx pm = dynamic pm.max_children = 15 pm.start_servers = 5 pm.min_spare_servers = 5 pm.max_spare_servers = 7
  • 16. $ 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)
  • 18. $ 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)
  • 20. $ 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)
  • 21. 【 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; } : }
  • 22. 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; } }
  • 23. 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"; } }
  • 24. Reverse Proxy にキャッシュ させる時間を PHP で制御 <?php header('X-Accel-Expires: '. 60 * 60 * 24); ?>