devkult_
tools29converters34

504 Gateway Timeout

5xx server error

A proxy or gateway didn't get a response from the upstream server in time.

504 means the middleman gave up waiting. The proxy reached the upstream application, but no response arrived before its timeout — nginx's proxy_read_timeout and AWS ALB's idle timeout both default to 60 seconds, and Cloudflare reports its own version as 524 after 100 seconds.

The request may well have finished on the backend after the proxy stopped waiting.

Class5xx Server Error
Retry?With backoff (idempotent requests only)
Cacheable by defaultNo — only with explicit Cache-Control
Response bodyAllowed
Defined inRFC 9110 §15.6.5 — 504 Gateway Timeout
Example response
HTTP/1.1 504 Gateway Timeout
Content-Type: application/json
Content-Length: 27

{"error":"Gateway Timeout"}

Common causes

  • Slow database queries or external API calls inside the request.
  • Long-running work — reports, exports, media processing — done synchronously.
  • An overloaded upstream queueing requests.
  • Network problems between the proxy and the upstream.

How to fix it

  1. Find the slow part with timing logs or tracing around queries and external calls.
  2. Move long work to a background job and return 202 Accepted with a status URL.
  3. Add indexes or caching for slow queries.
  4. Raise the proxy timeout (nginx proxy_read_timeout) only as a stopgap.

Useful here: Retries, backoff and Retry-After · 502 vs 503 vs 504

Send a 504 from your server

Express
res.status(504).json({ error: "Gateway Timeout" });
Next.js route handler
return Response.json({ error: "Gateway Timeout" }, { status: 504 });
Flask
return {"error": "Gateway Timeout"}, 504
Go net/http
http.Error(w, "Gateway Timeout", http.StatusGatewayTimeout)
nginx
return 504;

Check what a URL returns

Print just the status line from the command line, or check a whole list of URLs at once, with their response headers, in the HTTP Status Checker.

curl -sI https://example.com/your-path | head -n 1

Frequently asked questions

Did my request fail if I got a 504?

Not necessarily — the backend may have finished after the proxy timed out. Before retrying something non-idempotent, like a payment, check whether it went through.

504 vs 502?

504: no answer in time. 502: a bad answer, or a dropped connection.

Related status codes