Find out why the proxy in front of your site got a bad answer

502 Bad Gateway means a server acting as a gateway or proxy, such as a CDN or the web server in front of your app, got an invalid response from the server behind it. The proxy itself is up. The server it forwards to crashed, refused the connection, closed it early or sent something the proxy couldn't read, and the proxy's error log usually says which.

Where a 502 comes from

  • If your site runs behind a CDN, or behind a web server that passes requests to an app process, each of those is a proxy, and the server it passes requests to is its upstream. RFC 9110 defines 502 as the status a gateway or proxy sends when it got an invalid response from that upstream.

  • So the 502 comes from the proxy, and the cause sits one hop further in. MDN notes that a proxy should pass along a valid error response from the upstream rather than replace it with a 502, so a 502 usually means the upstream didn't send a usable response at all.

  • With several proxies in a row, such as a CDN in front of a load balancer in front of a web server, any of them can send the 502. The style of the error page and the response's Server header can show which layer sent it.

Why the upstream's response failed

  • The app isn't running

    The process crashed or is restarting after a deploy. With nothing listening on its port, the proxy's connection is refused.

  • The proxy points at the wrong place

    After the app's port or address changes, the proxy still forwards to the old one, where nothing answers.

  • The upstream closed the connection early

    A crash partway through a request, or a worker killed for running too long, ends the connection before a full response arrives.

  • A kept-alive connection closed under it

    If your server closes idle connections sooner than the proxy expects, the proxy can send a request on a connection the server has just closed. This shows up as occasional 502s under steady traffic.

  • A response the proxy can't read

    Malformed headers, or headers larger than the proxy accepts, count as an invalid response even when your app thinks the request succeeded.

  • A failed TLS handshake to the upstream

    When the proxy connects to your server over HTTPS, a certificate or TLS setting it rejects on your server fails that connection.

Amazon's load balancer guide lists these causes for the 502s its Application Load Balancer sends, including a refused or reset connection, a malformed response, a TLS handshake error and a server keep-alive shorter than the load balancer's idle timeout.

Test the upstream directly

# From the proxy's machine, request the app on the address and port the proxy forwards to (8080 here)
curl -sS -o /dev/null -w "%{http_code} in %{time_total}s\n" http://127.0.0.1:8080/

# Request the public address and print the status and the server that answered
curl -sSI https://yoursite.com/ | grep -iE '^(HTTP|server)'

What to look for in the logs

  • Start with the proxy's error log at the minute of the 502. It usually records the failed attempt to reach the upstream, such as a refused connection or an invalid header, along with the upstream address it tried. That address shows whether the proxy points where you think it does.

  • Then read your app's log and the server's system log for the same minute. A crash or restart at that time explains the 502s. If the app logged the request as a success, compare the response it sent with what the proxy accepts.

  • If the first curl command prints 000 after an error that starts curl: (7) Failed to connect, nothing accepted the connection on that address and port: the app isn't running, it listens somewhere else, or a firewall rejects the connection. curl's error list describes each exit code, and 7 means curl couldn't connect to the host.

How to fix a 502 Bad Gateway error

  1. Find which layer sent the 502Check the error page and the Server header, then request each layer directly, starting with the app and moving outward. The first layer that returns a 502 is the one that can't get a valid response from the layer behind it.
  2. Get the app running and listeningRestart a stopped or crashed process, then find out why it stopped: an out-of-memory kill or an error at startup.
  3. Point the proxy at the right upstreamCompare the address and port in the proxy's settings with where the app actually listens, and reload the proxy after any change.
  4. Match the 502s to their timing502s during every deploy mean nothing serves traffic while the app restarts, so start the new process before stopping the old one. Occasional 502s under steady traffic point to keep-alive timing: keep your server's idle timeout longer than the proxy's.

Frequently asked questions

Is a 502 my server's fault or my host's?

The 502 comes from the proxy, but the cause is the server behind it. If your app answers a direct request from the proxy's machine, look at the proxy's settings or your host's status page. If it doesn't, the fix is in your app.

What's the difference between a 502 and a 504?

Both come from a proxy. A 502 means the upstream's response was invalid, and a 504 means the proxy didn't get a response in time. RFC 9110 defines both, and the 504 Gateway Timeout guide linked below covers timeouts.

Can the visitor's own network cause a 502?

Sometimes. MDN notes that when the site works for other visitors, a 502 can come from the visitor's own setup, such as a VPN, a proxy, a firewall or custom DNS settings.