Skip to content
5 min

Five kinds of 502. The error log names each one.

I broke nginx five ways until I had five different 502s. Each leaves its own line in error.log. The stale IP one looked like a refused port while the app was fine.

  • Nginx
  • Docker
  • Debugging
Five sticky notes labelled A to E, each with one nginx error.log line: connection refused, upstream prematurely closed, too big header, host not found, and a refused connection to a stale IP.

Most advice for a 502 is about the browser. Refresh, clear the cache, try incognito. If you run the nginx, none of that helps.

A 502 means nginx reached for your backend and got something it couldn't use. And nginx writes down exactly what it got, every time. Not in the access log, that one just says 502. In the error log:

tail -n 20 /var/log/nginx/error.log

I put nginx in front of a small app in Docker and broke it on purpose until I'd seen five different 502s. Each one leaves its own line.

A: Connection refused

connect() failed (111: Connection refused) while connecting to upstream,
upstream: "http://172.21.0.2:3999/"

Nothing listens on that port. Wrong port in proxy_pass, or the app bound to 127.0.0.1 inside its container, or it's in the middle of restarting. This one is fast, the 502 came back in 8 ms. Compare the port in the log line with the port your app prints when it starts. ss -tlnp on the backend host if you're not sure.

B: Upstream closed early

upstream prematurely closed connection while reading response header from upstream

The app accepted the request, then died or hung up. A crash, an OOM kill, a gunicorn or PHP-FPM worker timeout. nginx can't tell you why. The app's own log at the same second can.

C: Too big header

upstream sent too big header while reading response header from upstream

The response headers didn't fit in nginx's buffer. In the lab a 2000 byte cookie went through and an 8000 byte one gave 502. proxy_buffer_size 16k; and proxy_buffers 8 16k; fix it. Also worth asking why the app is setting an 8 KB cookie.

D: Host not found

[emerg] host not found in upstream "api-backend" in /etc/nginx/conf.d/default.conf:4

This one stops nginx from starting at all. nginx looks up hostnames once, at start. If the backend's name doesn't exist yet, for example the backend container isn't up, there's no nginx.

E: The stale IP

This one I'd have never guessed. The error looks like A:

connect() failed (111: Connection refused) while connecting to upstream,
upstream: "http://172.21.0.2:3000/"

But the app was running fine. It had been recreated and came back at 172.21.0.4. nginx had looked up the name once, when it started, and kept using 172.21.0.2, which now belonged to a different container. Every request got a 502, in 0.003 seconds.

nginx -s reload fixed it instantly. To stop it coming back, put a resolver in and send proxy_pass through a variable, so nginx looks the name up again:

server {
    listen 80;
    resolver 127.0.0.11 valid=5s;
    location / {
        set $upstream http://b1:3000;
        proxy_pass $upstream;
    }
}

127.0.0.11 is Docker's DNS. On a VM use your network's DNS server. In Kubernetes, the cluster DNS service IP.

With that, I recreated the backend on yet another IP and didn't reload anything. 7 seconds later it answered 200. And with the backend missing completely, nginx still started, and the request got a clean error instead: b1 could not be resolved (3: Host not found).

Not every dead backend is a 502

I expected a stopped backend to give 502. It gave 504, after 60 seconds. A stopped container's IP doesn't refuse, it just never answers, so nginx waits for its 60 second connect timeout. That's a timeout problem, and a different hunt.

Timeouts have their own surprise. With proxy_read_timeout 3s and two servers in the upstream, a slow GET made the user wait 6.02 seconds, because nginx retried it on the second server. A POST wasn't retried, it failed at 3.01.

The short version

Read the upstream: part of the error line first. The IP and the port. Most of the time, that IP or that port just isn't your app right now.

All five, plus 504, 413 on upload, the SPA 404 on refresh, redirect loops and WebSockets, are one flowchart each in the Nginx Break-Fix Field Guide. If you want to start with something free, the Kubernetes sampler works the same way.

Useful to someone on your team?