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

nginx
location /example {
    return 502;
}
Apache (.htaccess)
RewriteEngine On
RewriteRule ^example$ - [R=502,L]
PHP
http_response_code(502);
echo 'Bad Gateway';
exit;
Node.js
res.writeHead(502, { 'Content-Type': 'text/plain' });
res.end('Bad Gateway');
Python (Flask)
# 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

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.