502 Bad Gateway means a server acting as a gateway or proxy received an invalid response from an upstream server. The reverse proxy the user reached (Nginx, a load balancer, a CDN) is itself fine, but the actual backend behind it is down or returned a broken response.
So the problem is usually the backend (application server), not the proxy. When the backend has crashed, its port is closed, or it sent a response that does not conform to the protocol, the proxy translates that into a 502.
GET /api/profile HTTP/1.1
Host: www.example.com
Accept: application/jsonHTTP/1.1 502 Bad Gateway
Content-Type: text/html
Content-Length: 40
<html><body><h1>502 Bad Gateway</h1></body></html>upstream backend { server 127.0.0.1:3000; }
location /api {
proxy_pass http://backend;
proxy_next_upstream error timeout http_502;
}# Bypass the proxy to see if the origin itself is healthy
curl -i http://127.0.0.1:3000/api/profile