HTTP Status Codes / 5xx HTTP Status Codes / HTTP 502

502 Bad Gateway

This is an educational reference page about HTTP 502 Bad Gateway. The page itself is served as 200 OK so it can be indexed as HTTP documentation.

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.

Specification

Registered in the IANA HTTP Status Code Registry as Bad Gateway. Defined in RFC 9110, Section 15.6.3.

What it means

HTTP 502 Bad Gateway means a gateway or proxy, such as nginx, a load balancer, or a CDN, got an invalid response from the server behind it. The proxy is working, but the connection or the answer from the upstream server failed. To fix a 502, start with the upstream server, not the proxy that reported it.

When to use it

Common causes

How to fix it

Examples

nginx in front of a crashed app

GET /dashboard HTTP/1.1
Host: app.example.com

HTTP/1.1 502 Bad Gateway
Server: nginx
Content-Type: text/html

<html><head><title>502 Bad Gateway</title></head>...

Common mistakes

Developer notes

502, 503, and 504 often come from infrastructure, not your code. Check the Server header and the error page format to see which layer sent the response: CDN, load balancer, or reverse proxy.

Questions

What does HTTP 502 Bad Gateway mean?

HTTP 502 means a proxy or gateway got an invalid response from the upstream server it forwarded the request to.

How do I fix 502 Bad Gateway in nginx?

Check that the upstream app is running on the address in proxy_pass, then read the nginx error log for the exact upstream failure.

What is the difference between 502 and 504?

502 means the upstream sent a bad response or the connection failed. 504 means the upstream did not respond in time.

See also

Header and content rules

RFC 9110 adds no header or content rule specific to 502. The 5xx class rules also apply. See 502 in the response obligations table.

Related comparisons

5xx server error – the server failed to fulfill an apparently valid request


HTTP status code lookup
REST API status code chooser
cURL command generator
By Ping Now, a website/API/service uptime monitor
Data from Wikipedia