HTTP Status Codes Error 504: Gateway Timeout
The proxy waited longer than its timeout (nginx 60s, ALB 60s idle, API Gateway 29s) without a response from the upstream. Speed up the handler or make it async and return 202.
Identify: does your log match?
HTTP/1.1 504 Gateway Time-out Server: nginx/1.27.2 2026/10/22 13:12:09 [error] 31#31: *2210 upstream timed out (110: Connection timed out) while reading response header from upstream, client: 172.18.0.1, server: api.example.com, request: "POST /v1/reports HTTP/1.1", upstream: "http://172.18.0.5:3000/v1/reports"
- High
Slow query or external call
The handler waits on an unindexed query or a slow third-party API longer than the proxy timeout.
- Medium
Synchronous long-running job
Reports, exports or video processing done in the request path will always exceed gateway limits under load.
- Medium
Upstream saturated
All workers are busy; requests queue in the app until the proxy gives up.
Fix
Return 202 and process in the background
app.post('/v1/reports', async (req, res) => { const file = await buildReport(req.body); // 90s res.json({ url: file.url }); });
app.post('/v1/reports', async (req, res) => { const job = await queue.add('report', req.body); res.status(202).location(`/v1/reports/${job.id}`).json({ id: job.id, status: 'queued' }); }); app.get('/v1/reports/:id', async (req, res) => { const job = await queue.getJob(req.params.id); res.json({ status: await job.getState(), url: job.returnvalue?.url }); });
The request finishes in milliseconds; clients poll or receive a webhook when the job is done.
Verify: reproduce it on purpose
$ curl -i --max-time 5 https://httpbin.org/delay/10Run it against a dev environment to trigger 504 on purpose and test your error handler.
Prevent
- Set app-level timeouts below the gateway timeout.
- Measure p99 latency per route.
- Use a job queue for anything > 10 s.
FAQ
What is the AWS API Gateway timeout?
REST and HTTP APIs have a 29-second integration timeout by default; REST APIs can request an increase for Regional and private endpoints.
Why does my request still complete after a 504?
The proxy gave up, but the upstream kept running. Make handlers idempotent so client retries do not duplicate work.
Is Cloudflare 524 the same as 504?
Similar: 524 is Cloudflare's 100-second origin response timeout. A 504 comes from your own proxy or load balancer.
Related errors
Last verified 2026-10-22 · Source: IANA HTTP Status Code Registry (RFC 9110) · Official docs ↗ · ~40,500 searches/mo