502 Bad Gateway

A gateway failed to obtain a valid response from the upstream service it contacted.

All HTTP status codes

What does HTTP 502 mean?

A page can reach the reverse proxy successfully while the next hop fails. A 502 directs attention to that gateway-to-upstream exchange: a reset connection, malformed response or protocol mismatch may be involved. Establish which gateway generated it before restarting services. An error page branded by your host can still be reporting a fault in the application's upstream connection.

An investigation example

Illustrative deployment investigation
Public result: 502 Bad Gateway
Proxy error: upstream connection reset while reading headers
Application workers restarted during every request.
Action: correct the crashing release and restore stable workers.
Verify: requests complete through the public proxy and resets stop recurring.

Common causes

  • The application process or PHP-FPM pool is stopped, restarting or crashing before it can complete a response.
  • The proxy points to an old socket, wrong port or backend address after a deployment.
  • A backend closes the connection partway through a response or sends invalid or oversized response headers.
  • TLS, name resolution or network access between the proxy and its upstream prevents a usable exchange.
  • A rolling deployment removes healthy workers before replacement instances are ready to receive traffic.

Diagnose the response

Find the gateway's upstream error and correlate it with the application's process, connection and deployment logs.

  • Start with the proxy's error log at the failure timestamp. Distinguish connection refusal, an early connection close, invalid response headers and TLS failure; each points to a different repair.
  • Confirm that the upstream process is running and listening at the socket or address the proxy actually uses. Review process crashes and deployment events rather than repeatedly restarting a service without understanding why it exits.
  • From an authorised server environment, test the upstream using the expected hostname, protocol and port. Preserve Host and TLS server-name handling so the diagnostic reaches the same virtual host as real traffic.
  • Check header size and format if the log reports an invalid response. Oversized cookies or application-generated headers can trigger a proxy failure even when the application's own status would have been 200.
  • Validate configuration before reloading a proxy and restore healthy capacity before sending traffic back. Recheck the public URL as well as the backend so that the complete visitor path is verified.

What it means for website monitoring

Compare Sitepulse's public result with gateway and application timestamps. Verify sustained responses after recovery; a single successful retry can miss an intermittent upstream failure.

Reference