What causes 502 Bad Gateway in Nginx reverse proxy setup?
#1
I’ve been trying to set up a reverse proxy for my home lab server using Nginx, but I keep running into 502 Bad Gateway errors. The backend service is definitely running on the port I specified, and the Nginx error log just shows “connection refused” even though a simple curl from the same machine works fine. I’m wondering if my proxy_pass directive is wrong or if it’s a firewall issue on the host itself.
Reply
#2
That error usually means Nginx can’t reach the upstream from where it runs. If curl on the host works but Nginx says connection refused, check whether Nginx is in a container or on a different network. In my setup Nginx was in Docker and the backend was on the host; from inside the Nginx container 127.0.0.1 wasn’t the host. I tried curling the upstream from the Nginx container and it failed until I wired the right network. Try curling the upstream from the Nginx process/container and see what it reports.
Reply
#3
Double check proxy_pass target. If you’re using proxy_pass http://127.0.0.1:PORT and Nginx runs in a container, that address is the container itself, not the host. Switch to the backend’s network name or host gateway (for Docker, a service name on the same network or host.docker.internal in some setups). Also keep in mind trailing slash niceties can bite you, though that usually causes path problems rather than connection refused.
Reply
#4
Firewall stuff can still bite you. I’ve seen ufw or firewalld block the port between containers, yielding the same refused error even when the service is listening. Check the host firewall rules and any container-level firewalls, and temporarily open the port to verify.
Reply
#5
Could be something else subtle. A mismatch between IPv4 vs IPv6 binding or the service listening on a different interface can bite you. Try testing from the Nginx host/container with both IPv4 and IPv6 probes (curl -4 and curl -6) to see what actually lands. If nothing, you may be chasing the wrong culprit.
Reply


[-]
Quick Reply
Message
Type your reply to this message here.

Image Verification
Please enter the text contained within the image into the text box below it. This process is used to prevent automated spam bots.
Image Verification
(case insensitive)

Forum Jump: