Question

Nginx reverse proxy Connection refused

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


Submit an answer


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

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.

Bobby Iliev
Site Moderator
Site Moderator badge
November 19, 2021
Accepted 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

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel