Diagnosing Nginx and PHP-FPM Communication Failures
When you attempt to access your application and receive the generic "An error occurred" response from Nginx, it often points to a breakdown in the communication between your web server and your process manager. In this specific scenario involving Nginx 1.12.2 and PHP 7.1.16, the root cause is rarely the code itself but rather a mismatch in how the two services are configured to talk to each other.
The initial symptom is a 502 Bad Gateway error. The server successfully receives your request for index.php, but when it tries to forward that request to the PHP interpreter, the connection fails. A look at the Nginx error log reveals the specific failure mode: connect() failed (111: Connection refused).
This message indicates that Nginx is attempting to reach a socket or port that does not exist from its perspective. The configuration provided shows Nginx configured to use TCP/IP networking:
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
# ...
}
Meanwhile, the PHP-FPM configuration defines a Unix domain socket:
[www]
listen = /tmp/php-cgi.sock
# ...
Here lies the fundamental incompatibility. Nginx is trying to open a TCP connection to 127.0.0.1 on port 9000, expecting PHP-FPM to be listening there. However, PHP-FPM is waiting for connections at /tmp/php-cgi.sock. Because the file systems and network stacks are different, Nginx gets a "Connection refused" immediately because there is no process bound to that TCP address.
The fix involves aligning the fastcgi_pass directive in Nginx with the listen directive in PHP-FPM. You must switch Nginx from the TCP socket to the Unix socket:
location ~ \.php$ {
root html;
fastcgi_pass unix:/tmp/php-cgi.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
Once this change is made, you must reload Nginx for the configuration to take effect. If you use systemctl, run:
sudo systemctl reload nginx
However, switching to Unix sockets introduces a new set of constraints regarding file permissions. The error log in your scenario confirms that PHP-FPM processes are running as the user and group www. Consequently, the socket file itself must be readable and writable by this user. If the socket is owned by root or has restrictive permissions like 0600, Nginx (often running as nginx or www-data) will fail to connect with a "Permission denied" error.
You should verify the ownership and permissions of the socket:
ls -l /tmp/php-cgi.sock
# Output example: srwxrwxrwx 1 www www ... /tmp/php-cgi.sock
If the permissions do not match the users defined in your php-fpm.conf, you will need to adjust them. The original configuration shows listen.mode = 0666 and listen.owner = www, which should be sufficient provided Nginx runs under the same user context or has appropriate access rights.
A common pitfall when troubleshooting these errors is assuming that because PHP-FPM is running (ps -ef | grep php-fpm returns processes), the interface is ready. The process might be listening on a socket that Nginx cannot see due to network vs. filesystem mismatches, or the socket path might differ slightly (e.g., /var/run/php/php7.1.16.sock vs /tmp/php-cgi.sock). Always verify the exact path defined in php-fpm.conf against the fastcgi_pass directive.
Additionally, ensure that your SCRIPT_FILENAME parameter is constructed correctly. In your original config, it was hardcoded to /scripts$fastcgi_script_name, which would likely cause a 404 error for files located directly in html. It should typically reference $document_root$fastcgi_script_name or explicitly define the root path relative to where Nginx serves static files.
By ensuring that the communication protocol (Unix socket) matches and that the underlying file permissions allow cross-process access, you restore the bridge between your web server and your PHP interpreter.