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:
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 { ... } block:
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;This made none of the PHP pages work, so 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.
You can check the following things:
sudo service php5-fpm status
Try to start that if it's not running
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
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
Switch off keepalive connections in nginx.conf
Try to implement caching (mostly done for static content, so that all requests do not bother PHP) if possible.