PHP-FPM and Nginx: 502 Bad Gateway

PHP-FPM and Nginx: 502 Bad Gateway

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

PHP-FPM and Nginx: 502 Bad Gateway

When a request hits your web server and returns a 502 Bad Gateway, it means Nginx received an invalid response from an upstream server, which in this context is the PHP-FPM daemon. While there are thousands of Stack Overflow threads discussing this error, they often fail to address the specific state of the system where Nginx hangs during restarts or fails to connect entirely.

In many deployments, particularly on cloud instances like Amazon EC2 (such as a t1.micro running Ubuntu), the issue isn't just a missing line in a configuration file; it is frequently a fundamental mismatch between how the FastCGI socket is defined and how Nginx attempts to communicate with it.

The Socket vs. TCP Port Misconfiguration

A common pitfall in PHP-FPM setups involves the fastcgi_pass directive in your Nginx configuration. In your specific case, you have enabled port 9000 on the EC2 instance but are also attempting to use a Unix socket (unix:/var/run/php5-fpm.pid). This creates an immediate logical conflict.

The PHP-FPM daemon does not listen for incoming connections on TCP port 9000 when configured to use sockets; it listens exclusively at the file system path defined by the PID file location. Conversely, if you uncomment fastcgi_pass 127.0.0.1:9000, PHP-FPM must be explicitly configured to listen on a TCP socket rather than a Unix domain socket.

Mixing these two approaches leads to connection timeouts. Nginx waits for a response from the upstream, but because the protocol endpoints don't align (one expects a file path, the other a network port), the request times out, triggering the 502 error.

Resolving the Configuration Lockup

You mentioned that running service php5-fpm restart hangs indefinitely. This behavior often indicates that the master process is unable to fork child processes or bind to the required resources, potentially due to permission issues or conflicting PID file paths.

In your provided configuration, you have set daemonize = no. While this is useful for debugging via php-fpm.log, keeping FPM in the foreground on a production server can cause process management tools to behave unexpectedly. More critically, ensure that the PID file path specified in php5-fpm.conf matches the actual location where PHP-FPM creates its socket.

If you intend to stick with Unix sockets (the default and most efficient method for local communication), update your Nginx configuration to point strictly to the socket file:

location ~ \.php$ {
    root   /home/wayvac/public;
    fastcgi_pass   unix:/var/run/php5-fpm.sock;  
    fastcgi_index  index.php;
    include fastcgi_params;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}

Note the switch from /var/run/php5-fpm.pid to unix:/var/run/php5-fpm.sock. The .pid file contains the process ID and is not a listening socket. You must verify that the actual socket exists at /var/run/php5-fpm.sock after starting the service.

Process Manager Tuning

Your current php-fpm.conf includes commented-out pool definitions for start_servers, min_spare_servers, and max_spare_servers. These are critical for performance but must be placed inside a specific pool block, not under [global].

For a small instance like t1.micro, you might start with conservative values to prevent memory exhaustion:

[www]
; ... other settings ...
pm = dynamic
pm.max_children = 5
pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 3
pm.max_requests = 500

Ensure these values are inside a block named [www] (or your chosen pool name) within pool.d/, not at the top level of the main config file. Incorrect placement here can cause FPM to fail to initialize child processes, leading directly to connection refused errors.

Security and Path Validation

Your Nginx configuration includes complex regex logic for path manipulation:

if ($document_root2 ~ "^(.*\\\\).*?[\\\\|\/]\.\.\/(.*)$") { set $document_root2 $1$2; }

This pattern attempts to sanitize directory traversal attacks. While the intent is valid, overly complex regex in Nginx can sometimes cause performance degradation or unexpected matches if not carefully tested. Ensure that your application root (/home/wayvac/public) has the correct permissions for the www-data user to read and execute PHP scripts. If the web server user cannot access the script file, PHP-FPM will return a fatal error, which Nginx translates into a 502.

Finally, double-check that the include directives in your main nginx.conf are pointing to valid files. In your snippet, you have:

include /etc/nginx/sites-enabled/*;

Ensure that the file included here does not override the PHP location block or accidentally disable FastCGI processing for static assets.

Our network