HTTP 504

504 Gateway Timeout

Server Error

The server, while acting as a gateway or proxy, did not receive a timely response from an upstream server it needed to access in order to complete the request. 504 differs from 502 in that the upstream did not produce a malformed response. It failed to respond within the gateway's timeout window. The gateway terminates the upstream request and emits 504 to the client. Common emitters include Nginx, HAProxy, Cloudflare, AWS ALB, and Kubernetes ingress.

When does this happen?

504 surfaces when the upstream takes too long. Long-running database queries, blocked locks, slow third-party API calls, and synchronous workloads that should have been async all produce 504s. Cloudflare's 524 is a related but distinct code (cf-specific timeout); ALB has a configurable target timeout that defaults to 60 seconds; Nginx's proxy_read_timeout default is also 60 seconds. Browsers display whatever HTML the gateway returns. The fix can be on either side: optimize the upstream to respond faster, or extend the gateway timeout to give the upstream more room. Persistent 504s on indexed pages cause de-indexing, similar to 502 and 500. Watch for 504s that mask deadlocks: if a query is held by another transaction, every other waiter will time out before the original transaction completes.

Common causes

How to fix it

Real-world examples

Reporting endpoint generates a 500MB CSV synchronously by querying a table without an index on the date column.
Query takes 90 seconds. Proxy times out at 60s and returns 504. Engineer moves report generation to a background job and serves the result via a polling endpoint.
Backend calls a third-party payment provider that is having an incident.
Payment provider's API hangs for 120 seconds. Application's proxy times out at 60s and returns 504. Operator adds a 5-second client-side timeout and a circuit breaker for the payment provider.
Database deadlock blocks every request that touches a hot row.
Affected requests time out at the gateway and return 504. DBA kills the long-running transaction and traffic recovers.
Application running in us-east tries to call a backend in eu-west across the public internet.
Network latency plus a slow query exceed the 60s default. Gateway returns 504. Operator moves the dependency to the same region.
Cloudflare cannot get a response from the origin within its default 120-second Proxy Read Timeout.
Cloudflare returns 524 (its variant of 504). Operator checks origin response times and optimizes the slow path.
Migration job runs an UPDATE on a 10-million-row table during peak traffic.
Table is locked; requests to that table time out at the gateway. Application returns 504 until the migration completes.

Debugging

How it differs from related codes

HTTP 502

502 is the upstream returning a bad or no response. 504 is the upstream not responding in time. Both surface at the gateway, but the cause differs. 502 is brokenness, 504 is slowness.

HTTP 503

503 is the server saying "I am unavailable" with a Retry-After. 504 is the gateway giving up on a backend that did not say anything. 503 is more cooperative.

HTTP 500

500 is an application-level error that returned promptly. 504 is the absence of a response within the budget. If your application returns 500 to the gateway, the client sees 500, not 504.

Related status codes

408 429 500 502 503

See HTTP 504 in your redirect chains?

Check your URLs with checkredirects.io