Acefina LLC runs production infrastructure on bare metal and spends a lot of time thinking about why web requests take longer than they should. 504 Gateway Timeout is the error that shows up when a proxy loses patience with your application. It is one of the more useful errors HTTP has, because it tells you something specific: the proxy gave up waiting, and in most cases the upstream was reachable but its answer was not coming back fast enough.
I also wrote a companion piece on 502 Bad Gateway, and the distinction between 502 and 504 is the difference between “your backend is broken” and “your backend is slow.” Fixing them pulls you in opposite directions. This guide covers what 504 actually means, the seven causes I see in the wild, the platform-specific quirks (Cloudflare 524 is a separate animal), and how to diagnose one without reaching for the proxy config first.
1. What 504 Gateway Timeout Actually Means
Per RFC 9110 (section 15.6.5), a 504 response means the server, acting as a gateway or proxy, did not receive a timely response from an upstream server it needed to contact to complete the request. The key word is “timely.” In the usual case the proxy did connect and send the request, then got no response back within the time it was willing to wait. A proxy that cannot even open the connection within its connect timeout also returns 504 (nginx proxy_connect_timeout; an AWS ALB gives up after 10 seconds).
Whenever you see 504, two servers are in the picture: the “gateway” the browser reached (nginx, Cloudflare, an AWS ALB, Vercel’s edge, a Kubernetes ingress) and the “upstream” behind it (your app, a microservice, a serverless function). The gateway has a stopwatch. When that stopwatch hits zero before the upstream has sent its response headers, the gateway returns 504 (Cloudflare returns its own 522 or 524 instead).
Three concrete scenarios produce a 504:
- The proxy could not open a connection to the upstream within its connect timeout, usually because a firewall drops the packets or the upstream host is down.
- The upstream accepted the connection but never wrote any response at all.
- The upstream was too slow to read the request, such as a large upload, and the proxy’s send timeout ran out.
In the second and third cases the network path works and the upstream process is alive, so the problem is time. That is why 504 and 502 demand completely different diagnosis paths, and why conflating them costs hours. Once the upstream has started sending the page, a later stall cannot turn into a 504, because the proxy has already passed the status on. The browser gets a page that is cut off instead.
2. The Seven Real Causes of a 504
Every 504 I have diagnosed in the last few years has fallen into one of these seven buckets. In rough order of how often each comes up in practice, slow database queries and blocked worker pools dominate, with external API dependencies a close third.
Slow database query
A missing index, a full table scan, or a lock-heavy transaction turns a 20ms query into a 90s wait. Every request hitting that code path now times out at the proxy.
Worker pool saturated
PHP-FPM, Gunicorn, Puma, or Node event-loop workers are all blocked on slow work. New requests queue up behind them until the proxy gives up and returns 504, even for endpoints that are normally fast.
External API dependency
Your code makes a synchronous call to Stripe, OpenAI, a CRM, a payment gateway, and that third party is slow or hanging. Your request hangs with it until your proxy’s read timeout trips.
Long-running legitimate work
Report generation, file uploads, CSV exports, or PDF rendering genuinely takes longer than your proxy_read_timeout. The work is fine, the HTTP request path is the wrong place for it.
Origin overloaded
CPU pegged at 100%, memory thrashing, swap hit. The app is still running but every request is slow. Proxy timeouts trip en masse. Often load-related and time-correlated with a traffic spike.
Network path partitioned
A cloud network link between proxy and origin is lossy or slow. TCP retransmits burn seconds off the timeout budget. Less common on the same LAN, relatively common across regions or VPC peering.
Backend stuck in GC or pause
A JVM stop-the-world GC, a Ruby long pause, a Node.js sync fs call, freezes the worker for several seconds. If it happens often enough, it looks like intermittent 504 to your users.
In the 20 days to April 19, 2026, the checks Visual Sentinel ran captured no 504 at all, and a 502 Bad Gateway was only about 0.1% of the 5xx errors they did capture. That says little about how common 504s are. Our checks stop waiting after 30 seconds by default (you can set up to 120), sooner than nginx’s 60-second proxy_read_timeout, so a request that would have ended in a 504 shows up in our data as a check that timed out. When 504 does hit, it is often a tail-latency event, and the causes above are the usual suspects.
3. If You’re a Visitor: What You Can Actually Do
Honest answer: not much. A 504 is a server-side failure and the fix lives on the site owner’s infrastructure. Unlike connectivity errors, nothing on your device is contributing. That said, four things are worth trying before you give up or report it:
- Wait a minute and try again. A 504 caused by a short spike on the origin, such as a slow query, a GC pause or a deploy that hasn’t warmed up, can clear on its own once the spike passes.
- Hard refresh. Ctrl+Shift+R (Cmd+Shift+R on Mac). A CDN edge near you might have briefly cached the 504. A hard refresh forces a new round trip that may hit a different edge or catch the origin after it has recovered.
- Try a different network. Switch between WiFi and cellular. If the site loads from one and not the other, you might be on a CDN edge that is being routed to an unhealthy origin region.
- Check if it’s just you. Open our free website status checker (or any “is-it-down” service) to see if the site is returning 504 for everyone or only from your region. If only you are seeing it, report your geographic region to the site owner, it narrows their search.
Things that won’t help: reinstalling your browser, running a virus scan, clearing cookies. None of them touch the slow upstream path that caused the timeout.
4. If You Run the Site: Diagnose From Upstream Outward
The single rule that will save you hours: never raise a proxy timeout before you understand what the upstream is waiting on. The timeout is a symptom. Raising it from 60s to 300s just makes the symptom quieter for five minutes at a time instead of one.
Step 1: Reproduce the 504 and time it
The first thing I always run when 504 alerts fire:
curl -v -o /dev/null \
-w "%{http_code} %{time_total}s %{remote_ip}\n" \
--max-time 180 \
https://yoursite.com/failing-pathThe time_total number is the real signal. If the 504 comes back after ~60s, your proxy timeout is 60s. If you get a 524 after about 125s instead, Cloudflare’s own timeout ran out first. The timing tells you which layer is giving up, and that narrows the fix dramatically.
Step 2: Hit the origin directly, bypassing the proxy
Get onto the origin host and curl the app itself, with a long max time so you can see the real duration:
# On the origin host
curl -v -o /dev/null \
-w "%{http_code} %{time_total}s\n" \
--max-time 300 \
http://127.0.0.1:3000/failing-pathIf the origin takes 90 seconds to send a successful response, your application is the bottleneck, not your proxy. The 504 at the edge is a correct answer to a slow upstream. Now the real question: why is that endpoint slow?
Step 3: Identify the specific slow dependency
This is where the actual work happens. The upstream is waiting on one of:
- A slow database query. Enable
log_min_duration_statementin Postgres or the slow query log in MySQL and reproduce the request. The query above your threshold is the cause. - An external API call. Wrap outbound HTTP calls with a short timeout and log every one that exceeds it. When Stripe or a CRM is slow, your requests are slow too.
- A lock. A long-held row lock or table lock serialises every request that wants the same row. Look for
pg_lockscontention orSHOW PROCESSLISToutput in MySQL. - Blocking work on an event loop (Node.js, or Python asyncio). A single sync
fs.readFileSyncor a heavy JSON parse in a hot path halts every other request on that process.
Step 4: Check worker concurrency
If one endpoint is slow, it starves the others. PHP-FPM, Gunicorn, Puma all have a fixed pool of workers. Slow endpoints grab workers, fast endpoints end up queued, and you see 504 on everything during a load spike.
# PHP-FPM (needs pm.status_path = /fpm-status and a web server route to it)
curl http://localhost/fpm-status
# pm2
pm2 list
# Gunicorn (the master plus its workers)
ps aux | grep -c '[g]unicorn'If every worker is busy, you can temporarily bump the pool size to relieve the symptom, but do not ship that as the fix. Find the slow endpoint, address the root cause, then put concurrency back at a sane level.
Step 5: Read the proxy error log for the exact directive
For nginx, tail the error log while you reproduce the 504:
tail -f /var/log/nginx/error.logThe first line that mentions upstream tells you which timeout tripped: upstream timed out (110: Connection timed out) while reading response header from upstream is proxy_read_timeout, while connecting to upstream is proxy_connect_timeout, and while sending request to upstream is proxy_send_timeout. Different directives point at different failure modes.
Step 6: Decide, fix it or move it off the request path
If the operation should be fast (a product page, a dashboard query), fix the slowness: add the index, cache the API call, fix the lock. If the operation is legitimately long (report generation, video encoding, bulk CSV import), move it to a background job and return a 202 Accepted with a job ID immediately. Do not force users to hold an HTTP connection open for two minutes. That is what BullMQ, Sidekiq, and Celery exist for.
Rather have it fixed for you? Acefina, the team behind Visual Sentinel, repairs broken and hacked sites for a fixed price.
5. Platform-Specific 504s
Every proxy, CDN, and PaaS has its own timeout budget and its own way of expressing it. Knowing which directive matters on your stack tells you which layer gave up first.
| Platform | Directive | Default | Notes |
|---|---|---|---|
| nginx | proxy_read_timeout | 60s | Max idle time between two successive reads from the upstream. Most common 504 cause. Scope to specific location blocks, not globally. |
| nginx | proxy_connect_timeout | 60s | Time to establish the TCP handshake with upstream. nginx notes it usually cannot exceed 75s. If this trips, the upstream is usually down, firewalled or too overloaded to accept new connections. |
| nginx | proxy_send_timeout | 60s | Max idle time between two successive writes to upstream. Only bites on request bodies the upstream is slow to consume (large uploads). |
| Cloudflare | Proxy timeout (plan-level) | 125s | The default on every plan; only Enterprise can raise it. If your origin takes longer than 125s, Cloudflare returns 524 (not 504), and you need a background job, not a bigger timeout. |
| AWS ALB | Idle timeout | 60s | Applies to the client-to-ALB and ALB-to-target connections. ALB returns 504 when the target sends nothing before it runs out. Keep your target’s keep-alive longer than this, or ALB can reuse a connection the target just closed and return 502. |
| Vercel | Function max duration | 300s on every plan; up to 800s on Pro and Enterprise | Hard per-invocation cap. If your function runs longer, Vercel terminates it and returns 504. Move long work to a queue or use a streaming response. |
| HAProxy | timeout server | None (unset means no limit) | Max inactivity on the server side. HAProxy returns 504 when it runs out before the server sends its response headers. Left unset, HAProxy waits with no limit and warns at startup, so set it. |
The Cloudflare 524 distinction
Cloudflare is the one case where 504 gets confusing, because Cloudflare also exposes Error 524. They mean related but not identical things:
- Cloudflare 524: the TCP connection to your origin succeeded, but Cloudflare waited 125 seconds (the default) for an HTTP response and got nothing. Your origin is either stuck or too slow: Cloudflare lists long-running work, such as a large data query, and an overloaded server as the common causes.
- Cloudflare 504: the standard HTTP gateway timeout. Often generated by a proxy between Cloudflare and your true origin (another nginx, an ALB, a Kubernetes ingress) that itself gave up first.
Check the CF-Ray header and the error page. A 524 is Cloudflare’s own page and says “A timeout occurred.” For a 504, Cloudflare’s docs say a Cloudflare-branded 504 page means your origin sent the 504, while a blank page without Cloudflare branding means it came from Cloudflare itself.
AWS ALB idle timeout and keep-alive
ALB returns 504 when it cannot connect to the target within 10 seconds, or when the target accepts the request but sends nothing before the idle timeout (default 60s) runs out. The idle timeout applies to both sides of the connection, and AWS recommends a target keep-alive longer than it. If your target closes connections after 30s but ALB reuses them for up to 60s, ALB can send a request down a connection the target just closed, and that shows up as a 502, not a 504. Set your application keep-alive to at least 75s when sitting behind ALB.
Vercel function duration limits
Vercel caps function duration by plan. With fluid compute, which is on by default, functions run for up to 300 seconds by default on every plan, and Pro and Enterprise can raise maxDuration to 800 seconds. Exceed that and you get 504 (FUNCTION_INVOCATION_TIMEOUT in the logs). Raising maxDuration in your function config helps up to the plan ceiling, but above a few seconds you should really be streaming the response or moving work to a queue with a webhook callback.
6. Diagnosing a 504 with curl
Three curl invocations will tell you almost everything about a 504. Run them in order:
The time check
curl -v -o /dev/null \
-w "status=%{http_code} total=%{time_total}s connect=%{time_connect}s starttransfer=%{time_starttransfer}s\n" \
--max-time 180 \
https://yoursite.com/failing-pathThe difference between time_connect and time_starttransfer is how long the origin took to start producing a response. If starttransfer is close to your proxy timeout, the origin was sitting on the request before writing anything, that is the classic 504 pattern.
Bypass the CDN, hit the origin directly
# --resolve forces curl to connect to ORIGIN_IP while sending the right SNI
curl -v -o /dev/null \
-w "%{http_code} %{time_total}s\n" \
--resolve yoursite.com:443:<ORIGIN_IP> \
--max-time 300 \
https://yoursite.com/failing-pathIf this eventually returns the page (say after 90 seconds), your origin works but is slower than your CDN is willing to wait. If this also 504s or hangs, the problem is on the origin side and the CDN is blameless.
Watch the proxy log in parallel
# Terminal 1
tail -f /var/log/nginx/error.log
# Terminal 2
curl -v --max-time 180 https://yoursite.com/failing-pathWatch which step nginx names when the timeout trips: connecting to upstream, sending request to upstream, or reading response header from upstream. Each step maps to one directive (proxy_connect_timeout, proxy_send_timeout, proxy_read_timeout), so you know which value to tune or which layer to investigate.
7. 504 vs 502 vs 408: The Timeout Trio
Three status codes overlap around “something took too long,” but they are reported from different places and for different reasons. Telling them apart is half the diagnosis.
| Status | Who Gave Up | What It Means |
|---|---|---|
| 504 Gateway Timeout | The proxy | Upstream was too slow. Usually the connection worked and the upstream was reachable, but the response did not arrive in time. Fix is usually on the upstream app. |
| 502 Bad Gateway | The proxy | Upstream sent a broken response or refused the connection outright. Crashed backend, bad TLS, closed port. Fix usually on the origin or the proxy-to-origin wiring. |
| 408 Request Timeout | The server | The client was too slow. Server waited for the request body or further request and the client never finished sending. Usually a client-side or network issue, not a backend issue. |
The quick mental model I use:
- 504: the backend answered slowly. Fix the app.
- 502: the backend answered wrong. Fix the backend process or the path to it.
- 408: the client answered slowly. Probably not your server’s fault.
Also worth knowing: 503 Service Unavailable is different from all three. 503 means the server cannot take the request right now: it is overloaded, in maintenance mode or rate limiting, or a load balancer in front of it has no healthy backend to send it to. 504 means the server tried and ran out of time. If you see 503, check your rate limiter, maintenance toggles and backend health before touching anything else.
8. How to See 504s Before Your Users Do
A hard truth about 504: your own origin health check almost never sees them. A curl localhost/health from the origin host passes in well under a second because the health endpoint is fast by design. Meanwhile, the slow dashboard page behind a database query is 504ing for every user. The health check is correct and useless at the same time.
The practical way to observe externally visible 504s is to monitor the public URL through the same proxy path from outside your own infrastructure. Visual Sentinel sends scheduled requests from independent regions in Europe, the US and Asia Pacific (Asia Pacific on paid plans). Detection time depends on the plan interval and your alert settings, and when a check fails, we check the site again before an outage alert goes out.
Catching 504s externally
Configured uptime checks from Europe, the US and Asia Pacific hit your public URL on the interval included with your plan and identify as VisualSentinelBot/2.0 (to let them through a firewall, allow the check addresses on that page). They record status and timing evidence, which can expose a rising response-time trend before a later timeout. A confirmed 504 can then follow the alert routes available on your plan. A failure shorter than the check interval can be missed.
Set up uptime monitoringBeyond monitoring, the structural fixes that cut 504 rates over time: aggressive query timeouts at the database layer so a single slow query cannot hold a worker indefinitely, circuit breakers around external API calls so a hung third party cannot drag your app down with it, moving any operation longer than a few seconds to a background job, and watching p95 and p99 response times rather than the average (an average of 200ms can hide a p99 of 5 seconds).
9. Frequently Asked Questions
Related Guides
What Is 502 Bad Gateway and How to Fix It
The other half of the gateway error pair. 502 is “the upstream answered wrong,” 504 is “the upstream never answered.” Read both to cover the full diagnosis tree.
What Is a DNS Error?
The failure that happens before any gateway is involved: resolution, propagation, and record misconfiguration triage.
Rai Ansar
Founder of Visual Sentinel
Rai Ansar is the founder of Visual Sentinel. He writes about website monitoring, reliability, and catching the failures that uptime checks miss. More from Rai Ansar.
Run a website of your own? Read about keeping watch over your own website, so an alert tells you when something on it breaks.
See your site checked
Add your site and we check its uptime, SSL certificate and screenshots free for 3 days. Then try a paid plan free for 10 days (no card with a work email) or subscribe, from $15 USD a month.
Check my site free