Question

Nginx reverse proxy Connection refused

Posted November 16, 2021 94 views
NginxDocker

Hi guys
I have spent hours trying to get two containers up & running correctly.
Container 1: nginx reverse proxy
Container 2: .net web api

Docker commands:

sudo docker network create --driver bridge tmpnetwork
sudo docker run -it -d --net tmpnetwork --rm -d -v /home/nginxfiles:/etc/nginx/conf.d --name revprox -p 80:80 nginx 
sudo docker run -it -d --net tmpnetwork --name playapi -p 8081:80 2fe9c1e579aa

The web api is up & running. This works fine:
curl http://localhost:8081/weatherforecast
and
http://example.com:8081/weatherforecast

conf file:

server {
    listen        80;
    listen [::]:80;
    server_name example.com;
    location / {
        proxy_pass         http://127.0.0.1:8081;
        proxy_http_version 1.1;
        proxy_set_header   Upgrade $http_upgrade;
        proxy_set_header   Connection keep-alive;
        proxy_set_header   Host $host;
        proxy_cache_bypass $http_upgrade;
        proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header   X-Forwarded-Proto $scheme;
    }
}

I have tried: ufw disable

sudo docker logs --tail=10 -f

Gives error:
failed (111: Connection refused) while connecting to upstream, client: xxx

How do I troubleshoot this?
Br

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

×
Submit an Answer
1 answer

Hi there,

As you’ve specified localhost/127.0.0.1 in your Nginx container, it is trying to connect on that port to itself.

Instead, as your containers are in the same network, you can specify the backend container name:

        proxy_pass         http://playapi;

That way the Nginx container will try to connect to the playapi container directly over the tmpnetwork network.

Your Nginx configuration would have worked if the Nginx service was running on the host itself rather than a separate container. Or if the Docker network was set to the host network rather than bridge.

Let me know how it goes!
Regards,
Bobby