SlideShare une entreprise Scribd logo
1  sur  27
Télécharger pour lire hors ligne
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 ?
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.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

Contenu connexe

Tendances

Varnish: Making eZ Publish sites fly
Varnish: Making eZ Publish sites flyVarnish: Making eZ Publish sites fly
Varnish: Making eZ Publish sites fly
Peter Keung
 
Socket programming with php
Socket programming with phpSocket programming with php
Socket programming with php
Elizabeth Smith
 
Engage 2013 - Multi Channel Data Collection
Engage 2013 - Multi Channel Data CollectionEngage 2013 - Multi Channel Data Collection
Engage 2013 - Multi Channel Data Collection
Webtrends
 
Eduardo Silva - monkey http-server everywhere
Eduardo Silva - monkey http-server everywhereEduardo Silva - monkey http-server everywhere
Eduardo Silva - monkey http-server everywhere
StarTech Conference
 
Realtime Communication Techniques with PHP
Realtime Communication Techniques with PHPRealtime Communication Techniques with PHP
Realtime Communication Techniques with PHP
WaterSpout
 

Tendances (20)

Ajax basics
Ajax basicsAjax basics
Ajax basics
 
Varnish: Making eZ Publish sites fly
Varnish: Making eZ Publish sites flyVarnish: Making eZ Publish sites fly
Varnish: Making eZ Publish sites fly
 
Socket programming with php
Socket programming with phpSocket programming with php
Socket programming with php
 
To Hire, or to train, that is the question (Percona Live 2014)
To Hire, or to train, that is the question (Percona Live 2014)To Hire, or to train, that is the question (Percona Live 2014)
To Hire, or to train, that is the question (Percona Live 2014)
 
LogStash - Yes, logging can be awesome
LogStash - Yes, logging can be awesomeLogStash - Yes, logging can be awesome
LogStash - Yes, logging can be awesome
 
HTTP 완벽가이드- 13 다이제스트 인증
HTTP 완벽가이드- 13 다이제스트 인증HTTP 완벽가이드- 13 다이제스트 인증
HTTP 완벽가이드- 13 다이제스트 인증
 
HTTP For the Good or the Bad
HTTP For the Good or the BadHTTP For the Good or the Bad
HTTP For the Good or the Bad
 
From zero to hero - Easy log centralization with Logstash and Elasticsearch
From zero to hero - Easy log centralization with Logstash and ElasticsearchFrom zero to hero - Easy log centralization with Logstash and Elasticsearch
From zero to hero - Easy log centralization with Logstash and Elasticsearch
 
Elastic stack
Elastic stackElastic stack
Elastic stack
 
Tecnologias Open Source para Alta Disponibilidade e Segurança de Aplicações Web
Tecnologias Open Source para  Alta Disponibilidade e Segurança de Aplicações WebTecnologias Open Source para  Alta Disponibilidade e Segurança de Aplicações Web
Tecnologias Open Source para Alta Disponibilidade e Segurança de Aplicações Web
 
Engage 2013 - Multi Channel Data Collection
Engage 2013 - Multi Channel Data CollectionEngage 2013 - Multi Channel Data Collection
Engage 2013 - Multi Channel Data Collection
 
Tools used for debugging
Tools used for debuggingTools used for debugging
Tools used for debugging
 
ReplacingSquidWithATS
ReplacingSquidWithATSReplacingSquidWithATS
ReplacingSquidWithATS
 
Eduardo Silva - monkey http-server everywhere
Eduardo Silva - monkey http-server everywhereEduardo Silva - monkey http-server everywhere
Eduardo Silva - monkey http-server everywhere
 
Puppet
PuppetPuppet
Puppet
 
Redis as a message queue
Redis as a message queueRedis as a message queue
Redis as a message queue
 
Realtime Communication Techniques with PHP
Realtime Communication Techniques with PHPRealtime Communication Techniques with PHP
Realtime Communication Techniques with PHP
 
Ruby Postgres
Ruby PostgresRuby Postgres
Ruby Postgres
 
Magento 2 Seminar - Miguel Balparda - M2 with PHP 7 and Varnish
Magento 2 Seminar - Miguel Balparda - M2 with PHP 7 and VarnishMagento 2 Seminar - Miguel Balparda - M2 with PHP 7 and Varnish
Magento 2 Seminar - Miguel Balparda - M2 with PHP 7 and Varnish
 
Scaling WordPress
Scaling WordPressScaling WordPress
Scaling WordPress
 

En vedette

PHP-FPMとuWSGI——mod_php以外の選択肢を探る
PHP-FPMとuWSGI——mod_php以外の選択肢を探るPHP-FPMとuWSGI——mod_php以外の選択肢を探る
PHP-FPMとuWSGI——mod_php以外の選択肢を探る
Yoshio Hanawa
 

En vedette (20)

PHP를 위한 NginX(엔진엑스) 시작과 설정
PHP를 위한 NginX(엔진엑스) 시작과 설정PHP를 위한 NginX(엔진엑스) 시작과 설정
PHP를 위한 NginX(엔진엑스) 시작과 설정
 
From LAMP to LNNP
From LAMP to LNNPFrom LAMP to LNNP
From LAMP to LNNP
 
mod_php vs. FastCGI 原理与比较
mod_php vs. FastCGI 原理与比较mod_php vs. FastCGI 原理与比较
mod_php vs. FastCGI 原理与比较
 
Running php on nginx
Running php on nginxRunning php on nginx
Running php on nginx
 
Word press on conoha このべん #3
Word press on conoha このべん #3Word press on conoha このべん #3
Word press on conoha このべん #3
 
Supercharging your PHP pages with mod_lsapi in CloudLinux OS
Supercharging your PHP pages with mod_lsapi in CloudLinux OSSupercharging your PHP pages with mod_lsapi in CloudLinux OS
Supercharging your PHP pages with mod_lsapi in CloudLinux OS
 
High performance PHP: Scaling and getting the most out of your infrastructure
High performance PHP: Scaling and getting the most out of your infrastructureHigh performance PHP: Scaling and getting the most out of your infrastructure
High performance PHP: Scaling and getting the most out of your infrastructure
 
Techtalk2015 MOD_PHP vs PHP-FPM
Techtalk2015 MOD_PHP vs PHP-FPMTechtalk2015 MOD_PHP vs PHP-FPM
Techtalk2015 MOD_PHP vs PHP-FPM
 
PHP7実環境ベンチ2016春
PHP7実環境ベンチ2016春PHP7実環境ベンチ2016春
PHP7実環境ベンチ2016春
 
PHP Files: An Introduction
PHP Files: An IntroductionPHP Files: An Introduction
PHP Files: An Introduction
 
第2回勉強会資料 柏木
第2回勉強会資料 柏木第2回勉強会資料 柏木
第2回勉強会資料 柏木
 
mod_php vs FastCGI vs FPM vs CLI
mod_php vs FastCGI vs FPM vs CLImod_php vs FastCGI vs FPM vs CLI
mod_php vs FastCGI vs FPM vs CLI
 
Nginx pres
Nginx presNginx pres
Nginx pres
 
PHP-FPMとuWSGI——mod_php以外の選択肢を探る
PHP-FPMとuWSGI——mod_php以外の選択肢を探るPHP-FPMとuWSGI——mod_php以外の選択肢を探る
PHP-FPMとuWSGI——mod_php以外の選択肢を探る
 
Ultrafast WordPress Virtual Word camp2015
Ultrafast WordPress Virtual  Word camp2015 Ultrafast WordPress Virtual  Word camp2015
Ultrafast WordPress Virtual Word camp2015
 
realpathキャッシュと OPcacheの面倒すぎる関係
realpathキャッシュと OPcacheの面倒すぎる関係realpathキャッシュと OPcacheの面倒すぎる関係
realpathキャッシュと OPcacheの面倒すぎる関係
 
PHP development with Docker
PHP development with DockerPHP development with Docker
PHP development with Docker
 
Alternative Infrastucture
Alternative InfrastuctureAlternative Infrastucture
Alternative Infrastucture
 
How to deploy PHP projects with docker
How to deploy PHP projects with dockerHow to deploy PHP projects with docker
How to deploy PHP projects with docker
 
How Flipkart scales PHP
How Flipkart scales PHPHow Flipkart scales PHP
How Flipkart scales PHP
 

Similaire à Nginx + PHP

Nginx internals
Nginx internalsNginx internals
Nginx internals
liqiang xu
 
WordPress Security - ওয়ার্ডপ্রেসের সিকিউরিটি
WordPress Security - ওয়ার্ডপ্রেসের সিকিউরিটিWordPress Security - ওয়ার্ডপ্রেসের সিকিউরিটি
WordPress Security - ওয়ার্ডপ্রেসের সিকিউরিটি
Faysal Shahi
 
Study2study#4 nginx conf_1_24
Study2study#4 nginx conf_1_24Study2study#4 nginx conf_1_24
Study2study#4 nginx conf_1_24
Naoya Nakazawa
 
Facebook的缓存系统
Facebook的缓存系统Facebook的缓存系统
Facebook的缓存系统
yiditushe
 
X64服务器 lnmp服务器部署标准 new
X64服务器 lnmp服务器部署标准 newX64服务器 lnmp服务器部署标准 new
X64服务器 lnmp服务器部署标准 new
Yiwei Ma
 

Similaire à Nginx + PHP (20)

Running PHP on nginx
Running PHP on nginxRunning PHP on nginx
Running PHP on nginx
 
Running PHP on Nginx
Running PHP on NginxRunning PHP on Nginx
Running PHP on Nginx
 
High Availability Content Caching with NGINX
High Availability Content Caching with NGINXHigh Availability Content Caching with NGINX
High Availability Content Caching with NGINX
 
High Availability Content Caching with NGINX
High Availability Content Caching with NGINXHigh Availability Content Caching with NGINX
High Availability Content Caching with NGINX
 
WordPress + NGINX Best Practices with EasyEngine
WordPress + NGINX Best Practices with EasyEngineWordPress + NGINX Best Practices with EasyEngine
WordPress + NGINX Best Practices with EasyEngine
 
PHP conference Berlin 2015: running PHP on Nginx
PHP conference Berlin 2015: running PHP on NginxPHP conference Berlin 2015: running PHP on Nginx
PHP conference Berlin 2015: running PHP on Nginx
 
10 Million hits a day with WordPress using a $15 VPS
10 Million hits a day  with WordPress using a $15 VPS10 Million hits a day  with WordPress using a $15 VPS
10 Million hits a day with WordPress using a $15 VPS
 
Apache and PHP: Why httpd.conf is your new BFF!
Apache and PHP: Why httpd.conf is your new BFF!Apache and PHP: Why httpd.conf is your new BFF!
Apache and PHP: Why httpd.conf is your new BFF!
 
Nginx internals
Nginx internalsNginx internals
Nginx internals
 
ApacheConNA 2015: What's new in Apache httpd 2.4
ApacheConNA 2015: What's new in Apache httpd 2.4ApacheConNA 2015: What's new in Apache httpd 2.4
ApacheConNA 2015: What's new in Apache httpd 2.4
 
Oscon 2011 - ATS
Oscon 2011 - ATSOscon 2011 - ATS
Oscon 2011 - ATS
 
How to install nginx vs unicorn
How to install nginx vs unicornHow to install nginx vs unicorn
How to install nginx vs unicorn
 
How to turn any dynamic website into a static site | 24.01.2018 | Artem Danil...
How to turn any dynamic website into a static site | 24.01.2018 | Artem Danil...How to turn any dynamic website into a static site | 24.01.2018 | Artem Danil...
How to turn any dynamic website into a static site | 24.01.2018 | Artem Danil...
 
ITB2019 NGINX Overview and Technical Aspects - Kevin Jones
ITB2019 NGINX Overview and Technical Aspects - Kevin JonesITB2019 NGINX Overview and Technical Aspects - Kevin Jones
ITB2019 NGINX Overview and Technical Aspects - Kevin Jones
 
WordPress Security - ওয়ার্ডপ্রেসের সিকিউরিটি
WordPress Security - ওয়ার্ডপ্রেসের সিকিউরিটিWordPress Security - ওয়ার্ডপ্রেসের সিকিউরিটি
WordPress Security - ওয়ার্ডপ্রেসের সিকিউরিটি
 
Zend Con 2008 Slides
Zend Con 2008 SlidesZend Con 2008 Slides
Zend Con 2008 Slides
 
Less and faster – Cache tips for WordPress developers
Less and faster – Cache tips for WordPress developersLess and faster – Cache tips for WordPress developers
Less and faster – Cache tips for WordPress developers
 
Study2study#4 nginx conf_1_24
Study2study#4 nginx conf_1_24Study2study#4 nginx conf_1_24
Study2study#4 nginx conf_1_24
 
Facebook的缓存系统
Facebook的缓存系统Facebook的缓存系统
Facebook的缓存系统
 
X64服务器 lnmp服务器部署标准 new
X64服务器 lnmp服务器部署标准 newX64服务器 lnmp服务器部署标准 new
X64服务器 lnmp服务器部署标准 new
 

Plus de Wataru OKAMOTO (10)

re:Invent 2015 参加報告
re:Invent 2015 参加報告re:Invent 2015 参加報告
re:Invent 2015 参加報告
 
Amimoto auto scaling
Amimoto auto scalingAmimoto auto scaling
Amimoto auto scaling
 
wp cli
wp cliwp cli
wp cli
 
Wckansai 2014
Wckansai 2014Wckansai 2014
Wckansai 2014
 
Sacss sp5
Sacss sp5Sacss sp5
Sacss sp5
 
StaticPress
StaticPressStaticPress
StaticPress
 
WordPress Cloud Design Pattern
WordPress  Cloud Design PatternWordPress  Cloud Design Pattern
WordPress Cloud Design Pattern
 
High performance web server
High performance web serverHigh performance web server
High performance web server
 
Mastering WordPress Vol.1
Mastering WordPress Vol.1Mastering WordPress Vol.1
Mastering WordPress Vol.1
 
WordPress を使いこなそう
WordPress を使いこなそうWordPress を使いこなそう
WordPress を使いこなそう
 

Dernier

+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 

Dernier (20)

Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024
 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation Strategies
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsTop 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 

Nginx + PHP

  • 1. Nginx + PHP 2012-09-15 @wokamoto
  • 2. デジタルキューブ で WordPress とか、 サイトのパフォーマンスチューニングとか やってます。 twitter : @wokamoto facebook : wokamoto http://profiles.wordpress.org/wokamoto https://github.com/wokamoto
  • 5.
  • 8. W3Techs の調査では 上位 1,000,000 サイトの 12.5%、 上位 100,000 サイトの 19.4% で採用されている http://w3techs.com/technologies/ cross/web_server/ranking
  • 9.
  • 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); ?>