502 Bad Gateway

What an HTTP 502 Bad Gateway error means, the usual causes, and how to find the fault as a visitor or as the site's operator.

Updated

A 502 Bad Gateway means that a server acting as a gateway or proxy received an invalid response from the upstream server it was trying to reach. The machine that answered you, such as a load balancer, a reverse proxy or a CDN, is working. The service behind it either did not answer properly or could not be reached.

Where the error comes from

Most sites put at least one intermediary in front of the application:

browser  ->  CDN / load balancer / reverse proxy  ->  application server

A 502 is produced by the middle box when the hop to its right fails. The problem is therefore almost always on the server side and not in your browser.

502, 503 and 504

Status Meaning
502 Bad Gateway The proxy got an invalid response from the upstream, or could not connect to it
503 Service Unavailable The server cannot handle the request right now, for example because of overload or maintenance
504 Gateway Timeout The proxy got no response from the upstream in time

If you are a visitor

  1. Reload after a minute. Many 502s are brief, occurring during a deployment or restart.
  2. Check another network or device. This rules out a problem local to you.
  3. Look for a status page for the site or service.
  4. Wait. If the site is at fault, nothing on your side will fix it.

If you run the site

Work from the proxy back toward the application.

Is the upstream running?

The most common cause is that the application process has crashed, is restarting, or is not listening where the proxy expects. Check the service status and confirm the port or socket.

Can the proxy reach it?

  • Wrong address or port in the proxy's upstream configuration
  • Firewall or security group blocking the proxy from the backend
  • DNS for the upstream name failing or returning a stale address
  • A Unix socket with the wrong path or permissions

Is the upstream closing the connection?

  • Crashes mid-request, such as running out of memory, show up as a connection reset.
  • Keep-alive mismatch. If the backend closes idle connections sooner than the proxy expects, the proxy may reuse a connection that is already closed.
  • Responses the proxy cannot parse, such as malformed headers or headers larger than its buffers.

Is it a protocol mismatch?

A proxy that speaks plain HTTP to a backend expecting TLS, or the reverse, produces a 502. So does a TLS handshake between proxy and backend that fails over a certificate or protocol version.

Read the proxy's error log

The proxy's own error log usually names the cause directly. Phrases such as "connection refused," "connection reset by peer," "no live upstreams" or "upstream sent too big header" each point to one of the sections above. Then test the backend directly from the proxy host, bypassing the proxy:

curl -v http://127.0.0.1:8080/

Replace the address and port with your upstream's. If the direct request fails too, the fault is in the application and not in the proxy.