Search code examples
nginxasp.net-corekestrel-http-serverkestrel

NGINX & Kestrel 502 Response (111: Connection Refused)


I have NGINX setup in a docker container, it properly serves static content so there is no issue there. I configured it as a proxy to point to Kestrel on my mac. Kestrel responds just fine on port 5000 (checked via Curl) but for some odd reason NGINX is unable to connect to it.

Logs say:

15 connect() failed (111: Connection refused) while connecting to upstream, client: 172.17.0.1, server: [OMITTED], request: "GET /api/values/5000 HTTP/1.1", upstream: "http://127.0.0.1:5000/api/values/5000"

My NGINX config:

      location / {
        proxy_pass         http://dotnet;
        proxy_http_version 1.1;
        proxy_set_header   Upgrade $http_upgrade;
        proxy_set_header   Connection keep-alive;
        proxy_cache_bypass $http_upgrade;
        proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header   X-Forwarded-Proto $scheme;
    }
}
upstream dotnet {
    zone dotnet 64k;
    server 127.0.0.1:5000;
}

Solution

  • Since nginx is running in your container, 127.0.0.1 is local to the container, not the host machine that's running it.

    You'll need change the IP address to match what the container sees as the host (see here: https://forums.docker.com/t/accessing-host-machine-from-within-docker-container/14248/4)

    Also, don't forget to open up the port on your firewall :-)