Error 502 in nginx + php5-fpm: Decoding the Socket Timeout
When an Nginx server returns a 502 Bad Gateway error, the immediate instinct is often to check CPU load or memory usage. If those metrics look healthyâas they do in this specific case with ~15% CPU and ample RAMâthe culprit usually lies in the communication channel between the web server and the PHP FastCGI Process Manager (FPM).
The diagnostic clue here is definitive. The nginx/error.log explicitly states: connect() to unix:/var/run/php5-fpm.sock failed. This is not a timeout waiting for data; it is a connection refusal or a failure to establish the socket handshake entirely. While Nginx logs often show "upstream timed out," that error implies the PHP process accepted the connection but couldn't finish in time. A connect() failure means the PHP-FPM daemon did not even wake up to accept the request within Nginx's expectations.
The provided configuration highlights a common misconfiguration regarding resource allocation versus process management. The user has set pm.max_children = 1024 while keeping start_servers = 64 and min_spare_servers = 64. This creates a significant discrepancy between the active capacity and the initial pool size. In a dynamic mode setup, Nginx attempts to spawn processes up to the maximum limit based on incoming load. However, if the system is under heavy request volume (even if CPU isn't saturated yet), the socket queue can fill up faster than the pm manager can spawn new processes to handle them.
The logs show accepted conn: 41680 with only active processes: 1. This disparity suggests a bottleneck in the spawning rate or a resource lock preventing PHP-FPM from accepting more workers despite having high limits configured. Nginx is likely hitting its limit on how many connections it will hold in the queue before giving up, but since the error is connect() failed, the issue might be that the socket itself is closed or inaccessible by the new processes spawned.
A critical point to examine is the listen directive in the PHP-FPM pool configuration: listen = /var/run/php5-fpm.sock. The permissions are set to 0666 with owner/group www-data. While this allows broad access, it can sometimes lead to race conditions where Nginx connects to a socket that another process has just dropped, or permission issues prevent the new child processes from binding correctly if the parent dies unexpectedly.
Furthermore, increasing backlog parameters in both sysctl and php-fpm does not solve a connection failure. The listen.backlog parameter defines how many pending connections the PHP-FPM daemon will queue before rejecting them. If Nginx cannot establish the TCP/Unix socket connection initially, increasing the backlog size is irrelevant; the connection never reaches the queue.
The solution often lies in ensuring that pm.max_children is set realistically for the server's memory footprint, preventing the system from becoming unresponsive during startup or sudden spikes. Additionally, verifying that the Nginx user (www-data) matches the PHP-FPM user and group is essential to prevent permission-denied errors on the socket file. If Nginx cannot connect() to the socket, it often means the socket is missing entirely, which happens if PHP-FPM has crashed or failed to start its worker processes correctly due to memory exhaustion during the spawn phase.
For modern deployments, sticking to legacy versions like Debian 6 and PHP 5.3 introduces compatibility risks with newer Nginx modules that may have stricter socket handling behaviors. Migrating to a supported stack, such as the one used by Laravel frameworks which often leverage similar Nginx + FPM setups, ensures better stability and security patches are applied.
To resolve this immediately, check if PHP-FPM is actually running and listening on the expected socket path using ls -l /var/run/php5-fpm.sock. Ensure that no other process has taken ownership of the socket or that the directory permissions haven't changed. If the socket exists but connections fail, increasing pm.max_children might help, but only if the system has enough RAM to support that many child processes without swapping.