Nginx php-fpm fast-cgi 502 Bad Gateway错误处理思路与方法

[ 2010-12-12 21:18:21 | 作者: admin ]
字号: | |
Nginx 502 Bad Gateway的含义是请求的PHP-CGI已经执行,但是由于某种原因(一般是读取资源的问题)没有执行完毕而导致PHP-CGI进程终止,一般来说Nginx 502 Bad Gateway和php-fpm.conf的设置有关。


常见的原因可能是php-cgi进程数不够用、php执行时间长(mysql慢)、或者是php-cgi进程死掉,都会出现502错误。

1. 在安装好的环境中,运行一段时间出现502问题,一般是因为默认php-cgi进程是5个,可能因为phpcgi进程不够用而造成502,需要修改/usr/local/php/etc/php-fpm.conf 将其中的max_children值适当增加。

2. php执行超时,修改/usr/local/php/etc/php.ini 将max_execution_time 改为300

3. 磁盘空间不足,可以使用 # df -h命令查看磁盘使用量

4. php-cgi进程死掉了。



通常的排查方法如下:

1、查看php fastcgi的进程数(max_children值)
# netstat -anop | grep php-cgi | wc -l
# netstat -anpo | grep php-fpm | wc -l

假如显示是5

2、查看当前进程
# ps aux | grep php-fpm 观察fastcgi/php-fpm进程数,假如使用的进程数等于或高于5个,说明需要增加。

3、调整/usr/local/php/etc/php-fpm.conf 的相关设置
pm.max_children = 5
request_terminate_timeout = 60



max_children最多5个进程,按照每个进程20MB内存,最多100MB。也就是1分钟。max_children增多,则php-cgi的进程多了就会处理的很快,排队的请求就会很少。 但是设置max_children也需要根据服务器的性能进行设定,一般来说一台服务器正常情况下每一个php-cgi所耗费的内存在20M左右。根据自己服务器购买的内存来实际决定。
request_terminate_timeout执行的时间为60秒,request_terminate_timeout值可以根据服务器的性能进行设定。一般来说性能越好你可以设置越高,20分钟-30分钟都可以。

4. 部分PHP程序的执行时间超过了Nginx的等待时间,可以适当增加nginx.conf配置文件中FastCGI的timeout时间,例如:
http
{
......
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
......
}

attachments/201810/30_092246_tb1aa1jjfxxxxahxxxxxxxxxxxx.jpg




***************************************************
Nginx php-fpm fast-cgi 502 Bad Gateway错误处理
***************************************************

Nginx php-fpm fast-cgi 502 Bad Gateway不正确 是FastCGI有疑问 ,造成NGINX 502不正确 的可能性比较多。
将网上找到的一些和502 Bad Gateway不正确 有关的疑问 和排查要领 列一下,先从FastCGI配置入手:

1.FastCGI进程能不能 已经启动

2.FastCGI worker进程数能不能 不够
通过命令查看服务器上一共开了多少的 php-cgi 进程
ps -fe |grep "php" | wc -l

运用 如下命令查看已经有多少个php-cgi进程用来处理tcp请求
 
 
netstat -anpo | grep "php-cgi" | wc -l
  

接近配置文件中配置的数值(php-fpm.conf里的max_children值),表明worker进程数配置 太少
参见:http://blog.s135.com/post/361.htm

3.FastCGI执行时间过长
根据实际情况调高以下参数值
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;

4.FastCGI Buffer不够
nginx和apache一样,有前端缓冲限定 ,可以调整缓冲参数
fastcgi_buffer_size 32k;
fastcgi_buffers 8 32k;
参见:http://www.hiadmin.com/nginx-502-gateway-error%E4%B8%80%E4%BE%8B/

5.Proxy Buffer不够
如果你用了Proxying,调整
proxy_buffer_size 16k;
proxy_buffers 4 16k;
参见:http://www.ruby-forum.com/topic/169040

6.https转发配置不正确
正确的配置要领

server_name www.mydomain.com;
location /myproj/repos {
set $fixed_destination $http_destination;
if ( $http_destination ~* ^https(.*)$ )
{
set $fixed_destination http$1;
}
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Destination $fixed_destination;
proxy_pass http://subversion_hosts;
[最后修改由 admin, 于 2018-10-30 09:23:30]
评论Feed 评论Feed: http://blog.xg98.com/feed.asp?q=comment&id=1599

这篇日志没有评论。

此日志不可发表评论。