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.
| Class | 5xx Server Error |
|---|---|
| Retry? | With backoff (idempotent requests only) |
| Cacheable by default | No — only with explicit Cache-Control |
| Response body | Allowed |
| Defined in | RFC 9110 §15.6.5 — 504 Gateway Timeout |
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
- Find the slow part with timing logs or tracing around queries and external calls.
- Move long work to a background job and return 202 Accepted with a status URL.
- Add indexes or caching for slow queries.
- 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
res.status(504).json({ error: "Gateway Timeout" });return Response.json({ error: "Gateway Timeout" }, { status: 504 });return {"error": "Gateway Timeout"}, 504http.Error(w, "Gateway Timeout", http.StatusGatewayTimeout)
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.