phpnginxfpmhttp-status-code-502

502 Bad Gateway


I'm having a problem that, more often than not, if I go to a page on my PHP server, I get a "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:

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.


Solution

  • You can check the following things:

    1. sudo service php5-fpm status
      

      Try to start that if it's not running

    2. While changing socket to TCP/IP, remove unix. I.e. 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 the /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:

      pm.max_children = 40
      pm.start_servers = 10
      pm.min_spare_servers = 5
      pm.max_spare_servers = 10
      
    4. Switch off keepalive connections in nginx.conf

    5. Try to implement caching (mostly done for static content, so that all requests do not bother PHP) if possible.