Question

Hi, Im getting connection Refused error on Nginx and dotnet combination.

Posted March 30, 2020 8.7k views
Nginx

Here is my Nginx config.


server {
    listen        80;
    server_name 139.59.90.155;                                                                                                                  location / {
        proxy_pass         http://localhost:5000;
        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;
    }

    location /manage {
        proxy_pass http://127.0.0.1:5000/manage;
        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 am able to curl to the dotnet application inside the droplet. But refuses to connect via a browser.

  • Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
  • tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 12063/nginx: master
  • tcp 0 0 127.0.0.53:53 0.0.0.0:* LISTEN 640/systemd-resolve
  • tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 820/sshd
  • tcp 0 0 127.0.0.1:5000 0.0.0.0:* LISTEN 10928/dotnet
  • tcp 0 0 127.0.0.1:5001 0.0.0.0:* LISTEN 10928/dotnet
  • tcp 0 340 139.59.90.155:22 83.137.200.73:63066 ESTABLISHED 3082/sshd: root@pts
  • tcp 0 1080 139.59.90.155:22 222.186.42.155:53686 ESTABLISHED 12043/sshd: [accept
  • tcp6 0 0 :::22 :::* LISTEN 820/sshd
  • tcp6 0 0 ::1:5000 :::* LISTEN 10928/dotnet
  • tcp6 0 0 ::1:5001 :::* LISTEN 10928/dotnet
edited by AHA
4 comments
  • Hi @samynmhd - that is indeed mysterious, what do the logs say?

    For example, run: tail -f /var/log/nginx/error.log and attempt to access the site again, what errors come up? If nothing, run: tail -f /var/log/nginx/access.log to watch the regular access logs and try again. What comes up then?

  • @AHA, This is what i get on error log.

    2020/03/31 13:53:18 [error] 3017#3017: *107 connect() failed (111: Connection refused) while connecting to upstream, client: 187.34.106.145, server: example.com, request: "GET / HTTP/1.1", upstream: "http://139.59.90.155:5000/", host: "64.225.84.127:80"
    2020/03/31 13:53:18 [error] 3017#3017: *107 connect() failed (111: Connection refused) while connecting to upstream, client: 187.34.106.145, server: example.com, request: "GET / HTTP/1.1", upstream: "http://127.0.0.1:5000/", host: "64.225.84.127:80"
    2020/03/31 13:57:28 [error] 3017#3017: *110 connect() failed (111: Connection refused) while connecting to upstream, client: 83.137.200.73, server: example.com, request: "GET / HTTP/1.1", upstream: "http://139.59.90.155:5000/", host: "139.59.90.155"
    2020/03/31 13:57:28 [error] 3017#3017: *110 connect() failed (111: Connection refused) while connecting to upstream, client: 83.137.200.73, server: example.com, request: "GET / HTTP/1.1", upstream: "http://127.0.0.1:5000/", host: "139.59.90.155"
    2020/03/31 13:57:31 [error] 3017#3017: *110 no live upstreams while connecting to upstream, client: 83.137.200.73, server: example.com, request: "GET / HTTP/1.1", upstream: "http://localhost/", host: "139.59.90.155"
    2020/03/31 14:04:54 [error] 875#875: *1 connect() failed (111: Connection refused) while connecting to upstream, client: 83.137.200.73, server: example.com, request: "GET / HTTP/1.1", upstream: "http://127.0.0.1:5000/", host: "139.59.90.155"
    2020/03/31 14:04:54 [error] 875#875: *1 connect() failed (111: Connection refused) while connecting to upstream, client: 83.137.200.73, server: example.com, request: "GET / HTTP/1.1", upstream: "http://139.59.90.155:5000/", host: "139.59.90.155"
    2020/03/31 14:04:56 [error] 875#875: *1 no live upstreams while connecting to upstream, client: 83.137.200.73, server: example.com, request: "GET / HTTP/1.1", upstream: "http://localhost/", host: "139.59.90.155"
    2020/03/31 14:06:38 [error] 875#875: *5 connect() failed (111: Connection refused) while connecting to upstream, client: 83.137.200.73, server: example.com, request: "GET / HTTP/1.1", upstream: "http://139.59.90.155:5000/", host: "139.59.90.155"
    2020/03/31 14:07:08 [error] 875#875: *5 connect() failed (111: Connection refused) while connecting to upstream, client: 83.137.200.73, server: example.com, request: "GET / HTTP/1.1", upstream: "http://139.59.90.155:5000/", host: "139.59.90.155"
    2020/03/31 14:10:45 [error] 2926#2926: *1 connect() failed (111: Connection refused) while connecting to upstream, client: 83.137.200.73, server: example.com, request: "GET / HTTP/1.1", upstream: "http://139.59.90.155:5000/", host: "139.59.90.155"
    
    edited by MattIPv4
  • Show comment This comment has been marked as resolved by samynmhd.
  • Hi there @samynmhd,

    Thanks for sharing the solution with the community!

    Indeed as far as I can see from your netstat command, your dotnet application was binding on 127.0.0.1:5000 this is why you are not able to access it directly from outside the Droplet.

    By changing your URL to ("http://*:5000"); your dotnet application is now binding on 0.0.0.0:5000 and it is accessible via the outside world.

    If you now run netstat -plant again, you should be able to see the following output:

    • tcp 0 0 0.0.0.0:5000 0.0.0.0:* LISTEN 10928/dotnet

    Hope that this helps!
    Regards,
    Bobby

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.

×
1 answer

This question was answered by @samynmhd:

I managed to fix, just hand to change the Progam.cs file. Added UseUrls() and its working now.

       public static IHostBuilder CreateHostBuilder(string[] args) =>
           Host.CreateDefaultBuilder(args)
               .ConfigureWebHostDefaults(webBuilder =>
               {
                   webBuilder.UseStartup<Startup>().UseUrls("http://*:5000");
               });

View the original comment

Submit an Answer