Sphere
Log in Join
☆ 7

nginx 502 bad gateway after upstream port change

nginx 1.24 on Ubuntu 22.04 as a reverse proxy. We moved the upstream app from port 3000 to 3001. Updated the proxy_pass to http://127.0.0.1:3001 and reloaded. Now every request returns 502 Bad Gateway. error.log says: connect() failed (111: Connection refused) while connecting to upstream, client: 10.0.1.4, upstream: http://127.0.0.1:3001/... The app is definitely listening on 3001 (curl localhost:3001 works). What is left?

⚑ Report

4 replies

☆ 0
C ctrl ·2026-10-27#1

curl works from your shell but does nginx user have permission? Also check the app is bound to 0.0.0.0:3001 or 127.0.0.1:3001, if it is bound to a specific interface nginx cannot reach, curl might be hitting a different path. Run ss -ltnp | grep 3001 to see the bind address.

⚑ Report
☆ 0
B byte ·2026-10-29#2

ss showed the app bound to 127.0.0.1:3001 but nginx runs in a Docker container on the same host, so 127.0.0.1 inside nginx is the container, not the host. Changed proxy_pass to the host bridge IP and it works. I keep forgetting the container network boundary.

⚑ Report
☆ 0
O onemore ·2026-10-31#3

Ah, that is the classic container localhost trap. Cleaner fix: put both nginx and the app on the same Docker network and use the service name as the upstream (http://app:3001). That way you are not hardcoding a bridge IP that changes on restart.

⚑ Report
☆ 0
A async ·2026-11-05#4

If you do keep them on the host, you can use host.docker.internal (Docker Desktop) or add host-gateway to /etc/hosts in the container. Either way, avoid 127.0.0.1 from inside a container, it never means the host. It is a good reminder that localhost is relative to where you run the command.

⚑ Report
Log in to reply.