A 502 means a gateway or proxy got an invalid reply from the server behind it. A 503 means a server is temporarily unable to handle the request, because of overload or maintenance. A 504 means the gateway waited and no timely reply came. All three are 5xx errors, but each points to a different place to look first.
Checked November 30, 2026 against RFC 9110, the nginx proxy module reference, AWS and Cloudflare troubleshooting pages and Google’s crawler documentation. Timeout defaults are the vendors’ published values and can be changed per site.
The short version
- 502 Bad Gateway. A gateway or proxy reached the next server and got something it could not use: a refused or reset connection, a malformed reply, or a reply cut off.
- 503 Service Unavailable. A server is deliberately or unavoidably not handling requests right now. RFC 9110 names overload and scheduled maintenance, and allows a
Retry-Afterheader. - 504 Gateway Timeout. A gateway or proxy reached the next server but did not get a response before its own timer ran out.
The key word in 502 and 504 is gateway. RFC 9110 defines both from the point of view of a server acting as a gateway or proxy. A site with no proxy in front of its application can still return 503, but it is much less likely to produce a 502 or a 504 of its own. If you see one of those two, there are at least two machines in the path, and the error was written by the one in front.
502 Bad Gateway: the reply was unusable
RFC 9110 says a 502 means the server, while acting as a gateway or proxy, received an invalid response from an inbound server it accessed while trying to fulfill the request. AWS lists what that looks like behind its Application Load Balancer:
- The target answered a connection attempt with a TCP reset, or closed the connection while a request was outstanding.
- The target response was malformed or carried invalid HTTP headers, or the response headers added up to more than 32 KB.
- The target was a Lambda function that returned an error, was throttled, or sent a body over 1 MB.
- The load balancer could not complete an SSL handshake with the target.
Cloudflare describes the same pattern from its side. A 502 or 504 page branded with Cloudflare’s name means the origin server itself returned that status. A blank, unbranded 502 or 504 points at Cloudflare, for example an origin that sends gzip-compressed content with a wrong content-length header.
Where to look first: the application process behind the proxy. Is it running, listening on the port the proxy expects, and returning well-formed responses? A crash, a restart in the middle of a deploy, or a wrong port in the proxy settings all end up as 502.
503 Service Unavailable: the server says not now
A 503 is the one of the three that describes the state of the server itself. RFC 9110 says the server is currently unable to handle the request because of a temporary overload or scheduled maintenance, which will likely be alleviated after some delay. The server may send Retry-After, either as a number of seconds or as a date, and the RFC adds that a server under load might simply refuse the connection instead of sending a 503.
Behind AWS’s load balancer, the documented cause is that the target group has no registered targets, or all of them are in an unused state. Cloudflare says a 503 generated by its own network contains the word cloudflare in the error page, while one without it came from the origin, whose logs should be checked for overload, an exhausted connection pool or a maintenance mode.
Where to look first: capacity and deploy state. Is every instance out of rotation, is a maintenance switch on, or is the application rejecting requests on purpose?
The related 429 status has a different meaning. It says that one client has sent too many requests, while a 503 describes the service’s own condition. Our guide to HTTP 429 covers Retry-After and retry limits in detail.
504 Gateway Timeout: the silence was too long
RFC 9110 says a 504 means the gateway did not receive a timely response from an upstream server it needed to access. The word timely hides a number, and that number is a setting in the proxy. The documented defaults differ:
- nginx:
proxy_connect_timeoutis 60 seconds by default andproxy_read_timeoutis 60 seconds by default. The read timeout applies between two successive reads, not to the whole response, so a response that keeps trickling bytes does not trip it. - AWS Application Load Balancer: the connection idle timeout is 60 seconds by default, the time allowed to connect to a target is 10 seconds, and AWS recommends sending at least 1 byte before each idle timeout elapses for long operations.
- Cloudflare: the proxy read timeout is 125 seconds. When the origin connects but does not answer within it, Cloudflare returns its own 524 rather than a standard 504. A 522 means it could not connect at all: no SYN+ACK within 19 seconds, or no data after the connection within 90 seconds.
Where to look first: whatever was slow, usually a database query, an export or a call to a third-party service. The proxy is only reporting that it gave up.
A worked example: which timer fires first
Take a report page that sits behind Cloudflare, then nginx, then the application, with the defaults above. How long the application stays silent decides what the visitor sees:
- The app answers after 45 seconds. Both timers are longer, so the visitor gets the page, slowly.
- The app stays silent for 75 seconds. nginx gives up first, at 60 seconds, and returns a 504 to Cloudflare. Cloudflare shows a branded 504, which tells the owner the origin said it.
- The owner raises
proxy_read_timeoutto 300 seconds, and the app stays silent for 140 seconds. Now nginx is still waiting when Cloudflare’s 125-second timer ends, and the visitor sees a 524. - The app closes idle connections after 30 seconds, but the AWS load balancer holds them for 60. The balancer can send a request down a connection the app just closed. AWS documents that case as a 502 and advises setting the application’s idle timeout higher than the balancer’s.
The lesson is that a 504 or 524 is not a statement about how slow the application is in general. It names the shortest timer in the chain, and the fix is to find which request ran past it. Raising one timer only moves the failure to the next one. Our explainers on what a CDN does and on why a status page lags behind an outage show where those layers sit.
What to do, by who you are
- A visitor. Reload once after a minute. Errors from a deploy or a brief overload often clear. If it stays, the owner has to fix it, and the page’s status or social channels are the place to check. A 5xx says nothing about your device or connection.
- A site owner. Read the error page before the logs. Branded error pages name the layer that wrote them. Then match the code: 502 to the application process, 503 to capacity and maintenance switches, 504 to the slow request. Cloudflare’s advice for hosting providers is to include the error code, the time with time zone and the URL, and to check every proxy and firewall between the edge and the origin, not only the origin’s own log.
- An API client. Treat 502, 503 and 504 as temporary, honor
Retry-Afterwhen it is present, and cap retries. A 504 does not tell you whether the work finished upstream, so for anything that creates an order or moves money, check the state before sending it again. - During maintenance. Return 503 with
Retry-After, not a 200 page that says “back soon”, so that clients and crawlers can tell it is temporary.
How search crawlers treat 5xx errors
Google’s documentation says 5xx and 429 errors prompt its crawlers to slow down. URLs that are already indexed are preserved for a time, but those that return a server error persistently are eventually dropped, and any content received with a 5xx status is ignored. Once the server answers with a 2xx status again, the crawl rate rises gradually. When an outage is large, the provider usually publishes a post-incident report, and our guide to reading a post-incident report explains the terms. Past incidents are listed in the outage tracker.




