9

I'm testing net core app deployment to a Digitalocean droplet running Ubuntu 18.04 and Nginx 1.14. For testing, I'm using the templates available from 'dotnet new'.

The dotnet new web and dotnet new mvc apps work fine. I'm able to reach them from my browser. I'm having issues with the dotnet new webapi app. However I try to reach it, I end up with a ERR_CONNECTION_REFUSED response.

Short version of what I've done to setup my droplet:

wget -q https://packages.microsoft.com/config/ubuntu/18.04/packages-microsoft-prod.deb

sudo dpkg -i packages-microsoft-prod.deb

sudo add-apt-repository universe

sudo apt-get install apt-transport-https

sudo apt-get update

sudo apt-get install dotnet-sdk-2.2

sudo apt install nginx sudo ufw allow 'Nginx HTTP'

/etc/nginx/sites-available/default content:

server {
 listen 80;
 location / {
 proxy_pass http://127.0.0.1:5000; #also tried 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;
 }
}

Results from curl (ip address intentionally derped)

curl -I -X GET http://123.123.123.123/api/values

HTTP/1.1 307 Temporary Redirect
Server: nginx/1.14.0 (Ubuntu)
Date: Sun, 19 May 2019 00:32:21 GMT
Content-Length: 0
Connection: keep-alive
Location: https://123.123.123.123:5001/api/values

My Nginx log gives me this error, which I've Googled to no avail:

2019/05/19 00:03:06 [error] 3632#3632: *4 connect() failed (111: Connection refused) while connecting to upstream, client: 12.123.12.123, server: , request: "GET / HTTP/1.1", upstream: "http://127.0.0.1:5000/", host: "123.123.123.117"

In response to @Oliver's question, here's the output of netstat -ntlp

Active Internet connections (only servers)
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      3527/nginx: master
tcp        0      0 127.0.0.53:53           0.0.0.0:*               LISTEN      605/systemd-resolve
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      946/sshd
tcp6       0      0 :::22                   :::*                    LISTEN      946/sshd

Any suggestions as to what I might be overlooking?

Thanks!

IdusOrtus
  • 1,005
  • 1
  • 16
  • 24
  • 2
    The Web API template enforced HTTPS, so the redirection leads the browser to port 5001. However, your nginx configuration does not expose the HTTPS service at port 5001. That explains exactly why ERR_CONNECTION_REFUSED happened. You can edit the test project and remove its HTTPS redirection. – Lex Li May 19 '19 at 05:03
  • 3
    What's the output of `netstat -nltp`? – Oliver May 19 '19 at 13:12
  • @Oliver I updated the question to include netstat -nltp output, thanks! Looking into removing the HTTPS redirection, thanks for that @ Lex Li – IdusOrtus May 19 '19 at 14:40

2 Answers2

17

The response from the server explains the cause. The GET request to http://127.0.0.1:5000 is redirected to https://123.123.123.123:5001/api/values, it's because the HTTPS redirection is active.

Solution: Open the Startup.cs file. Find the following line in the public void Configure(...) method and comment it out:

app.UseHttpsRedirection();

Here you can read more about it.

0

I'm in ASP .NET Core Web API, this changes worked for me

Some changes in /Program.cs. Just replace IHostBuilder to IWebHostBuilder and add UseUrls

public static IWebHostBuilder CreateHostBuilder(string[] args) =>
        Microsoft.AspNetCore.WebHost.CreateDefaultBuilder(args)
        .UseUrls("http://0.0.0.0:5000")
        .UseStartup<Startup>();