解决nginx中FastCGI的502 Bad Gateway错误

本博客一直运行正常,前段时间经常遇到502 Bad Gateway错误,web采用的是nginx,跑fastcgi,用php-fpm管理。当出现该问题,我采用重启php-cgi进程的方式(php-fpm restart),但总归不是长久之计,我通过检查排错,调整了配置文件(php-fpm.conf)中<value name="max_children">5</value>的值为10,而内存占用没有多大变化。保存配置文件并重启php-cgi,这样系统中的php-cgi进程为10个了。观察几天,再也没遇到502错误。注:因为本人主机是centos的vps主机,内存较小,故不配置更多的php-cgi进程。在实际生产环境中,你可根据硬件水平开启更多的进程!

恰好在我的运维群里孤竹朋友发了以下经验,故摘录。

1.FastCGI进程是否已经启动

2.FastCGI worker进程数是否不够
运行 netstat -anpo | grep "php-cgi" | wc -l 判断是否接近FastCGI进程,接近配置文件中设置的数值,表明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;
}
参见:http://www.ruby-forum.com/topic/169040

Tags: nginx, fastcgi, 502

上一篇: IIS5.1与6.0中Server Application Unavailable的解决方法
下一篇: 解决:cannot execute binary file

相关文章

发表评论