Warning: count(): Parameter must be an array or an object that implements Countable in /www/wwwroot/mdaeo.org/usr/plugins/AMP/Action.php on line 388
Nginx 502 Bad Gateway错误的终极解决方案 彻底解决lnmp出现的502错误

Mdaeo'Blog

Nginx 502 Bad Gateway错误的终极解决方案 彻底解决lnmp出现的502错误

使用Lnmp一键安装包部署Nginx环境,却发现经常出现 502 Bad Gateway 错误,比如在不停的刷新过程中,时不时就出现一次502错误;

经过本人摸索,终于发现了一个解决方法:

lnmp安装后,Fastcgi 默认的监听端口是这样的:fastcgi_pass  unix:/tmp/php-cgi.sock;

而随心微博之前没发生502错误的配置文件是:fastcgi_pass  127.0.0.1:9000;

换成监听9000端口后,再次狂刷新页面,没有出现;找一个外地朋友测试一下:刷新50+,没有出现502。而之前我是基本上刷新了八九次就会出现一次502错误;

所以,确定应该就是fastcgi监听端口的问题引起的;

具体步骤:

1.修改nginx.conf

nginx安装目录:/usr/local/nginx/conf/ ,打开nginx.conf,查找到:

fastcgi_pass  unix:/tmp/php-cgi.sock;

改为:

fastcgi_pass  127.0.0.1:9000;

2.修改php-fpm.conf

php安装目录:,/usr/local/php/etc/,打开php-fpm.conf

查找到第26行:     

 <value name="listen_address">/tmp/php-cgi.sock</value>

改为

      <value name="listen_address">127.0.0.1:9000</value>

3.依次重启Nginx和php-fpm

nginx 重启:
/usr/local/nginx/sbin/nginx -t
/usr/local/nginx/sbin/nginx -s reload

php-fpm重启:

/usr/local/php/sbin/php-fpm restart

okay,完成操作,现在试试是否还存在502错误了吧,如果还存在,可能就是fastcgi进程数不够,可适当根据自身服务器内存需要,修改cgi进程数目,在php-fpm.conf修改max_children最大数目,如我的服务器2G内存,修改为  <value name="max_children">72</value>。

 

可能还会修改:

      <value name="request_terminate_timeout">9999s</value>

      <value name="request_slowlog_timeout">999s</value>

当前页面是本站的「Google AMP」版。查看和发表评论请点击:完整版 »

因本文不是用Markdown格式的编辑器书写的,转换的页面可能不符合AMP标准。