HTTP 502 Bad Gateway — What It Means and How to Fix It

The HTTP 502 Bad Gateway status code indicates that the server, while acting as a gateway or proxy, received an invalid response from an upstream server it accessed in attempting to fulfill the request. This typically means there's an issue with a server further up the chain that the primary server relies upon.

Last updated: 2026-06-11

RECOMMENDED

Essential Reading: Designing Data-Intensive Applications

The system design bible for software engineers. Learn to build reliable, scalable, and maintainable systems.

View on Amazon →
Status Code 502
Name Bad Gateway

Common Causes

  • Upstream server is down or unreachable (e.g., crashed, overloaded, network issues).
  • Incorrect server configuration (e.g., wrong proxy settings, firewall blocking).
  • DNS resolution problems for the upstream server.
  • Timeout issues between the gateway and the upstream server.
  • Malformed or invalid responses from the upstream server.

Code Examples to Handle HTTP 502

curl -I https://httpbin.org/status/502

How to Fix It

  1. Check the status of the upstream server(s) that the gateway/proxy relies on. Ensure they are running and accessible.
  2. Review the gateway/proxy server's error logs for more specific details about the invalid response or connection failure.
  3. Verify network connectivity and DNS resolution between the gateway/proxy and the upstream server.
  4. Increase timeout settings on the gateway/proxy if the upstream server is slow to respond but eventually does.
💡 Example: A user tries to access a website. The web server (acting as a reverse proxy) attempts to fetch data from a backend API server. If the API server is down or returns a malformed response, the web server will return a 502 Bad Gateway to the user.
🛠️ Developer Tip: When developing, ensure your backend services are robust and handle requests efficiently. Implement proper logging on all service layers to quickly pinpoint the source of invalid responses or connection failures.

Related Status Codes

Frequently Asked Questions

What causes HTTP 502?

HTTP 502 is caused when a server, acting as a gateway or proxy, receives an invalid or erroneous response from another server it's trying to communicate with to fulfill your request. This often points to a problem with an upstream server or network connectivity between them.

How do I fix HTTP 502?

To fix a 502, you typically need to investigate the upstream server(s) that the gateway/proxy depends on. Check if they are running, review their logs for errors, verify network connectivity, and ensure proper server configurations. Sometimes, simply restarting the affected services can resolve transient issues.