Doprax Logo
502, 503, 504: Three gateway errors that all look the same and mean completely different things

Jun 24, 2026

5 min Read

502, 503, 504: Three gateway errors that all look the same and mean completely different things

by Swetlana A

502 error

503 error

504 error

A 502, a 503, and a 504 all show up as the same thing to your users: a blank page where your app used to be. To you, they are three different bugs with three different owners. One is almost always your application. One is usually you taking the server down on purpose, or your server falling over. One is something taking too long, which might be your code or might be the network between two machines you do not control.

Getting them confused costs you time. You restart the wrong service, you stare at the wrong logs, you blame the wrong layer. So here is the short version, then the long one.

The quick rule: 502 means the upstream answered wrong. 503 means the upstream is not there. 504 means the upstream answered too slowly. "Upstream" is whatever your reverse proxy is forwarding to, usually your app server.

The setup that produces these errors

Almost every gateway error happens in the same shape. A reverse proxy (nginx, Caddy, Traefik, a load balancer) sits in front, takes the request, and forwards it to your actual application (gunicorn, uvicorn, Node, PHP-FPM, whatever). The proxy is the one writing the error code, because the proxy is the one that tried to reach your app and got an answer it did not like.

That detail matters. When you see a 502, nginx is telling you about a conversation it had with your app. The error page is from nginx. The problem is almost never nginx. Read the codes as nginx reporting on someone else's behavior.

502 Bad Gateway: your app answered, and the answer was garbage

A 502 means the proxy connected to your app, sent the request, and got back something that was not a valid HTTP response. The connection worked. The reply was broken.

This is your fault about 90% of the time, and it is usually one of these:

  • Your app crashed mid-request. The worker died while handling the request, so the proxy got a closed connection or a partial response. Check your application logs first, not nginx. The stack trace is on the app side.
  • You are pointing the proxy at the wrong port. Your app is on 8000, nginx is forwarding to 8080. The thing answering on 8080 is not speaking HTTP the way nginx expects, so you get a 502 instead of a clean "nothing here".
  • The worker timed out and was killed. Gunicorn's default worker timeout is 30 seconds. A request that runs longer gets the worker killed, and the proxy sees a dropped connection. (This one masquerades as a 502 but is really a timeout in disguise, which is why people confuse it with a 504.)
  • A response header got mangled. Less common, but a malformed header or an oversized one your app emitted will trip the proxy.

How to confirm: hit your app directly, bypassing the proxy.

If that returns a clean response and the proxy still gives a 502, the problem is in the proxy config (wrong port, wrong socket path). If curl against your app also fails or hangs, the problem is your app. That single command splits the diagnosis in half.

503 Service Unavailable: there is nothing to forward to

A 503 means the proxy tried to reach your app and there was no app to reach. Nothing is listening. The upstream is down, not broken, just absent.

The honest causes:

  • Your app is not running. It crashed and did not restart, or it never started after a deploy, or the process exited on a config error. This is the most common 503 by a wide margin.
  • You restarted it yourself. During a deploy, there is a window where the old process is gone and the new one is not ready. Users hitting that window get a 503. This is normal and brief if your deploy is clean, and very much not brief if your new process is crash-looping.
  • The proxy is intentionally serving 503. Some setups return 503 on purpose when a backend is marked unhealthy or when you have a maintenance page configured. Load balancers do this when every backend fails its health check.
  • You hit a connection or worker limit. The app is up but every worker is busy, so new connections get refused. Raising worker count or adding a queue helps, but first confirm the workers are actually busy and not deadlocked.

How to confirm: check whether the process is alive and listening.

If ss shows nothing listening on the port your proxy expects, that is your 503 right there. Start the service, then watch it for 30 seconds to make sure it stays up instead of crash-looping.

504 Gateway Timeout: your app is thinking too hard

A 504 means the proxy reached your app, the app accepted the request, and then the app took longer to respond than the proxy was willing to wait. The connection is fine. The app is just slow on this request.

Where the time usually goes:

  • A slow database query. The request is blocked on a query that scans a table it should be indexing. This is the single most common real 504. The fix is in your query, not your timeout.
  • An external API call with no timeout of its own. Your app calls a third-party service, that service hangs, and your request hangs with it until the proxy gives up. Always set a client-side timeout on outbound calls so your app fails fast instead of dragging the proxy down with it.
  • Genuinely heavy work in the request path. Image processing, a report that aggregates a million rows, a synchronous job that should be a background task. The request is doing too much before it answers.
  • The proxy timeout is just set too low for a legitimately long operation. This is the one case where raising the timeout is the right move, and it is rarer than people want it to be.

How to confirm: time the request against your app directly.

If that number is bigger than your proxy's timeout, you have found your 504. The question then is whether the request should take that long. Usually it should not, and the answer is a database index or a background job, not a bigger timeout.

Who owns each one

ErrorWhat happenedUsually caused byLook here first502App answered, answer was invalidCrash, wrong port, killed workerApp logs503Nothing was listeningApp down, deploy gap, health check failsystemctl status, ss -tlnp504App answered too slowlySlow query, hung external callQuery timing, request profiling

The pattern across all three: the proxy is the messenger, the app is almost always the subject. Read the proxy logs to learn which error you have, then go straight to the application side to fix it. The number of hours lost to debugging nginx when the bug was a missing database index is not small.

The one config that prevents the most pain

Set timeouts that match reality at every layer, and make them agree. If your app's worker timeout is 30 seconds but your proxy waits 60, the proxy will report a 502 (worker killed) instead of a clean 504 (timeout), and you will spend the first ten minutes debugging the wrong error. Make the proxy timeout slightly longer than your app's worker timeout so the slow case surfaces as a 504, which tells you the truth: the app was slow, not broken.

That alignment alone turns a category of confusing 502s into honest 504s, and an honest error code is half the fix.

On a Doprax ProVM you control the whole stack, proxy and app on the same box, so there is no mystery middle layer adding its own timeouts and its own error pages between you and your code.