5xx Server Errors

HTTP 502 Bad Gateway

The server acting as a gateway received an invalid response from an upstream server.

When is 502 used?

  • Nginx returning error from upstream
  • CDN origin server failure
  • API gateway upstream timeout

How to handle a 502 response

The problem is on the server side. Check server and proxy logs, retry idempotent requests with exponential backoff, and alert on sustained 5xx rates.

Check the status code a URL returns from the command line:

curl -s -o /dev/null -w "%{http_code}\n" https://example.com

How to return 502 from your server

Node.js (Express)

res.status(502).json({ message: 'Bad Gateway' });

Python (Flask)

return {"message": "Bad Gateway"}, 502

Go (net/http)

w.WriteHeader(502)

Frequently Asked Questions

What does HTTP 502 mean?

502 Bad Gateway: The server acting as a gateway received an invalid response from an upstream server.

Is 502 an error?

Yes. 502 is in the 5xx Server Errors class. Server errors: the server failed to fulfil an apparently valid request.