502 vs 504 Errors: How to Troubleshoot Reverse Proxy Failures
A website may display a 502 or 504 error even though its domain, DNS, and front-end web server are working. Both responses usually indicate trouble between a gateway or reverse proxy and an upstream service. Understanding the difference helps you investigate the correct layer instead of repeatedly restarting everything.
What a 502 Bad Gateway Usually Means
A 502 Bad Gateway response means the proxy could not obtain a valid response from the upstream server. The backend may be stopped, listening on a different address, refusing connections, closing the connection unexpectedly, or returning data the proxy cannot interpret.
Start by confirming the upstream address and port in the proxy configuration. Then test the backend directly from the proxy host with a local request. Check whether the application service is running and whether a firewall, Unix socket permission, container network, or TLS setting blocks the connection. Our overview of HTTP status codes explains where gateway errors fit within the wider 5xx class.
What a 504 Gateway Timeout Usually Means
A 504 Gateway Timeout generally means the proxy reached the upstream path but did not receive the required response within the allowed time. The application may be processing a slow database query, waiting for another API, locked on a resource, overloaded, or simply configured with a task that takes longer than the proxy permits.
Do not begin by increasing every timeout. A longer timeout can hide a slow application while tying up workers and connections for longer. Measure how long the request takes when sent directly to the backend, inspect database and application performance, and decide whether the request should be optimized, moved to a background job, or given a carefully chosen exception.
Read Logs From Both Sides
The proxy error log often identifies the failed phase: connecting to the upstream, sending the request, reading response headers, or reading the response body. Match the timestamp and request path with the application, PHP-FPM, container, or database logs. Our guide to Linux log files for website troubleshooting provides a structured way to correlate these events.
Also check system resources. Exhausted workers, memory pressure, a full disk, or connection limits can produce intermittent gateway errors that disappear during a manual test. Record the upstream response time and error rate so you can see whether failures follow traffic peaks or deployments.
Fix the Cause and Verify the Result
For a 502, verify the service endpoint, protocol, permissions, certificates, and application process. For a 504, investigate slow or blocked work before changing timeout values. After editing a proxy configuration, validate its syntax, reload it safely, and repeat the exact failing request.
Monitor the public URL from outside the server and alert on both status codes. A simple homepage check may miss errors limited to login, search, checkout, or API routes, so include important transactions where appropriate.
For the exact behaviour of directives such as proxy_connect_timeout, proxy_read_timeout, and proxy_next_upstream, consult the official NGINX proxy module documentation. The status code is a useful clue, but the correlated logs and a direct backend test usually reveal the real fault.