我一個統計程序估計要跑1分多鐘以上php
查看了一個php-fpm 配置文件nginx
[13-Oct-2013 12:06:07] WARNING: [pool www] child 7458, script '/home/wwwroot/admin/index.php' (request: "GET /index.php") execution timed out (101.515909 sec), terminating [13-Oct-2013 12:06:07] WARNING: [pool www] child 7458 exited on signal 15 (SIGTERM) after 1130895.840878 seconds from start [13-Oct-2013 12:06:07] NOTICE: [pool www] child 24885 started 很明顯了sql
部分PHP程序的執行時間超過了Nginx的等待時間,能夠適當增長nginx.conf配置文件中FastCGI的timeout時間數據庫
google了一較之後vim
http://rtcamp.com/wordpress-nginx/tutorials/php/increase-script-execution-time/app
Changes in php.iniwordpress
If you want to change max execution time limit for php scripts from 30 seconds (default) to 300 seconds.php-fpm
vim /etc/php5/fpm/php.ini Set…post
max_execution_time = 300 In Apache, applications running PHP as a module above would have suffice. But in our case we need to make this change at 2 more places.優化
Changes in PHP-FPM
This is only needed if you have already un-commented request_terminate_timeout parameter before. It is commented by default, and takes value of max_execution_time found in php.ini
Edit…
vim /etc/php5/fpm/pool.d/www.conf Set…
request_terminate_timeout = 300 Changes in Nginx Config
To increase the time limit for example.com by
vim /etc/nginx/sites-available/example.com location ~ .php$ { include /etc/nginx/fastcgi_params; fastcgi_pass unix:/var/run/php5-fpm.sock; fastcgi_read_timeout 300; } If you want to increase time-limit for all-sites on your server, you can edit main nginx.conf file:
vim /etc/nginx/nginx.conf Add following in http{..} section
http { #... fastcgi_read_timeout 300; #... } Reload PHP-FPM & Nginx
Don’t forget to do this so that changes you have made will come into effect:
service php5-fpm reload service nginx reload 原來,php-fpm有一個參數 max_requests,該參數指明瞭,每一個children最多處理多少個請求後便會被關閉,默認的設置是500。由於php是把請求輪詢給每一個 children,在大流量下,每一個childre到達max_requests所用的時間都差很少,這樣就形成全部的children基本上在同一時間 被關閉。
在這期間,nginx沒法將php文件轉交給php-fpm處理,因此cpu會降至很低(不用處理php,更不用執行sql),而負載會升至很高(關閉和開啓children、nginx等待php-fpm),網卡流量也降至很低(nginx沒法生成數據傳輸給客戶端)
解決問題很簡單,增長children的數量,而且將 max_requests 設置未 0 或者一個比較大的值:
打開 /usr/local/php/etc/php-fpm.conf
而後重啓php-fpm。
2、增長緩衝區容量大小
將nginx的error log打開,發現「pstream sent too big header while reading response header from upstream」這樣的錯誤提示。查閱了一下資料,大意是nginx緩衝區有一個bug形成的,咱們網站的頁面消耗佔用緩衝區可能過大。參考老外寫的修 改辦法增長了緩衝區容量大小設置,502問題完全解決。後來系統管理員又對參數作了調整隻保留了2個設置參數:client head buffer,fastcgi buffer size。
3、request_terminate_timeout
若是主要是在一些post或者數據庫操做的時候出現502這種狀況,而不是在靜態頁面操做中常見,那麼能夠查看一下php-fpm.conf設置中的一項:
request_terminate_timeout
這個值是max_execution_time,就是fast-cgi的執行腳本時間。
0s
0s爲關閉,就是無限執行下去。(當時裝的時候沒仔細看就改了一個數字)
發現,問題解決了,執行很長時間也不會出錯了。
優化fastcgi中,還能夠改改這個值5s 看看效果。
php-cgi進程數不夠用、php執行時間長、或者是php-cgi進程死掉,都會出現502錯誤。
==============================================
我把以上的值300改爲1000秒去了