ENGINEERING JOURNAL

Nginx 502 Bad Gateway with PHP-FPM: The Usual Suspects

nginx 502 bad gateway from PHP-FPM means PHP never answered. Ordered checklist: error log, FPM status, socket path, pool saturation, timeouts, permissions.

THE ARTICLE 01 — 02

A 502 Bad Gateway from nginx means exactly one thing: nginx asked an upstream server for the response and got something it could not use. When the upstream is PHP-FPM, that translates to something concrete: PHP-FPM never answered, answered too slowly, or died in the middle of the answer. The trick is to read what nginx wrote down. This is the ordered checklist I work through on a typical Ubuntu server with nginx and PHP-FPM (the same stack I ran on EC2), from most common to least.

1. Start at the nginx error log, not the browser

Before touching any config, open the log. The 502 is never a mystery if you read the line that caused it:

sudo tail -n 50 /var/log/nginx/error.log

Look for lines with connect() to unix:/run/php/php8.2-fpm.sock failed. If the reason in parentheses is 2: No such file or directory, the socket is missing. If it is 13: Permission denied, the socket exists but nginx cannot talk to it. If you see upstream timed out, PHP-FPM was reachable but too slow. Each of those points at a different suspect, which is why this step comes first.

2. PHP-FPM is not running

The most common cause I have seen after a deploy or a reboot: FPM is simply down, and nothing is listening on the socket. Check it directly:

sudo systemctl status php8.2-fpm

If it is inactive or failed, restart it:

sudo systemctl restart php8.2-fpm

If it fails to start, the log tells you why:

sudo tail -n 50 /var/log/php8.2-fpm.log

Often it is a typo in a pool file that crept in during the last deploy. Validate the FPM config before restarting, the same way you validate nginx config:

sudo php-fpm8.2 -t
sudo nginx -t

3. nginx points at the wrong socket

In your nginx site config there is a line like this:

fastcgi_pass unix:/run/php/php8.2-fpm.sock;

And in the PHP-FPM pool config (/etc/php/8.2/fpm/pool.d/www.conf) there is a matching line:

listen = /run/php/php8.2-fpm.sock

These two must agree. The classic way they drift apart is a PHP version upgrade: you install PHP 8.2, the pool now listens on php8.2-fpm.sock, but the nginx config still points at php8.1-fpm.sock. Every request then 502s because nginx knocks on a door that no longer exists. Confirm what the pool actually listens on:

grep -n "^listen" /etc/php/8.2/fpm/pool.d/www.conf

Then compare it with fastcgi_pass in /etc/nginx/sites-enabled/. After fixing, reload (not restart) nginx so you do not drop healthy connections:

sudo nginx -t && sudo systemctl reload nginx

4. The FPM pool is exhausted

If FPM is running and the socket is right, the next suspect is saturation. Each pool has a fixed number of worker processes (pm.max_children). When traffic or a few slow scripts use them all up, new requests queue. If the queue fills too, FPM stops accepting and nginx reports 502. The FPM log gives it away:

WARNING: [pool www] server reached max_children setting (20), consider raising it

Raising max_children is the obvious fix, but only if the machine has the RAM. A rough estimate: check an average worker size with ps, then make sure max_children * average_size leaves headroom for the OS and the database client. On a small EC2 instance, overshooting this turns a 502 problem into an out-of-memory killer problem, which is worse.

5. A slow script trips one of three timeouts

When the log says upstream timed out, some request took longer than allowed. There are three separate timers, and confusing them wastes time:

  • max_execution_time in php.ini: how long PHP itself allows a request to run.
  • request_terminate_timeout in the pool config: how long FPM waits before killing a worker that is still running.
  • fastcgi_read_timeout in the nginx config: how long nginx waits for FPM to send a response.

Set them deliberately, from the inside out: request_terminate_timeout slightly above max_execution_time, and fastcgi_read_timeout above both. If nginx gives up at 60 seconds but PHP is allowed 120, you get 502s on jobs that were actually going to finish.

6. Socket permissions block nginx

If the socket exists and FPM is running, but the nginx log shows connect() failed (13: Permission denied), the socket file permissions are wrong. nginx runs as one user (usually www-data) and the FPM pool creates the socket as another. In www.conf, these lines control it:

listen.owner = www-data
listen.group = www-data
listen.mode = 0660

A common way this breaks: someone hardens the pool file, sets listen.mode = 0600 without setting owner and group, and only root can open the socket. After changing these, restart FPM (not reload) so the socket file is recreated with the new ownership.

The order of operations

When I hit a 502, I run the same sequence every time: read the nginx error log, confirm FPM is running, verify the socket path matches between nginx and the pool, check whether the pool is saturated, look for timeout mismatches, and finally check socket permissions. For the bigger picture of how I approach any production failure, see how I triage a production incident. A 502 is nginx telling you exactly where to look: read what it wrote, work the list in order, and resist the urge to restart everything blindly. A blind restart sometimes clears the symptom for an hour and teaches you nothing about the cause.

Muhammad Aqib ← ALL ARTICLES
KEEP EXPLORING

More ideas.
Better engineering.

Explore the journal ↗