技术饭

nginx 502 bad gateway 解决 connect() failed (111: Connection refused) while connecting to upstream

copylian    0 评论    271 浏览    2020.11.10

nginx 502 bad gateway 解决 connect() failed (111: Connection refused) while connecting to upstream,新安装了centos8,配置了php7.2,还没怎么用,一直报错,烦都烦死。多方求证最后才知道是php的配置是:fastcgi_pass 127.0.0.1:9000; 但是php-fpm/www.conf里配置的监听listen不是这个,所以就报错了。

1105548-20180604100133086-1888188126.png

也不知道什么问题,就去看了一下nginx日志  /var/log/nginx/error.log,发现了这个错误

2020/11/09 23:39:06 [error] 75618#0: *49 connect() failed (111: Connection refused) while connecting to upstream, client: 58.23.32.158, server: a.piao-duoduo.com, request: "GET / HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "a.piao-duoduo.com"

2020/11/09 23:39:06 [error] 75618#0: *49 connect() failed (111: Connection refused) while connecting to upstream, client: 58.23.32.158, server: a.piao-duoduo.com, request: "GET /favicon.ico HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "a.piao-duoduo.com", referrer: "http://a.piao-duoduo.com/"

2020/11/09 23:39:08 [error] 75618#0: *49 connect() failed (111: Connection refused) while connecting to upstream, client: 58.23.32.158, server: a.piao-duoduo.com, request: "GET / HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "a.piao-duoduo.com"

2020/11/09 23:39:09 [error] 75618#0: *49 connect() failed (111: Connection refused) while connecting to upstream, client: 58.23.32.158, server: a.piao-duoduo.com, request: "GET /favicon.ico HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "a.piao-duoduo.com", referrer: "http://a.piao-duoduo.com/"

去搜了一下,这样的错误有两个解决方式

 1.php-fpm没有运行

执行如下命令查看是否启动了php-fpm,如果没有则启动你的php-fpm即可

netstat -ant | grep 9000

2.php-fpm队列满了

/etc/php-fpm.d/www.conf配置文件pm.max_children修改大一点,重启php-fpm并观察日志情况

3、修改配置文件

因为nginx和php有两种链接方式,一种是

fastcgi_pass 127.0.0.1:9000;

另一种是这个

fastcgi_pass unix:/run/php/php7.0-fpm.sock;

这个具体怎么用要去php fpm里面去看他的配置文件

/etc/php-fpm.d/www.conf里面的Listen

如果Listen是端口就写127.0.0.1:9000;

如果是路径,nginx的配置文件也要学路径,unix:/run/php/php7.0-fpm.sock;

;listen = /run/php-fpm/www.sock

listen = 127.0.0.1:9000

重新访问就好了

1604938111(1).png

所以说修改一下配置文件,在重启一下nginx,就好了。其实按理说不能发生这种事的,但是呢?自己搭的时候,几乎所有的事都有可能发生。记录下来,以防再次入坑。

飓风呀
感谢你的支持,我会继续努力!
扫码打赏,感谢您的支持!
nginx 502 refused upstream 

文明上网理性发言!

  • 还没有评论,沙发等你来抢