502 Bad Gateway
The server, while acting as a gateway or proxy, received an invalid response from the inbound server it accessed.
❓ Why does this happen?
Upstream application crashed, Nginx/proxy cannot connect to Node.js backend port.
✅ How to fix it
Check if the upstream application process is running and responding on its designated port.
Return or handle 502 in your stack
// app/api/resource/route.ts
export function GET() {
return Response.json(
{ error: "Bad Gateway", status: 502 },
{
status: 502
}
);
}Live 502 mock endpoint
Point your app, tests, or retry logic at this free public URL to reproduce a real 502 response (CORS enabled, optional ?delay=ms up to 5000).
https://castov.com/api/mock/502502 vs similar status codes
502 means the upstream answered with an invalid response or refused the connection. 504 means the upstream never answered in time.
502 comes from a proxy that got a bad answer from upstream. 503 is the service itself (or the proxy) saying it is temporarily unavailable.
520 is Cloudflare's catch-all for an empty or unexpected origin response; 502 is the generic gateway error from other proxies.
Frequently Asked Questions
What does HTTP Status 502 mean?
Why am I getting a 502 status code?
How do I fix a 502 Bad Gateway error?
What is the difference between 502 Bad Gateway and 504 Gateway Timeout?
What is the difference between 502 Bad Gateway and 503 Service Unavailable?
What is the difference between 502 Bad Gateway and 520 Web Server Returned an Unknown Error?
How can I test how my app handles a 502 response?
Other 5xx status codes
Browse the full HTTP status codes directory or troubleshoot terminal errors in the Error Fix Encyclopedia.