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

  1. Confirm whether the 502 affects all PHP routes or only a subset tied to one application or pool.
  2. Review Nginx and PHP-FPM logs together so you can see both the failed upstream connection and the backend state behind it.
  3. Verify Nginx is pointing at the correct socket path or TCP port for the active PHP-FPM service.
  4. Check whether PHP-FPM is running, stable, and accepting connections without crashing or restarting repeatedly.
  5. Inspect socket ownership, permissions, and service user configuration if Nginx cannot open the backend socket.
  6. Review pool capacity, slow requests, and timeouts to determine whether workers are saturated rather than offline.
  7. Correct the specific path, permission, or pool issue and reload both services in the right order.
  8. Retest dynamic routes under representative load so the fix survives beyond a single restart.
  9. Keep Nginx upstream targets and PHP-FPM pool configuration aligned during upgrades and deploy automation.