Nginx + PHP(php-fpm)遇到的502 Bad Gateway错误
我一個(gè)統(tǒng)計(jì)程序估計(jì)要跑1分多鐘以上
查看了一個(gè)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程序的執(zhí)行時(shí)間超過了Nginx的等待時(shí)間,可以適當(dāng)增加nginx.conf配置文件中FastCGI的timeout時(shí)間
google了一較以后
http://rtcamp.com/wordpress-nginx/tutorials/php/increase-script-execution-time/?
Changes in php.ini
If you want to change max execution time limit for php scripts from 30 seconds (default) to 300 seconds.
vim /etc/php5/fpm/php.iniSet…
max_execution_time = 300In 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.confSet…
request_terminate_timeout = 300Changes 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.confAdd 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有一個(gè)參數(shù) max_requests,該參數(shù)指明了,每個(gè)children最多處理多少個(gè)請求后便會被關(guān)閉,默認(rèn)的設(shè)置是500。因?yàn)閜hp是把請求輪詢給每個(gè) children,在大流量下,每個(gè)childre到達(dá)max_requests所用的時(shí)間都差不多,這樣就造成所有的children基本上在同一時(shí)間 被關(guān)閉。
在這期間,nginx無法將php文件轉(zhuǎn)交給php-fpm處理,所以cpu會降至很低(不用處理php,更不用執(zhí)行sql),而負(fù)載會升至很高(關(guān)閉和開啟children、nginx等待php-fpm),網(wǎng)卡流量也降至很低(nginx無法生成數(shù)據(jù)傳輸給客戶端)
解決問題很簡單,增加children的數(shù)量,并且將 max_requests 設(shè)置未 0 或者一個(gè)比較大的值:
打開 /usr/local/php/etc/php-fpm.conf
然后重啟php-fpm。
二、增加緩沖區(qū)容量大小
將nginx的error log打開,發(fā)現(xiàn)“pstream sent too big header while reading response header from upstream”這樣的錯(cuò)誤提示。查閱了一下資料,大意是nginx緩沖區(qū)有一個(gè)bug造成的,我們網(wǎng)站的頁面消耗占用緩沖區(qū)可能過大。參考老外寫的修 改辦法增加了緩沖區(qū)容量大小設(shè)置,502問題徹底解決。后來系統(tǒng)管理員又對參數(shù)做了調(diào)整只保留了2個(gè)設(shè)置參數(shù):client head buffer,fastcgi buffer size。
三、request_terminate_timeout
如果主要是在一些post或者數(shù)據(jù)庫操作的時(shí)候出現(xiàn)502這種情況,而不是在靜態(tài)頁面操作中常見,那么可以查看一下php-fpm.conf設(shè)置中的一項(xiàng):
request_terminate_timeout
這個(gè)值是max_execution_time,就是fast-cgi的執(zhí)行腳本時(shí)間。
0s
0s為關(guān)閉,就是無限執(zhí)行下去。(當(dāng)時(shí)裝的時(shí)候沒仔細(xì)看就改了一個(gè)數(shù)字)
發(fā)現(xiàn),問題解決了,執(zhí)行很長時(shí)間也不會出錯(cuò)了。
優(yōu)化fastcgi中,還可以改改這個(gè)值5s 看看效果。
php-cgi進(jìn)程數(shù)不夠用、php執(zhí)行時(shí)間長、或者是php-cgi進(jìn)程死掉,都會出現(xiàn)502錯(cuò)誤。
==============================================
我把以上的值300改成1000秒去了
轉(zhuǎn)載于:https://www.cnblogs.com/jackluo/p/3366612.html
總結(jié)
以上是生活随笔為你收集整理的Nginx + PHP(php-fpm)遇到的502 Bad Gateway错误的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 安卓开发界面代码
- 下一篇: 最全的PHP后台管理系统源码