502 Bad Gateway

2019-07-28 01:24发布

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.

Error logs:

/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]

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

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].

Things I've tried so far:

  • 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;

  • 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.

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

    /etc/nginx/sites-available/default containing the line fastcgi_pass unix:127.0.0.1:9000; /etc/php5/fpm/pool.d/www.conf containing the line listen = 127.0.0.1:9000;

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

  • ensured I'm not disabling PHP files in my nginx.conf setup. I'm not, and sometimes it works, so that can't be it.

php5-fpm setup:

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

So: what else should I try? TIA.

标签: nginx fpm
1条回答
够拽才男人
2楼-- · 2019-07-28 01:48

You can check following things,

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

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- 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- switch off keepalive connections from nginx.conf

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

查看更多
登录 后发表回答