More
More
502 Bad Gateway
A proxy, load balancer or CDN got an invalid response from the server behind it.
- Code
502- Reason phrase
- Bad Gateway
- Class
- 5xx Server error
- Defined in
- RFC 9110 section 15.6.3
- Cacheable by default
- No
What it means
Server error: the request may be fine, but the server failed to carry it out.
When a server should send it. By a gateway or proxy (nginx, a load balancer, a CDN) when the server behind it returned an invalid response or closed the connection. Applications themselves almost never send it.
Common causes. The application server (PHP-FPM, Node, Gunicorn) crashed, restarted or refused the connection.
What to do. Retry in a minute. Site owners should check that the backend service is running and look at the proxy's error log.
How clients and crawlers treat it
Browsers and HTTP clients. Browsers show the proxy's error page. Retrying after a moment often works, because the usual cause is a backend restarting or crashing.
Google Search. Googlebot treats 5xx responses as a sign of trouble and temporarily slows its crawl rate. Already indexed URLs are kept at first, but are dropped if the errors continue. Source: Google Search Central, How HTTP status codes, and network and DNS errors affect Google Search.
Caching. Not cacheable by default (RFC 9110 section 15.1 does not list it). A cache stores it only when the response says so with Cache-Control or Expires.
Example response
HTTP/1.1 502 Bad Gateway Content-Type: text/plain; charset=utf-8 Bad Gateway
How to send 502
location /example {
return 502;
}
RewriteEngine On RewriteRule ^example$ - [R=502,L]
http_response_code(502); echo 'Bad Gateway'; exit;
res.writeHead(502, { 'Content-Type': 'text/plain' });
res.end('Bad Gateway');
# in a view function return 'Bad Gateway', 502
Change the paths to suit. In nginx, add_header needs always to apply to error responses.
Related codes
- 504Gateway TimeoutA proxy or CDN waited too long for the server behind it.
- 503Service UnavailableThe server cannot handle the request right now.
- 500Internal Server ErrorSomething went wrong on the server and it has no more specific code to give.
All status codes · All 5xx codes
Names and numbers from the IANA HTTP Status Code Registry. Google Search behaviour as documented by Google Search Central.