Loading...

HTTP errors

504 Gateway Timeout: who waited, for how long, and how to fix it

A 504 is a waiting problem, not a broken connection. A proxy in front of your site asked your server for a page, waited as long as it was allowed to, and gave up. This guide shows how to find out who was waiting for whom, which timeout ran out first, and how to fix the slow part instead of hiding it.

9 min read Intermediate Updated

504 Gateway Timeout: who waited, for how long, and how to fix it

What a 504 actually says

A gateway is anything that takes a request and passes it on: a CDN edge, a load balancer, nginx in front of PHP-FPM or Node. 504 Gateway Timeout is the gateway saying: I reached the server behind me, I asked, and the answer did not come before my limit.

That sets 504 apart from its neighbours. A 502 Bad Gateway means the answer was unusable or the connection failed. A 503 Service Unavailable means something turned the request away on purpose. A 504 means nobody refused anything — the request was accepted, and it may well still be running on your server after the visitor has been shown the error.

That last point matters in practice. A 504 does not cancel the work: a report that timed out at the proxy can finish on the origin a minute later, and a visitor who presses the button again starts a second copy.

Who waited for whom

A typical request passes several waiting points: visitor → CDN edge → your web server (nginx or Apache) → your application (PHP-FPM, Node, a container) → your database or an external API. Every arrow is a gateway with its own timeout, and they are set independently, often by different people.

The rule that saves the most time: the shortest timeout in the chain is the one that fires. If the edge waits 60 seconds, your nginx waits 60 seconds for PHP-FPM and PHP stops a script after 30, the request never gets near the edge's limit — PHP ends it at 30 seconds and the hop above it reports the failure.

So the first question is not "why is the CDN timing out" but how long did it take before the error appeared? A 504 at almost exactly 60 seconds points at a 60-second limit; one at 30 seconds, at a 30-second limit. Compare that figure with the limits you know about and the hop that gave up usually names itself.

The causes behind almost every 504

A slow query. One query without the right index, scanning a table that grew from ten thousand rows to ten million. It was fast at launch and is slow today, and nothing in the code changed.

Work that is long by nature. CSV and Excel exports, PDF reports, bulk imports, image or video processing, a backup started from an admin page. These are not bugs; they are jobs that should not run inside a web request.

An external call with no timeout. A payment provider, a shipping API, a search service. When it hangs, your request hangs with it, and your visitors wait out someone else's outage.

An overloaded origin. When every PHP-FPM worker or thread is busy, new requests wait in a queue before they even start. Each request may be quick; the wait in line is what runs the clock out. These 504s cluster at traffic peaks.

Network trouble between hops. A firewall that silently drops packets instead of refusing them makes the proxy wait for a connection that will never open. The result is a 504 rather than a fast failure.

Diagnose it: measure where the time goes

Time the request through the CDN, then the same request straight to your origin. curl can split the time into phases, as in the commands below.

If connect is large or never completes, the problem is reaching the origin: a firewall, routing, a server that is down. If connect is quick and ttfb (time to first byte) is long, the origin accepted the request and is still working on it — the classic 504, and the answer is in your application.

The second command bypasses the CDN with --resolve and still sends your hostname. If the origin alone also takes 70 seconds, the edge is reporting the truth. If it answers in two seconds directly but times out through the CDN, look at what differs: a firewall rule, another backend for that hostname, or headers your application treats differently.

Then read the origin's own logs for that moment. In nginx, add $request_time and $upstream_response_time to the log format; in Apache, %D. Turn on your database's slow query log and, for PHP, the PHP-FPM slowlog, which writes a stack trace of every request that runs too long — usually the fastest way to the exact line.

Timing a request through the CDN and directly to the origin

# through the CDN
curl -o /dev/null -s -w 'connect=%{time_connect}s tls=%{time_appconnect}s ttfb=%{time_starttransfer}s total=%{time_total}s code=%{http_code}\n' \
  https://www.example.com/reports/export

# straight to the origin, same hostname
curl -o /dev/null -s -w 'connect=%{time_connect}s ttfb=%{time_starttransfer}s total=%{time_total}s code=%{http_code}\n' \
  --resolve www.example.com:443:ORIGIN_IP https://www.example.com/reports/export

Fix the slow part first

Raising a timeout makes the error disappear and leaves the visitor waiting just as long. Before touching any limit, make the request shorter.

Index and cache. Find the slow query in the slow log and add the index it needs. Cache results that many visitors share, in the application or at the edge with Cache-Control headers.

Move long work out of the request. Turn an export into a background job: the click queues the job and returns at once, the page shows progress, and the finished file arrives as a download link or by email. The web request stays short however large the job grows.

Give every external call its own timeout. A few seconds, with a sensible fallback, so another company's outage does not become yours.

Size the origin for the peak. If 504s cluster at busy hours, add workers (PHP-FPM pm.max_children, Node processes, container replicas) and let the edge cache absorb repeat requests so fewer of them reach the origin at all.

Raise the timeout only where it belongs

Some requests are long by design: a large upload being processed, a report that cannot be split, an API that streams results. For those, raise the limit — only for that path, and at every hop.

On cdn.com.tr this is a delivery rule setting: Origin response timeout (seconds), from 1 to 3,600 seconds per rule, 60 seconds by default. Create a separate rule for the path that needs it, for example /admin/export/, and give only that rule a longer timeout. The rest of the site keeps the short default and still fails fast when something is genuinely wrong. On the domain of a Managed Container app the default is 300 seconds unless the rule sets another value.

The timeout counts the time between two pieces of data from your origin, not the total length of the request. As long as data keeps flowing, the edge keeps the request open. That is why a streaming response — a CSV written row by row, a progress feed — can run for many minutes, while a script that computes for five minutes and then prints everything at once cannot.

Then line up the hops behind the edge: nginx proxy_read_timeout or fastcgi_read_timeout, PHP max_execution_time, your application server's own limit. A long path is only as long as its shortest limit.

When a CDN is in front

A CDN changes what a 504 costs you.

Cached pages keep serving. On a rule that caches, if your origin times out while the edge still holds a copy of the page — even an expired one — the edge serves that copy instead of the error. A slow minute on the origin becomes a slightly stale page rather than a broken one. That is a good reason to cache everything that can be cached, with TTLs that match how often the content really changes.

Slow paths become visible. When 504s appear only on /search or /checkout — the uncached, dynamic paths — you know exactly where to look.

Long-lived connections follow the same rule. A WebSocket connection that sends nothing for longer than the timeout is closed. Give the WebSocket path a separate rule with Allow WebSocket connections switched on and a timeout that suits it, or send a small heartbeat message more often than the limit. The origin connection guide shows both settings.

How to set it up

Step-by-step guides in the help centre:

504 Gateway Timeout FAQ

Is a 504 my server's fault or the CDN's?

In most cases the time is spent on your origin and the edge is the one that waited. Compare a request through the CDN with a direct request to the origin. If the origin alone is just as slow, the fix is on the origin; if it is fast directly and slow only through the CDN, look for a firewall or routing difference that affects the edge's connection.

What is the default origin timeout on cdn.com.tr?

60 seconds without data from your origin. You can set it per delivery rule from 1 to 3,600 seconds in Origin response timeout (seconds). On the domain of a Managed Container app the default is 300 seconds.

Should I set the timeout to 3,600 seconds everywhere?

No. A long timeout on every path means that when something really breaks, visitors watch a spinner for an hour instead of getting an answer in a minute, and stuck requests pile up on your origin. Keep the default for the site and raise it only on the rule for the path that is slow by design.

Why does my export work for small files and fail for large ones?

Because it builds the whole file before sending the first byte. Small exports finish inside the limit, large ones do not. Stream the file as it is generated, or run the export as a background job and send a download link when it is ready.

Can I get a 504 even if my application is fast?

Yes, when requests queue before they start. If every worker is busy, a fast request can still wait long enough to time out. 504s that appear only at busy hours point to capacity, not to code.

Does refreshing the page help?

Sometimes, but every refresh starts the same slow work on your origin again. On heavy pages, repeated refreshes add load exactly when the server is already struggling.