Nginx – Communication failure between nginx and PHP-FPM

502-errornginxphp-fpmvps

I've received my very first VPS just today, and wanted to install a nginx + PHP + MariaDB setup on it.
And, well, I actually did it, but I've got a problem configuring it.

Getting nginx to serve a static .html-file was no problem, trying to serve a dynamic PHP-file however just shows a public "502 Bad Gateway" message. My interpretation of the nginx logfile indicates a communication error between nginx and PHP-FPM, but I'm a bloody newbie, so that can be incredibly wrong.

php5-fpm.log:

[05-Aug-2012 21:50:10] NOTICE: fpm is running, pid 10982
[05-Aug-2012 21:50:10] NOTICE: ready to handle connections

part of nginx's error.log:

2012/08/06 00:13:20 [error] 11419#0: *4 connect() failed (111: Connection refused) while connecting to upstream, client: REMOVED, server: lo$
2012/08/06 00:23:21 [error] 11419#0: *9 connect() failed (111: Connection refused) while connecting to upstream, client: REMOVED, server: lo$
2012/08/06 00:23:22 [error] 11419#0: *9 connect() failed (111: Connection refused) while connecting to upstream, client: REMOVED, server: lo$
2012/08/06 00:23:23 [error] 11419#0: *9 connect() failed (111: Connection refused) while connecting to upstream, client: REMOVED, server: lo$

I've lookes through most configurations, but I left almost everything default in the first place. PHP-FPM runs still on port 9000, the one nginx is supposed to query…

Best Answer

This is a long shot (as I don't know your distribution, versions or config), but if I remember correctly recent default configurations of PHP-FPM (at least here on Debian) switched to file-based communication (UNIX sockets) instead of TCP.

You might verify that by inspecting PHP-FPM's config file, or using lsof, netstat -a or the likes.

Edit: Just checked with my setup. I'm using Apache here, so that won't help directly, but the line for FastCGI configuration reads:

FastCgiExternalServer /var/run/apache2/php5.fcgi -idle-timeout 600 -flush -socket /var/run/php5-fpm.sock

As you can see it's referring to a socket file, namely /var/run/php5-fpm.sock.