Skip to main content
HTTP/5xx (Server Error)

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/502

502 vs similar status codes

Frequently Asked Questions

What does HTTP Status 502 mean?
The server, while acting as a gateway or proxy, received an invalid response from the inbound server it accessed.
Why am I getting a 502 status code?
Upstream application crashed, Nginx/proxy cannot connect to Node.js backend port.
How do I fix a 502 Bad Gateway error?
Check if the upstream application process is running and responding on its designated port.
What is the difference between 502 Bad Gateway and 504 Gateway Timeout?
502 means the upstream answered with an invalid response or refused the connection. 504 means the upstream never answered in time.
What is the difference between 502 Bad Gateway and 503 Service Unavailable?
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.
What is the difference between 502 Bad Gateway and 520 Web Server Returned an Unknown Error?
520 is Cloudflare's catch-all for an empty or unexpected origin response; 502 is the generic gateway error from other proxies.
How can I test how my app handles a 502 response?
Send a request to https://castov.com/api/mock/502 — it returns a real 502 Bad Gateway response with CORS enabled. Add ?delay=2000 to simulate a slow server.

Other 5xx status codes

Browse the full HTTP status codes directory or troubleshoot terminal errors in the Error Fix Encyclopedia.