我一個統計程序估計要跑1分多鐘以上
查看了一個php-fpm 配置文件
[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
很明顯了
部分PHP程序的執行時間超過了Nginx的等待時間,可以適當增加nginx.conf配置文件中FastCGI的timeout時間
google了一較以後
http://rtcamp.com/wordpress-nginx/tutorials/php/increase-script-execution-time/
If you want to change max execution time limit for php scripts from 30 seconds (default) to 300 seconds.
vim /etc/php5/fpm/php.ini
Set…
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.
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
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; #... }
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。
二、增加緩沖區容量大小
將nginx的error log打開,發現“pstream sent too big header while reading response header from upstream”這樣的錯誤提示。查閱了一下資料,大意是nginx緩沖區有一個bug造成的,我們網站的頁面消耗占用緩沖區可能過大。參考老外寫的修 改辦法增加了緩沖區容量大小設置,502問題徹底解決。後來系統管理員又對參數做了調整只保留了2個設置參數:client head buffer,fastcgi buffer size。
三、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秒去了