502 Bad Gateway는 게이트웨이나 프록시 역할을 하는 서버가 상위(upstream) 서버로부터 유효하지 않은 응답을 받았다는 뜻입니다. 사용자가 접속한 리버스 프록시(Nginx, 로드밸런서, CDN) 자체는 정상이지만, 그 뒤의 실제 백엔드가 죽었거나 깨진 응답을 돌려줄 때 발생합니다.
즉 문제는 대개 프록시가 아니라 백엔드(애플리케이션 서버)에 있습니다. 백엔드가 크래시했거나, 포트가 닫혀 있거나, 프로토콜에 맞지 않는 응답을 보냈을 때 프록시가 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