Diagnosing nginx 502 Bad Gateway with spawn-fcgi
A 502 Bad Gateway error from nginx is a generic upstream communication failure, but it rarely points to a single monolithic cause. When you are running PHP via spawn-fcgi and hitting this error immediately after a server boot or restart, the most probable culprit is that the FastCGI process has terminated before nginx attempted to send a request to it.
In your specific setup, you are launching php5-cgi as a daemon using /usr/bin/spawn-fcgi. The command binds to 127.0.0.1 on port 9000, running under the www-data user, with process management handled by a PID file at /var/run/fastcgi-php.pid. While this approach works for many legacy environments, it introduces a race condition that is common in automated boot sequences defined in rc.local.
The core issue lies in the timing of the startup script versus the nginx worker processes. When the system boots, rc.local executes your spawn-fcgi command. This process starts the PHP daemon and immediately exits, writing its PID to the file you specified. However, nginx workers may also be initializing at roughly the same time. If an incoming HTTP request arrives before the PHP daemon has fully initialized its internal state or established its listening socket on port 9000, nginx receives a connection refusal or timeout. Since the upstream is effectively unavailable, nginx returns a 502 error.
You mentioned seeing no logs. This is actually consistent with how spawn-fcgi behaves in this context. If the PHP process dies immediately after startingâperhaps due to missing environment variables like PATH, incorrect permissions on the CGI script directory, or an inability to load specific extensionsâit will terminate without writing a standard error log that nginx can easily parse for immediate feedback. The absence of logs often suggests the process never entered a stable "listening" state long enough to generate an error message before crashing.
To resolve this, you should verify the environment in which spawn-fcgi runs. spawn-fcgi does not inherit all environment variables from your shell session; it requires an explicit -E /etc/environment flag or manual definition of necessary variables like PATH and LD_LIBRARY_PATH. Without these, the PHP interpreter may fail to locate its own binaries or shared libraries shortly after spawning, causing an immediate exit.
Additionally, check the permissions on /var/run/fastcgi-php.pid. If nginx cannot read this file to verify the process state, or if the www-data user lacks write permissions to the directory where the PID is stored, inconsistencies can arise. Ensure that the directory /var/run/ (or wherever you place the PID) has the correct ownership and mode, typically owned by root with writable access for the target user.
If you find that the PHP process is indeed dying silently, inspecting the actual output of the spawned process is crucial. You can redirect the standard error of the spawn-fcgi command to a log file during development:
/usr/bin/spawn-fcgi -a 127.0.0.1 -p 9000 -u www-data -g www-data \
-f /usr/bin/php5-cgi -P /var/run/fastcgi-php.pid -e /var/log/php-fcgi-error.log
By capturing the stderr, you will see if the PHP interpreter is crashing upon startup. If the process is stable but nginx still times out, consider increasing the fastcgi_read_timeout and fastcgi_connect_timeout directives in your nginx configuration to accommodate the brief initialization window of the daemon. However, fixing the race condition by ensuring the PHP process is fully ready before accepting requests remains the most robust solution.
