Dev.to WebDev 🛠 Dev 👁 2 📖 2 min read

How to Fix a 502 Bad Gateway in Nginx (Without Guessing)

A 502 means your reverse proxy got the request fine — but the app behind it didn't answer properly. Before touching any config, find out which layer is actually failing. That one habit fixes most 502s in minutes.

A 502 means your reverse proxy got the request fine — but the app behind it didn't answer properly. Before touching any config, find out which layer is actually failing. That one habit fixes most 502s in minutes.

The 4 causes that cover almost every 502

What the log says What it usually means
connect() failed (111: Connection refused) The app crashed, is bound to the wrong address, or the port is wrong
upstream prematurely closed connection A worker timed out, or the container was killed for running out of memory
upstream sent too big header Large cookies / auth headers overflow Nginx's default proxy buffers
connect() to unix:/run/php/php8.3-fpm.sock failed (13: Permission denied) The socket file is owned by a different user than Nginx runs as

Step 1 — Read the error log first

sudo tail -n 5 /var/log/nginx/error.log
sudo docker ps -a --format "table {{.Names}} {{.Status}} {{.Ports}}"

The log line tells you the exact failure. One tell from the full guide: a container showing Exited (137) was killed by the Linux OOM killer (128 + 9 SIGKILL). Confirm with dmesg -T | grep -i oom, then add swap or raise the container's memory limit — restarting it without that just repeats the crash.

Step 2 — The Docker binding trap (the most common one)

Two mistakes cause most Docker-related 502s:

  1. Binding to 127.0.0.1 inside the container. Frameworks like Vite, Next.js, Flask and FastAPI often default to loopback, so nothing outside the container can reach them. Bind to 0.0.0.0 instead (e.g. uvicorn app:main --host 0.0.0.0 --port 8000).
  2. proxy_pass http://localhost:3000; when Nginx itself runs in Compose. That points at the Nginx container, not your app. Point it at the Compose service name (http://web-app:3000;) and use Docker's embedded DNS resolver 127.0.0.11 so restarts don't leave Nginx caching a dead IP.

The full guide's production config also raises the timeouts (proxy_connect_timeout 60s, read/send 120s) and the proxy buffers (proxy_buffer_size 128k, proxy_buffers 4 256k) — that's what covers the timeout and oversized-header rows of the table above.

Step 3 — Test, reload, verify

sudo nginx -t && sudo systemctl reload nginx
curl -I https://app.example.com/api/v1/health

nginx -t catches a syntax mistake before the reload, and the curl -I should come back HTTP/2 200. If it doesn't, go back to Step 1 — the log line will have changed, and the new line names the next layer to fix.

Full guide with the complete config, the diagnostic matrix and the terminal walkthrough: https://thepracticalstack.blogspot.com/2026/11/how-to-fix-502-bad-gateway-in-nginx.html

Originally published on The Practical Stack. This is a companion summary — the full step-by-step guide lives there.

📰 Read the original article on Dev.to WebDev

Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.