1.worker_processes 越大越好(一定數量後效能增加不明顯) 2.worker_cpu_affinity 所有cpu平分worker_processes 要比每個worker_processes 都跨cpu分配效能要好;不考慮php的執行,測試結果worker_processes數量是cpu核心數的2倍效能最優 3.unix domain socket(共用記憶體的方式)要比tcp網路連接埠配置效能要好不考慮backlog,請求速度有量級的飛躍,但錯誤率超過50%加上backlog,效能有10%左右提升 4.調整nginx、php-fpm和核心的backlog(積壓),connect() to unix:/tmp/php-fpm.socket failed (11: Resource temporarily unavailable) while connecting to upstream錯誤的返回會減少nginx:設定檔的server塊 listen 80 default backlog=1024; php-fpm:設定檔的 listen.backlog = 2048 kernel參數:/etc/sysctl.conf,不能低於上面的配置 net.ipv4.tcp_max_syn_backlog = 4096net.core.netdev_max_backlog = 4096 5.增加單台伺服器上的php-fpm的master執行個體,會增加fpm的處理能力,也能減少報錯返回的幾率多執行個體啟動方法,使用多個設定檔: /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配置 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.測試環境和結果 記憶體2Gswap2Gcpu 2核 Intel(R) Xeon(R) CPU E5405 @ 2.00GHz採用ab遠端存取測試,測試程式為php的字串處理常式 1)在開4個php-fpm執行個體,nginx 8個worker_processes 每個cpu4個worker_processes ,backlog為1024,php的backlog為2048,核心backlog為4096,採用unix domain socket串連的情況下,其他保持參數不變 效能和錯誤率較為平衡,可接受,超過4個fpm執行個體,效能開始下降,錯誤率並沒有明顯下降結論是fpm執行個體數,worker_processes數和cpu保持倍數關係,效能較高影響效能和報錯的參數為php-fpm執行個體,nginx worker_processes數量,fpm的max_request,php的backlog,unix domain socket 10W請求,500並發無報錯,1000並發報錯率為0.9% 500並發: Time taken for tests: 25 seconds avg.Complete requests: 100000Failed requests: 0Write errors: 0Requests 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: 100000Failed requests: 524(Connect: 0, Length: 524, Exceptions: 0)Write errors: 0Non-2xx responses: 524Requests 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網路連接埠串連,結果如下 500並發: Concurrency Level: 500Time taken for tests: 26.934431 secondsComplete requests: 100000Failed requests: 0Write errors: 0Requests 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並發: Concurrency Level: 1000Time taken for tests: 28.385349 secondsComplete requests: 100000Failed requests: 0Write errors: 0Requests 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 與1)比較,有大約10%的效能下降 7. 調整fpm的max_request參數為1000,並發1000報錯返回降到200個以下,Transfer rate在800左右