Heim  >  Artikel  >  Backend-Entwicklung  >  nginx+php-fpm出现502 bad gateway异常解决办法

nginx+php-fpm出现502 bad gateway异常解决办法

WBOY
WBOYOriginal
2016-06-13 12:18:181206Durchsuche

nginx+php-fpm出现502 bad gateway错误解决方法

1,nginx error 找不到位置 一般需要修改/usr/local/nginx/conf/nginx.conf(慎重修改 有可能引起其他网站问题)

2,502 error

情况1: nginx 挂掉:

命令:/usr/local/nginx/sbin/nginx-s reload

之后等待一会儿

情况2:php 挂掉:

首先需要修改/usr/local/php-5.6.0/etc/php-fpm.conf文件中的参数 具体修改哪个修改到多少要不断尝试

然后利用以下的命令中的一条或几条重启配置/usr/local/php-5.6.0/sbin/php-fpm reload

/usr/local/php-5.6.0/sbin/php-fpm

/usr/local/php-5.6.0/sbin/php-fpm -h

/usr/local/php-5.6.0/sbin/php-fpm–t

/usr/local/php-5.6.0/sbin/php-fpm–R

最后重启nginx 之后等待一会儿

如果上面方法不好用 那么可以尝试下面的

1.php-fpm进程数不够用

使用 netstat -napo |grep "php-fpm" | wc -l 查看一下当前fastcgi进程个数,如果个数接近conf里配置的上限,就需要调高进程数。

但也不能无休止调高,可以根据服务器内存情况,可以把php-fpm子进程数调到100或以上,在4G内存的服务器上200就可以。

2. 调高调高linux内核打开文件数量

可以使用这些命令(必须是root帐号)

echo 'ulimit -HSn 65536' >> /etc/profile

echo 'ulimit -HSn 65536' >> /etc/rc.local

source /etc/profile

 3.脚本执行时间超时

如果脚本因为某种原因长时间等待不返回 ,导致新来的请求不能得到处理,可以适当调小如下配置。

nginx.conf里面主要是如下

fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;

php-fpm.conf里如要是如下

request_terminate_timeout = 10s

4.缓存设置比较小

修改或增加配置到nginx.conf

proxy_buffer_size 64k;
proxy_buffers  512k;
proxy_busy_buffers_size 128k;

5. recv() failed (104: Connection reset by peer) while reading response header from upstream

可能的原因机房网络丢包或者机房有硬件防火墙禁止访问该域名

但最重要的是程序里要设置好超时,不要使用php-fpm的request_terminate_timeout,

最好设成request_terminate_timeout=0;

因为这个参数会直接杀掉php进程,然后重启php进程,这样前端nginx就会返回104: Connection reset by peer。这个过程是很慢,总体感觉就是网站很卡。

May 01 10:50:58.044162 [WARNING] [pool www] child 4074, script '/usr/local/nginx/html/quancha/sameip/detail.php' execution timed out (15.129933 sec), terminating
May 01 10:50:58.045725 [WARNING] [pool www] child 4074 exited on signal 15 SIGTERM after 90.227060 seconds from start
May 01 10:50:58.046818 [NOTICE] [pool www] child 4082 started

说一千道一万最重要的就是程序里控制好超时,gethostbyname、curl、file_get_contents等函数的都要设置超时时间。

另一个就是多说,这个东西是增加了网站的交互性,但是使用的多了反应就慢了,如果你网站超时且使用了多说是,可以关闭它。


Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn