502错误的网关 [英] 502 Bad Gateway

查看:304
本文介绍了502错误的网关的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我经常遇到的问题是,如果我转到服务器上使用php的页面,则会收到"502 Bad Gateway"错误.

I'm having the problem that more often than not, if I go to a page on my server that uses php, I get the "502 Bad Gateway" error.

错误日志:

/var/log/nginx/error.log每分钟显示大约3个该错误的副本:

/var/log/nginx/error.log shows about 3 copies of this error per minute:

2016/08/27 15:07:22 [error] 17309#0: *53554 connect() to unix:/var/run/php5-fpm.sock 
failed (11: Resource temporarily unavailable) while connecting to upstream, client: 
[dedicated server], server: localhost, request: "POST /xmlrpc.php HTTP/1.0", 
upstream: "fastcgi://unix:/var/run/php5-fpm.sock:", host: [my IP address]

nginxphp5-fpm进程的CPU负载有时很巨大(超过100%),但有时却很明显(2%),很少那么微不足道.

The CPU load from nginx or php5-fpm processes is sometimes huge (in excess of 100%), but sometimes just noticeable (2%), rarely insignificant.

syslog(!)中,我看到了很多东西:

Here's something I see a lot of in syslog (!):

Aug 27 15:17:21 [site] avahi-daemon[871]: Invalid response packet from host 
[some IP address that isn't mine and nslookup never heard of].

到目前为止我已经尝试过的事情:

  • apt-get update
  • 已删除并重新安装php5php5-cgiphp5-fpm
  • 确保apache2不在我的系统上运行
  • 已添加nginx.conf,在http { ... }中:

  • apt-get update
  • removed and reinstalled php5, php5-cgi, and php5-fpm
  • made sure apache2 is not running on my system
  • added this to nginx.conf, inside a http { ... }:

fastcgi_buffers 8 16k; fastcgi_buffer_size 32k; fastcgi_connect_timeout 300; fastcgi_send_timeout 300; fastcgi_read_timeout 300;

fastcgi_buffers 8 16k; fastcgi_buffer_size 32k; fastcgi_connect_timeout 300; fastcgi_send_timeout 300; fastcgi_read_timeout 300;

确保运行nginx的同一用户拥有/var/run/php5-fpm.sock.这是与/etc/php5/fpm/pool.d/www.conf中的listen.ownerlisten.group引用的所有者相同.

ensured that the same user that runs nginx, owns /var/run/php5-fpm.sock. This is the same owner referenced as listen.owner and listen.group in /etc/php5/fpm/pool.d/www.conf.

试图将对该套接字的引用更改为TCP/IP套接字:

tried changing the references to that socket to a TCP/IP socket:

/etc/nginx/sites-available/default包含行fastcgi_pass unix:127.0.0.1:9000; /etc/php5/fpm/pool.d/www.conf包含行listen = 127.0.0.1:9000;

由于这使所有php页面均无法正常工作,因此我恢复了该更改.

Since this made none of the php pages ever work, I reverted that change.

php5-fpm设置:

我怀疑这是php5占用内存或CPU时间的问题,因为(a)经常会这样,并且(b)如果我没有收到504错误,那么在使用php的任何页面上加载时间都非常慢.我认为这是/etc/php5/fpm/pool.d/www.conf文件的相关部分:

I suspect it's a problem with php5 eating up memory or CPU time since (a) it often does and (b) if I don't get that 504 error, I get a very slow load time on any page using php. Here's what I think is the relevant part of the /etc/php5/fpm/pool.d/www.conf file:

pm = dynamic
pm.max_children = 5
pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 3

所以:我还应该尝试什么? TIA.

So: what else should I try? TIA.

推荐答案

您可以检查以下内容,

1- sudo service php5-fpm status,如果没有运行,请尝试启动

1- sudo service php5-fpm status, try to start that if no running

2-将套接字更改为tcp/ip时,删除unix,即在/etc/nginx/sites-available/default文件中,在php块中将fastcgi_pass unix:/var/run/php5-fpm.sock;更改为fastcgi_pass 127.0.0.1:9000;,在/etc/php5/fpm/pool.d/www.conf文件中将listen = /var/run/php5-fpm.sock更改为listen = 127.0.0.1:9000

2- while changing socket to tcp/ip remove unix, ie, in the /etc/nginx/sites-available/default file, in the php block change fastcgi_pass unix:/var/run/php5-fpm.sock; to fastcgi_pass 127.0.0.1:9000; and in /etc/php5/fpm/pool.d/www.conf file change listen = /var/run/php5-fpm.sock to listen = 127.0.0.1:9000

3-尝试增加php的进程管理器,即pm.max_children=40pm.start_servers = 10pm.min_spare_servers = 5pm.max_spare_servers = 10

3- try to increase the process managers for php, i.e., pm.max_children=40, pm.start_servers = 10, pm.min_spare_servers = 5, pm.max_spare_servers = 10

4-关闭nginx.conf的keepalive连接

4- switch off keepalive connections from nginx.conf

5-尝试实施缓存(已针对几乎静态的内容完成,因此所有请求都不会打扰php).

5- try to implement caching (is done for almost static content, so that all requests do not bother php) if possible.

这篇关于502错误的网关的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆