nginx+php-fpm性能參數優化原則
隨著nginx的使用率不斷提高,很多朋友的服務器都在改用nginx,但是nginx處理php程序的方式是php-fpm,與apache的handler有所不同,由于很多人不會優化配置,導致網站打開的時候502、503錯誤的發生率非常高。本文中泰州網站建設跟(gen)大(da)家(jia)分享一下nginx+php-fpm性(xing)能參數的(de)優化。
1.worker_processes越(yue)大(da)越(yue)好(hao)(一定數量后性能增加不明顯)
2.worker_cpu_affinity所(suo)有cpu平(ping)分worker_processes要(yao)比每個worker_processes都跨cpu分配性(xing)能(neng)要(yao)好;不考慮(lv)php的執(zhi)行,測試結果worker_processes數量是cpu核數的2倍性(xing)能(neng)最優
3.unix domain socket(共享內(nei)存的(de)方式)要(yao)比tcp網絡(luo)端口配(pei)置性能要(yao)好(hao)
不考慮backlog,請求速度有量(liang)級的飛躍,但錯(cuo)誤率(lv)超過50%
加上backlog,性能有10%左右(you)提升
4.調整nginx、php-fpm和內核的backlog(積(ji)壓(ya)),connect() to unix:/tmp/php-fpm.socket failed (11: Resource temporarily unavailable) while connecting to upstream錯誤的返回會(hui)減少
nginx:
配置文件的server塊
listen 80 default backlog=1024;
php-fpm:
配置文件的
listen.backlog = 2048
kernel參數:
/etc/sysctl.conf,不能(neng)低于上面的配置
net.ipv4.tcp_max_syn_backlog = 4096 net.core.netdev_max_backlog = 4096
5.增加單臺服(fu)務器上的php-fpm的master實例,會增加fpm的處理(li)能(neng)(neng)力,也能(neng)(neng)減(jian)少報錯(cuo)返(fan)回的幾率
多(duo)(duo)實例(li)啟動(dong)方(fang)法,使(shi)用多(duo)(duo)個配置文件:
/usr/local/php/sbin/php-fpm -y /usr/local/php/etc/php-fpm.conf & /usr/local/php/sbin/php-fpm -y /usr/local/php/etc/php-fpm1.conf &
nginx的fastcgi配(pei)置
upstream phpbackend { # server 127.0.0.1:9000 weight=100 max_fails=10 fail_timeout=30; # server 127.0.0.1:9001 weight=100 max_fails=10 fail_timeout=30; # server 127.0.0.1:9002 weight=100 max_fails=10 fail_timeout=30; # server 127.0.0.1:9003 weight=100 max_fails=10 fail_timeout=30; server unix:/var/www/php-fpm.sock weight=100 max_fails=10 fail_timeout=30; server unix:/var/www/php-fpm1.sock weight=100 max_fails=10 fail_timeout=30; server unix:/var/www/php-fpm2.sock weight=100 max_fails=10 fail_timeout=30; server unix:/var/www/php-fpm3.sock weight=100 max_fails=10 fail_timeout=30; # server unix:/var/www/php-fpm4.sock weight=100 max_fails=10 fail_timeout=30; # server unix:/var/www/php-fpm5.sock weight=100 max_fails=10 fail_timeout=30; # server unix:/var/www/php-fpm6.sock weight=100 max_fails=10 fail_timeout=30; # server unix:/var/www/php-fpm7.sock weight=100 max_fails=10 fail_timeout=30; } location ~ .php* { fastcgi_pass phpbackend; # fastcgi_pass unix:/var/www/php-fpm.sock; fastcgi_index index.php; .......... }
6.測試(shi)環境和結果
內存2G
swap2G
cpu 2核Intel(R) Xeon(R) CPU E5405 @ 2.00GHz
采用ab遠程(cheng)訪(fang)問測試,測試程(cheng)序為php的字符串處理程(cheng)序
1) 在開4個php-fpm實例,nginx 8個worker_processes每(mei)個cpu4個worker_processes,backlog為1024,php的backlog為2048,內核(he)backlog為4096,采用unix domain socket連接的情(qing)況下,其他保持(chi)參數不變
性能(neng)和錯誤率較為平衡,可接(jie)受,超過4個fpm實(shi)例(li),性能(neng)開始下降(jiang),錯誤率并沒(mei)有(you)明顯下降(jiang)
結論是fpm實(shi)例數,worker_processes數和cpu保持倍數關系,性能(neng)較(jiao)高
影響性能和報錯的參(can)數為
php-fpm實例(li),nginx worker_processes數量,fpm的max_request,php的backlog,unix domain socket
10W請求,500并發無報(bao)(bao)錯,1000并發報(bao)(bao)錯率為0.9%
500并發:
Time taken for tests: 25 seconds avg.
Complete requests: 100000
Failed requests: 0
Write errors: 0
Requests per second: 4000 [#/sec] (mean) avg.
Time per request: 122.313 [ms] (mean)
Time per request: 0.245 [ms] (mean, across all concurrent requests)
Transfer rate: 800 [Kbytes/sec] received avg.
1000并發:
Time taken for tests: 25 seconds avg.
Complete requests: 100000
Failed requests: 524
(Connect: 0, Length: 524, Exceptions: 0)
Write errors: 0
Non-2xx responses: 524
Requests per second: 3903.25 [#/sec] (mean)
Time per request: 256.197 [ms] (mean)
Time per request: 0.256 [ms] (mean, across all concurrent requests)
Transfer rate: 772.37 [Kbytes/sec] received
2)在其他參數不變,unix domain socket換為tcp網絡端口連接,結果如(ru)下(xia)
500并發:
Concurrency Level: 500
Time taken for tests: 26.934431 seconds
Complete requests: 100000
Failed requests: 0
Write errors: 0
Requests per second: 3712.72 [#/sec] (mean)
Time per request: 134.672 [ms] (mean)
Time per request: 0.269 [ms] (mean, across all concurrent requests)
Transfer rate: 732.37 [Kbytes/sec] received
1000并發(fa):
Concurrency Level: 1000
Time taken for tests: 28.385349 seconds
Complete requests: 100000
Failed requests: 0
Write errors: 0
Requests per second: 3522.94 [#/sec] (mean)
Time per request: 283.853 [ms] (mean)
Time per request: 0.284 [ms] (mean, across all concurrent requests)
Transfer rate: 694.94 [Kbytes/sec] received
與(yu)1)比(bi)較,有大約10%的(de)性能下降
7. 5.16調整fpm的max_request參數為(wei)1000,并發1000報錯返(fan)回降到200個以下,
Transfer rate在800左右