504 Gateway Timeout: causes and fixes
By the ProxyOnly team · 16 Nov 2026 · 5 min read
A 504 means a gateway gave up waiting for the server behind it. Where the timeouts live, what makes replies slow, and how to fix them.
A 504 Gateway Timeout means that a server acting as a gateway or proxy did not get a response in time from the server it needed to reach. It is the slow cousin of the 502 Bad Gateway. With a 502 the reply from behind was bad. With a 504 there was no reply before the gateway gave up. RFC 9110 defines the status code that way.
Two facts help before you start troubleshooting. The timeout belongs to the server in the middle, not to the one that is slow. And the request usually did arrive, so the work behind it may still be running when you see the error.
Every hop has its own patience
A request can pass through a browser, a CDN, a load balancer, a web server and an application, and each one has a time limit. The 504 comes from whichever gives up first. Some documented defaults:
- nginx waits up to 60 seconds to connect to a proxied server (
proxy_connect_timeout) and gives up after 60 seconds of silence while reading its response (proxy_read_timeout). Its documentation adds that the connect timeout usually cannot exceed 75 seconds. - Cloudflare returns error 524 when it connected to your server but received no HTTP response within 125 seconds by default. It returns error 522 when it cannot connect: no reply to its connection attempt within 19 seconds, or no data within 90 seconds after connecting.
The Cloudflare numbers are from its documentation on 16 November 2026. Limits like these change, so look up the current values for your own provider.
See it happen
We used the gateway from the 502 article, which waits two seconds for the application, and added a path on the application that sleeps for five. These are the lines of the gateway that matter:
try:
conn = http.client.HTTPConnection(*UPSTREAM, timeout=2)
conn.request("GET", self.path)
resp = conn.getresponse()
except socket.timeout:
return self.fail(504, "Gateway Timeout")Requesting the slow path through the gateway printed this:
/slow -> 504 in 2.006378sThe visitor got an error after exactly two seconds. The application, though, was not told to stop. In our test its handler still ran for the full five seconds, because nothing cancelled it. This is why a retry after a 504 can double the work, and why forms that change data need care.
There is a second route to a 504, and it shows why the cause matters. A closed port refuses a connection at once, and a gateway turns that into a 502. A firewall that silently drops packets leaves the gateway waiting until its connect timeout runs out. We tried both from a terminal:
$ curl -sS http://127.0.0.1:9/
curl: (7) Failed to connect to 127.0.0.1 port 9 after 0 ms: Couldn't connect to server
$ curl -sS --connect-timeout 3 http://192.0.2.1/
curl: (28) Failed to connect to 192.0.2.1 port 80 after 3005 ms: Timeout was reachedThe address 192.0.2.1 is reserved for documentation, so nothing answers there. That is a good stand-in for a server whose packets are dropped.
What causes a 504
- A slow database query, a missing index, or a database that is overloaded.
- The application calls another service, such as a payment or search API, without a timeout of its own, and that service is slow.
- Too much work inside one request: a large export, a report, an import or image processing.
- Not enough workers. When every worker is busy, new requests wait in a queue until the gateway's timer runs out.
- Network problems or silent packet loss between the gateway and the application.
- A firewall that drops packets instead of rejecting them.
- Cold starts on platforms that stop idle applications.
- A slow DNS lookup of the application's host name.
If you are a visitor
- Wait a moment and reload once. Repeated reloads add load to a server that is already struggling.
- Be careful with forms and payments. A 504 after you submit does not tell you whether the action happened. Check your email, order history or bank before you try again, so that you do not pay twice.
- Check whether the site is down for everyone. How to check if a website is down lists quick tests.
- If you use a proxy or VPN, try without it. Our page on proxy connection timed out covers the proxy side.
If you run the site
- Find which hop timed out. Compare the gateway's log with the application's log for the same request. In nginx, log
$request_timeand$upstream_response_time. The second is the time spent receiving the response from the upstream server, in seconds with millisecond resolution. If it is close to your timeout, the application is the slow part. - Find the slow part inside the application with the database's slow query log, a profiler or traces. Look at the time spent in outbound calls too.
- Fix the slow part first. Add the missing index, cache the result, or put a timeout on outbound calls so that your application fails fast with a clear error instead of hanging.
- Move long jobs out of the request. Return at once with a job ID, do the work in a background queue, and let the page check on progress. Cloudflare's documentation recommends status polling for long-running requests.
- Raise a timeout only as a last resort, and only for the routes that need it. A longer timeout keeps a worker busy for longer, which can make an overload worse.
- Check capacity: the number of workers, the queue length, CPU and memory.
- Check the network path and the firewall rules between the gateway and the application.
If a script gets a 504
Set your own timeouts so that the script never waits forever. In the Python requests library the timeout is a pair, one value for connecting and one for waiting for data. We ran this against the two cases above:
import requests
for label, url in (("blackhole address", "http://192.0.2.1/"), ("slow server", "http://127.0.0.1:9001/slow")):
try:
requests.get(url, timeout=(2, 3))
except requests.exceptions.ConnectTimeout:
print(label, "-> ConnectTimeout: no connection within 2 seconds")
except requests.exceptions.ReadTimeout:
print(label, "-> ReadTimeout: connected, but no data for 3 seconds")It printed:
blackhole address -> ConnectTimeout: no connection within 2 seconds
slow server -> ReadTimeout: connected, but no data for 3 secondsThen treat a 504 as temporary. Retry requests that are safe to repeat, with growing waits and a fixed number of attempts, as shown in the article on 503 Service Unavailable. Do not retry requests that change data without checking whether they went through.
If you send many requests at once to the same site and it starts timing out, you may be the cause. Lower your concurrency and read rate limiting for scrapers. If the timeouts only appear through your proxy, test the proxy on its own first, as described in how to test a proxy before you use it.
Try it yourself
Create an account, add funds from US$1 and order a proxy in the country you need.
Keep reading
25 Nov 2026 · Troubleshooting
409 Conflict explained
A 409 means your request conflicts with the resource's current state. The duplicate case, the lost update case, and how ETag and If-Match prevent both.
25 Nov 2026 · Troubleshooting
405 Method Not Allowed explained
A 405 means the URL exists but rejects your HTTP method. The Allow header says which methods work, and most 405s come from one of five causes.