Errors and troubleshooting
How do I deal with 504 Gateway Timeout?
Work out which hop timed out, retry with backoff, and make the request cheaper for the server, because a 504 means a gateway gave up waiting for a slow upstream.
What a 504 tells you
504 Gateway Timeout comes from an intermediary, such as a load balancer, CDN or proxy, that forwarded your request and did not receive an answer within its time limit. The server behind it may still be working on your request, or it may be down. Unlike a 502, nothing invalid came back; simply nothing came back in time. For scrapers, the three usual sources are a slow origin server, a slow or overloaded proxy, and an expensive request such as a huge search or export.
Find the hop that timed out
- Branding of the error page. A CDN page (with a request or Ray ID) means the site's edge waited for its origin. A plain page from your proxy provider means the proxy waited for the target.
- Direct comparison. Fetch the same URL without the proxy. If it is slow or 504 directly as well, the site is struggling.
- Timing. A 504 that arrives after exactly 30, 60 or 100 seconds reflects a gateway's timeout setting. Your own client timeout should be longer than that, or you will never see the 504 and just get a client side timeout instead.
Retry, but sensibly
A 504 is retryable, with backoff. But consider whether the request has side effects: if you posted a form, the server may have processed it even though the gateway gave up. For non-idempotent actions, check the result before retrying, or use an idempotency key if the API supports one; see idempotency.
import time, requests
RETRYABLE = {502, 503, 504}
def fetch_with_patience(session, url, tries=4, base=3.0):
for n in range(tries):
resp = session.get(url, timeout=(10, 90)) # connect, read
if resp.status_code not in RETRYABLE:
return resp
time.sleep(base * (2 ** n))
return resp
The split timeout keeps a dead connection from hanging for minutes while allowing slow but working pages to finish. The entry on request timeouts explains the two values.
Make the request lighter
Heavy requests are the most common cause you control. Ask for fewer results per page, narrow date ranges, avoid sorting options that force the server to scan everything, and split one giant export into many small queries. If an API offers pagination with a cursor, use small pages. A request that takes the server 40 seconds will hit a 30 second gateway limit every time, no matter how often you retry.
Proxy timeouts
Residential and mobile proxies add latency, and some gateways time out long requests to protect their network. If 504s appear only through the proxy, try a faster exit, a datacenter IP for that target, or a provider setting that raises the timeout. Test with the checks in how to test a proxy.
Cloudflare's related errors
Behind Cloudflare, origin timeouts usually appear as error 524 (origin accepted the connection but did not reply in time) or error 522 (connection to the origin timed out). The handling is similar: back off, lighten the request, and check whether the site is down for everyone.
Polling instead of waiting
When a server offers a job style API for long work, use it: start the job, get an id, and poll for the result. That keeps each HTTP request short and avoids gateway limits entirely. The GateSolver API follows that pattern for CAPTCHA tasks: createTask returns quickly with a task id, and getTaskResult calls are short, so a solve that takes a while never hits a gateway timeout. The page on async APIs describes the pattern in general.
Common questions
What is the difference between 502 and 504?
502 means the upstream sent an invalid reply or broke the connection; 504 means it did not reply in time.
Should I raise my client timeout for 504s?
Your client timeout should exceed the gateway's, but raising it will not stop a gateway from giving up first.
Can a 504 happen even though my request was processed?
Yes. The server may finish after the gateway gave up, so check the result before retrying actions with side effects.
Do proxies cause 504s?
They can, when the exit is slow or the provider limits request duration; compare with a direct request.
How many times should I retry a 504?
Three or four times with growing delays, then move on and log the URL for a later attempt.
Keep reading
- TimeoutConnect, read and total timeouts for HTTP calls, plus the separate deadline for an asynchronous task such as a CAPTCHA solve.
- Why do I get 502 Bad Gateway through a proxy?Through a proxy, a 502 usually means the gateway got no valid reply from the target or its own exit node.
- What is Cloudflare error 524?Error 524 means the origin accepted Cloudflare's connection but sent no HTTP reply before the timeout, usually a slow request.
- IdempotencyThe property that repeating an operation has the same effect as doing it once, and why it decides which retries are safe.