HTTP 502 Bad Gateway

HTTP 502 Bad Gateway: causes, nginx upstream checks, the logs to read and how it differs from 504 and 503.

loading HTTP 502 Bad Gateway
About this tool

502 Bad Gateway: what it means

A 502 comes from a middle machine: a reverse proxy, a load balancer or a CDN that tried to get the answer from the server behind it and received nothing valid. The proxy itself is working, the application behind it is down, crashed, unreachable or answered with something that is not HTTP.

The code belongs to the 5xx class and its official name is "Bad Gateway". The page opens the status code list on 502, so you can compare it with its neighbours.

What the response looks like

HTTP/1.1 502 Bad Gateway
Server: nginx
Content-Type: text/html
Content-Length: 150

Common causes

  • The application process crashed or is not running, so nothing listens on the upstream port: nginx logs "connect() failed (111: Connection refused)".
  • The proxy points to the wrong port, host or socket after a change in the app or the container.
  • The upstream closed the connection in the middle of the answer ("upstream prematurely closed connection"), for example a worker killed for memory.
  • Response headers larger than the proxy buffer ("upstream sent too big header"), common with big cookies.
  • A rolling deploy in which the proxy still sends traffic to instances that are starting or stopping.

How to fix it

If you are a visitor

  • Wait a minute and reload: during a restart the error clears by itself.
  • If it persists, the owner has to act; check the site's status page.

If you run the site or the API

  • Call the upstream directly from the proxy host: curl -v http://127.0.0.1:3000/ . If that fails too, the proxy is innocent.
  • Check that the app is listening on the port the proxy expects: ss -ltnp | grep 3000.
  • Read both logs: the proxy's error log names the failure, the app's log says why it died.
  • Make the app restart on failure (systemd Restart=always, a container restart policy) and add a health check to the load balancer.

Snippets to copy

nginx: the upstream and a larger header buffer

upstream app {
    server 127.0.0.1:3000;
}
server {
    location / {
        proxy_pass http://app;
        proxy_buffer_size 16k;
    }
}

Is the app alive behind the proxy?

curl -sv http://127.0.0.1:3000/health
ss -ltnp | grep 3000
sudo systemctl status myapp

Codes it is confused with

CodeDifference
504 Gateway TimeoutThe upstream answered too slowly and the proxy gave up waiting.
503 Service UnavailableThe server itself says it cannot serve right now.
500 Internal Server ErrorThe application answered, and the answer is a failure.

Caching, retries and search engines

Because a 502 is usually a temporary state of the upstream, clients and load balancers can retry it with backoff. For search engines a short 502 is harmless; a long one makes them slow down their visits and, if it continues, drop the URLs.

Look up other status codes

  1. Open the list and type a number (404) or a word (timeout) in the search box.
  2. Read the description of the code and copy "502 Bad Gateway" with the button on its card.
  3. Use the # button to copy a link straight to that code.

Frequently asked questions

What is the difference between 502 and 504?

With 502 the proxy got an invalid answer or no connection at all; with 504 the connection worked but the answer took longer than the proxy timeout.

Why do I get 502 only after a deploy?

The new version may fail to start (missing variable, port in use) or take longer than the health check allows, while the proxy already sends it traffic.

Which port does the proxy use to reach my app?

The one in proxy_pass or the upstream block of the configuration, for example 3000 for Node or 8000 for Python apps. The ports page lists the usual ones.

Report a problem