Introduction
An Nginx 502 to PHP-FPM means the web server accepted the request but could not get a valid response from the PHP backend. That can come from a dead socket, crashed pool, permission mismatch, or workers that are too busy to accept another request. The fastest fix is to determine whether Nginx cannot reach PHP-FPM at all or can reach it but not within the time and capacity limits configured.
Symptoms
- Pages backed by PHP return 502 Bad Gateway while static files still load
- The issue appears after deploys, PHP upgrades, or traffic spikes
- Nginx error logs mention upstream, connect, socket, or FastCGI failures
- The outage affects dynamic routes more than the whole host
- Restarts help briefly before the error returns
Common Causes
- PHP-FPM is stopped, crashing, or listening on a different socket or port than Nginx expects
- Socket permissions prevent Nginx from connecting to the PHP-FPM pool
- The PHP-FPM pool is exhausted by slow requests or too few workers
- FastCGI or upstream timeouts are too low for the current workload
- A deployment changed PHP versions, service names, or pool configuration paths
Step-by-Step Fix
- Confirm whether the 502 affects all PHP routes or only a subset tied to one application or pool.
- Review Nginx and PHP-FPM logs together so you can see both the failed upstream connection and the backend state behind it.
- Verify Nginx is pointing at the correct socket path or TCP port for the active PHP-FPM service.
- Check whether PHP-FPM is running, stable, and accepting connections without crashing or restarting repeatedly.
- Inspect socket ownership, permissions, and service user configuration if Nginx cannot open the backend socket.
- Review pool capacity, slow requests, and timeouts to determine whether workers are saturated rather than offline.
- Correct the specific path, permission, or pool issue and reload both services in the right order.
- Retest dynamic routes under representative load so the fix survives beyond a single restart.
- Keep Nginx upstream targets and PHP-FPM pool configuration aligned during upgrades and deploy automation.