A 502 Bad Gateway means the web server tried to proxy the request to PHP-FPM and got back something it couldn’t use — usually nothing at all, a half-broken response, or a connection that hung up early. On a WordPress site that’s almost always PHP-FPM dying or refusing the connection, not WordPress itself. The fix lives below the application.
What’s happening
Nginx returns 502 when it can’t talk to its upstream — in WordPress terms, when the request to php-fpm.sock or 127.0.0.1:9000 fails. The failure modes are: FPM isn’t running, FPM is running but has no free children, the worker that picked up the request crashed before responding, FPM’s request_terminate_timeout killed the worker mid-execution, or the socket file has wrong permissions so Nginx can’t open it. Apache can also return 502 when in proxy mode, but more often returns 500 in the same scenario.
A 502 that comes and goes — works on retry, fails again ten requests later — almost always means pm.max_children is too low for the traffic. A 502 that’s constant from the moment of a deploy usually means FPM isn’t running at all, or the socket path in Nginx doesn’t match where FPM is actually listening.
How to verify
sudo systemctl status php8.2-fpm
sudo journalctl -u php8.2-fpm -n 50
tail -f /var/log/nginx/error.log
tail -f /var/log/php8.2-fpm.log
ls -la /run/php/ # check socket exists and permissions
curl --unix-socket /run/php/php8.2-fpm.sock http://example.com/ -I
ss -tlnp | grep 9000 # if listening on TCP
If nginx error.log says connect() failed (111: Connection refused), FPM isn’t running or isn’t listening where Nginx expects. If it says connect() failed (13: Permission denied), the socket permissions are wrong. If you see upstream prematurely closed connection, FPM is starting to respond and then dying mid-response.
The fix
- Confirm FPM is running.
systemctl status php8.2-fpm— if it’s not, start it and read the journal for why it died. Common causes: a syntax error in a pool config, a missing socket directory after a deploy. - Match the socket path. Nginx
fastcgi_pass unix:/run/php/php8.2-fpm.sock;must match thelisten =line in the FPM pool config. After a PHP version bump, the socket path changes — Nginx still points at the old one. - Raise
pm.max_children. Ifpm.status_pathshowsactive processesequal tomax childrenconsistently, FPM is saturating. Raise it, but watch RAM — each worker holds 50-200MB. - Look for OOM kills.
dmesg | grep -i oomandjournalctl -k | grep -i kill. If the kernel is killing FPM workers, the server is over-provisioned for memory. - Tighten or relax
request_terminate_timeout. If long-running requests are getting killed, raise it. If runaway requests are exhausting workers, lower it and find the slow request. - Check for segfaults.
journalctl -k | grep -i segfaultwill show if a PHP extension is crashing workers. Most often: an out-of-dateopcacheextension after a PHP point release, or a binary plugin module compiled for the wrong PHP version.
For sites where 502s are intermittent and you need real-time FPM saturation alerts, Stack Harbor wires that into enterprise WordPress operations observability. To diagnose a current 502, book a consult.