日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > php >内容正文

php

Nginx + PHP(php-fpm)遇到的502 Bad Gateway错误

發(fā)布時(shí)間:2023/12/20 php 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Nginx + PHP(php-fpm)遇到的502 Bad Gateway错误 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

我一個(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.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.

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有一個(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)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。