Skip to main content

HTTP · 6 common causes

504 Gateway Timeout

Updated

The proxy waited for your application and gave up before a response arrived.

The exact message

$ curl -i https://app.example.com/reports/export
HTTP/1.1 504 Gateway Time-out
server: nginx

<html><head><title>504 Gateway Time-out</title></head>
<body><center><h1>504 Gateway Time-out</h1></center></body></html>

# nginx error.log
upstream timed out (110: Connection timed out) while reading response header from upstream

Paths, line numbers and stack frames differ on every machine, and the wording is tightened between versions. The part that identifies the problem is 504 Gateway Timeout.

How to fix it

Cause 1 of 6

The request genuinely takes longer than the proxy allows.

Make it asynchronous: accept the request, return 202 with a job id, do the work in a queue, and let the client poll or receive a webhook. Raising the timeout is a stopgap — most managed load balancers cap it anyway, and a request holding a worker for minutes hurts everything else on the box.

Cause 2 of 6

A slow query or a missing index.

Time the database work for that endpoint and run EXPLAIN ANALYZE on the slow query. An endpoint that was fine and got slow as data grew is nearly always a sequential scan that used to be small.

Cause 3 of 6

The application is blocked on a third party.

Set an explicit timeout on every outbound HTTP call your code makes — most clients default to waiting forever. Without one, a single slow vendor holds your workers until the proxy gives up on all of them.

Cause 4 of 6

Every worker is busy.

If the queue is full, even fast requests time out waiting for a slot. Check worker saturation and connection pool usage; adding workers helps only if the bottleneck is not the database behind them.

Cause 5 of 6

Timeouts across the chain are in the wrong order.

Client, CDN, load balancer and application server each have their own. Order them so the innermost is shortest and each layer outward is slightly longer — otherwise the proxy abandons work the application is still doing, and you pay for it twice.

Cause 6 of 6

A deadlock or an unbounded retry loop.

A request waiting on a lock another request holds never returns. Look for long-running transactions and retries with no cap; database lock views show what is blocking what.

Stopping it coming back

Set an explicit timeout on every outbound call and database query, move anything that can exceed a few seconds onto a queue, and alert on p99 latency rather than waiting for the timeout to be hit.

504 Gateway Timeout — common questions

Can I just raise the timeout?
Sometimes, for a genuinely long internal job behind your own nginx. But managed load balancers impose their own maximum, browsers give up eventually, and a long-held connection consumes a worker the whole time. It buys time; it does not solve the shape of the problem.
Why does the browser show a 504 when the work actually completed?
Because the proxy stopped waiting; the application carried on and finished. That mismatch is exactly why long jobs should return a job id immediately — otherwise the user retries and you run the work twice.
Is 504 ever a network problem rather than slowness?
It can be. If the upstream address is unroutable, the proxy waits for a TCP timeout and then reports 504 rather than a connection refusal. Check whether the proxy can reach the upstream at all before optimising queries.

Test the fix before you paste it back

Reproducing the failure in a scratch file is usually faster than editing the real one. AltF Code is a free in-browser editor with real syntax highlighting for HTML, CSS, JavaScript, TypeScript, Python, SQL and JSON, and it runs HTML, CSS and JavaScript live in a preview panel. No install and no sign-up.

Open the editor