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

Become a contributor for community

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

DigitalOcean Documentation

Full documentation for every DigitalOcean product.

Resources for startups and SMBs

The Wave has everything you need to know about building a business, from raising funding to marketing your product.

Get our newsletter

Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

New accounts only. By submitting your email you agree to our Privacy Policy

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.