Chapter 4 · Lesson 10 of 18
Deploy and operate

Fix 502 Bad Gateway behind a reverse proxy

A 502 Bad Gateway means the reverse proxy is running but could not get a usable answer from your app, usually because the app is down or on another port or address.

By the end you will

Find why a reverse proxy returns 502 and get your app answering again.

Time
20 min
Level
Intermediate
Check
4 questions
You need
  • A server with nginx in front of an app
  • SSH access with sudo

You deploy, open the site, and see a plain “502 Bad Gateway” page. Your server is up, nginx answers, yet the app does not. Working from the proxy toward the app finds the break in a few commands.

How it works#

A reverse proxy accepts the browser’s request and forwards it to your app, which is called the upstream. The proxy then returns the app’s reply. If the proxy cannot open a connection, or the app closes it without a valid HTTP reply, the proxy tells the browser “502 Bad Gateway”. That page comes from nginx, so you know nginx works.

So a 502 is a question about the hop between nginx and your app. Four things break it:

  • The app is not running or crashed.
  • The app runs, but on another port or hostname than proxy_pass names.
  • The app listens on an address the proxy cannot reach. In a container, 127.0.0.1 means “this container only”.
  • The app accepts the connection and then dies or sends a broken reply.

A 504 is related but different: the proxy reached the app, which did not answer within the proxy’s time limit.

Step 1: Read the proxy error log#

bash
sudo tail -n 30 /var/log/nginx/error.log

What you should see: lines with the failing request. The line connect() failed (111: Connection refused) while connecting to upstream means nothing is listening at the address in proxy_pass. The line upstream timed out (110: Connection timed out) while reading response header from upstream means a timeout (504).

Step 2: See what is listening#

bash
sudo ss -tlnp

-t shows TCP, -l listening sockets, -n numbers instead of names, -p the owning process.

What you should see: one line per listening socket. Find your app’s port. If it is missing, the app is down. If it shows 127.0.0.1:3000 but your proxy points at another port, fix the port in proxy_pass.

Step 3: Test the app directly#

bash
curl -I http://127.0.0.1:3000

Replace 3000 with your port. -I fetches headers only.

What you should see: a status line such as HTTP/1.1 200 OK. If this fails with curl: (7) Failed to connect, the proxy is not the problem.

Step 4: Check the container#

bash
docker ps
docker logs --tail 50 NAME

What you should see: the container listed as Up. If it is not listed, it exited, and the logs say why. A status like Restarting means it crashes in a loop.

If it runs but is unreachable, check two things. First, the port must be published, for example 127.0.0.1:3000:3000 in your compose file. Second, the app inside must listen on 0.0.0.0, not 127.0.0.1, or the published port cannot reach it. Set the app’s host or bind option, for example HOST=0.0.0.0 or --host 0.0.0.0, depending on the framework.

Check it worked#

Run curl -I https://example.com again. You should get your app’s normal status, such as 200, instead of 502. Then confirm the error log has no new connect() failed lines:

bash
sudo tail -n 5 /var/log/nginx/error.log

Edge cases and gotchas#

  • Nginx resolves hostnames only at start or reload. If proxy_pass http://app:3000; points at a container that was recreated with a new IP, nginx may keep the old address. Reload nginx, or use a resolver with a variable.
  • host not found in upstream at startup. Nginx cannot resolve the name when it reads the config, usually because the proxy and app are not on the same Docker network or the app is not started yet.
  • localhost may mean IPv6. localhost can resolve to ::1 while the app listens on IPv4 only. Use 127.0.0.1 in proxy_pass to be exact.
  • An app that is still starting. A slow boot gives 502 for the first seconds of a deploy. A health check and a start-up wait avoid sending traffic too early.
  • Out of memory kills. If the kernel kills the app on a small server, it disappears with no app log. Check docker inspect NAME --format '{{.State.OOMKilled}}', and consider adding swap.
  • SELinux can block the connection. On some Linux distributions that use it, nginx is denied connecting to a port and the log shows (13: Permission denied). The fix is a policy setting, not a port change.
  • Behind a CDN. The CDN may show its own 502 or 5xx page. Check whether the error comes from the CDN or from your server by calling your origin address directly.

Troubleshooting#

SymptomLikely causeFix
connect() failed (111: Connection refused) while connecting to upstreamApp down or wrong port in proxy_passStart the app, or fix the port, then sudo nginx -t and reload
no live upstreams while connecting to upstreamAll servers in an upstream block are marked failedFix the app, then reload nginx
upstream prematurely closed connection while reading response header from upstreamApp crashed or reset the connection mid-requestRead the app logs for a crash or exception
upstream timed out (110: Connection timed out) (504)App too slow or hungFind the slow request, or raise proxy_read_timeout as a stopgap
Works after restart, then 502 againApp crashes under load or runs out of memoryCheck logs, OOMKilled, and memory use
502 only inside Docker, app works on the hostApp listens on 127.0.0.1 inside the containerBind to 0.0.0.0
(13: Permission denied) while connecting to upstreamSELinux or a firewall rule blocks nginxAllow the connection in the policy
Still 502 after editing the configConfig not reloadedsudo nginx -t then sudo systemctl reload nginx

Real-world scenario#

You move an app into Docker. Before, it ran directly on the server on port 3000 and nginx used proxy_pass http://127.0.0.1:3000;. After the move the site returns 502.

bash
sudo tail -n 3 /var/log/nginx/error.log
docker ps
curl -I http://127.0.0.1:3000

The log shows connect() failed (111: Connection refused) while connecting to upstream. docker ps shows the container is up with no ports column entry, and curl fails. The compose file has no ports: line, so nothing is published. You add 127.0.0.1:3000:3000, run docker compose up -d, and curl now answers, but with Connection reset by peer. The app is binding to 127.0.0.1 inside the container. You set its host option to 0.0.0.0, recreate the container, and the site loads. Two separate causes hid behind one symptom, which is why each step checks one hop.

Quick reference#

CheckCommand
Proxy error logsudo tail -n 30 /var/log/nginx/error.log
Listening portssudo ss -tlnp
Test the appcurl -I http://127.0.0.1:3000
Container statedocker ps and docker logs --tail 50 NAME
Test and reload nginxsudo nginx -t && sudo systemctl reload nginx
OOM checkdocker inspect NAME --format '{{.State.OOMKilled}}'

Next#

Protect your data before the next outage: Back up a Postgres database in Docker.

Check what you learned4 questions
1The nginx error log says connect() failed (111: Connection refused) while connecting to upstream. What does it mean?
2Your app in a container listens on 127.0.0.1 inside the container. What happens?
3You changed proxy_pass to the new port but still get 502. What did you likely skip?
4Users see 504 instead of 502. What does that point to?

Frequently asked questions

Is 502 an nginx problem?

Rarely. Nginx is working, since it answered you. The fault is almost always the app behind it, which may be stopped, crashed, or not where the proxy expects.

Why does my container app refuse connections?

If it listens on 127.0.0.1 inside the container, nothing outside can reach it. It must listen on 0.0.0.0.

What is the difference between 502 and 504?

502 means the proxy got no valid answer, often a refused connection or a crashed app. 504 means the app was reached but did not answer in time.

The app works with curl on the server but not through nginx. Why?

The proxy may point at another port or hostname, run in a different network or container, or be blocked by a security policy such as SELinux.

Sources

View MD Edit
Share
Helpful?
Progress is saved in this browser only.

Keep going: guides and tutorials

View MD Edit
Share
Helpful?
Run this on your own server

Levelrail deploys apps from git with health-checked releases and one-click rollback.

docker pull ghcr.io/glincker/levelrail:latest Install Levelrail From GLINR Studios