502 Bad Gateway

A server acting as a gateway or proxy received an invalid response from the upstream server it contacted.

Status

HTTP/1.1 502 Bad Gateway

Details

502 is reported by the intermediary, not by the origin. The reverse proxy, load balancer, or CDN reached upstream and got something it could not use: a connection refused, a connection reset mid-response, a malformed status line, or nothing at all.

The distinction from 504 is precise and useful. 504 means the upstream was too slow, 502 means the upstream answered badly or not at all. If a service crashes on start, everything in front of it reports 502 until it comes back.

Common causes

How to fix it

As the client

As the server

Examples

GET /api/v2/order HTTP/1.1
Host: api.example.com

HTTP/1.1 502 Bad Gateway
Content-Type: text/html; charset=utf-8
Server: nginx

Notes for proxy users

Through a forward proxy, a 502 can come from the proxy itself when it cannot reach the destination host, or from a reverse proxy at the destination. Requesting a known-good site through the same proxy separates the two in one step: if that succeeds, the proxy is healthy and the target's infrastructure is the problem.

Specifications

RFC 9110 §15.6.3

See also