# HTTP 504 Gateway Timeout — Meaning, Causes & Fixes

> 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
- Defined in: [RFC 9110 §15.6.5 — 504 Gateway Timeout](https://www.rfc-editor.org/rfc/rfc9110.html#section-15.6.5)

## Example response

```http
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.

## 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;
```

## FAQ

### 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

- [502 Bad Gateway](https://www.devkult.com/http/502.md)
- [524 A Timeout Occurred](https://www.devkult.com/http/524.md)
- [408 Request Timeout](https://www.devkult.com/http/408.md)

Open the interactive page: https://www.devkult.com/http/504