locked
Deploying ASP.NET Core to a Linux VM with Nginx RRS feed

  • Question

  • User-1370514677 posted

    Hi everyone,

    I'm following this tutorial in order to deploy an ASP.NET Core app to a Linux VM :

    https://docs.microsoft.com/en-us/aspnet/core/host-and-deploy/linux-nginx?view=aspnetcore-3.1

    I'm using CentOS 8 Stream and Nginx.

    But I'm facing an issue.

    Indeed I have my app in my user folder :

    > /home/theo/WebApp
    > cd WebApp

    I've then added the HttpHeaders to the Startup.cs file :

    using Microsoft.AspNetCore.HttpOverrides;
    
    ...
    
    app.UseForwardedHeaders(new ForwardedHeadersOptions
    {
        ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto
    });

    And then built and published my app like so :

    > sudo dotnet build
    Success
    > sudo mkdir /srv/www/WebApp
    > sudo dotnet publish -o /srv/www/WebApp

    Also I have set up Nginx like the tutorial :

    server {
        listen        80;
        server_name   example.com *.example.com;
        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;
        }
    }

    And the following :

    > sudo nginx -t
    Success
    > systemctl reboot
    > sudo systemctl start nginx
    > sudo systemctl status nginx
    Active

    So until here, everything works.

    Then I get my VM IP :

    > ip a
    192.168.1.17

    Now, the issue is that when I try to run the published app I get a connection timed out error whether I use localhost:5000 or 192.168.1.17:5000 :

    If the app runs on the server but fails to respond over the Internet, check the server's firewall and confirm that port 80 is open. If using an Azure Ubuntu VM, add a Network Security Group (NSG) rule that enables inbound port 80 traffic. There's no need to enable an outbound port 80 rule, as the outbound traffic is automatically granted when the inbound rule is enabled.

    So I installed ufw and did

    > sudo ufw allow "Nginx Full"
    

    But even after having rebooted, it still doesn't work on Windows or my phone through WiFi.

    Do I have to use Kestrel absolutely for a local VM ?

    Saturday, December 19, 2020 8:17 PM

All replies

  • User-474980206 posted

    I don’t see where you started the asp.net core app, typically also with systemctl . It needs to be running for nginx to reverse proxy to it.

    Saturday, December 19, 2020 9:12 PM
  • User-1370514677 posted

    Hi @Bruce,

    I did the following in my VM :

    > cd /srv/www/WebApp
    > dotnet WebApp.dll
    (ASP.NET Core log info)
    Listening http://localhost:5001
    Listening http://localhost:5000

    And then tried 192.168.1.17:5000 inside Windows but it doesn't work.

    Saturday, December 19, 2020 10:26 PM
  • User-474980206 posted

    Typically port 5000 would be blocked by firewall rules. to check if the asp.net core app is  running in the vm and supports port 5000, from console in the vm

    curl http://localhost:5000
    Sunday, December 20, 2020 8:32 PM
  • User-1370514677 posted

    Hi @bruce,

    Well I did the following :

    > cd /srv/www/WebApp
    > sudo dotnet WebApp.dll
    => Switch from tty to tty2 > curl http://localhost:5000 No errors, empty output

    Sunday, December 20, 2020 9:49 PM
  • User-474980206 posted

    Should of had html for the body. Use the -v switch to see the headers

    Monday, December 21, 2020 3:34 PM
  • User-1370514677 posted

    Hi @bruce,

    I get this :

    * Rebuilt URL to: http://localhost:5000/
      % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                     Dload  Upload   Total   Spent    Left  Speed
    
      0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0*   Trying ::1...
    * TCP_NODELAY set
    * Connected to localhost (::1) port 5000 (#0)
    > GET / HTTP/1.1
    > Host: localhost:5000
    > User-Agent: curl/7.61.1
    > Accept: */*
    > 
    < HTTP/1.1 500 Internal Server Error
    < Date: Tue, 22 Dec 2020 13:32:55 GMT
    < Server: Kestrel
    < Content-Length: 0
    < 
    
      0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
    * Connection #0 to host localhost left intact

    Tuesday, December 22, 2020 1:33 PM
  • User-474980206 posted

    Pretty clear you app to having an issue. What do your error logs show. You could enable the dev error page if you haven’t yet added proper logging

    Tuesday, December 22, 2020 3:37 PM