Host ASP.NET Core application on Linux with Nginx

Unofficial Content

This guide explains setting up a production-ready ASP.NET Core application generated with Genexus .Net Core generator on Linux (Ubuntu 14.04) with Nginx as a reverse proxy.

Prerequisites for .NET Core on Linux

See linux prerequisites for other linux versions.

Install .NET Core for Ubuntu 14.04

1. Register the Microsoft Product key as trusted.

curl https://packages.microsoft.com/keys/microsoft.asc | gpg --dearmor > microsoft.gpg
sudo mv microsoft.gpg /etc/apt/trusted.gpg.d/microsoft.gpg

2. Set up the desired version host package feed.

sudo sh -c 'echo "deb arch=amd64 https://packages.microsoft.com/repos/microsoft-ubuntu-trusty-prod trusty main" > /etc/apt/sources.list.d/dotnetdev.list' 
sudo apt-get update

3. Install .NET Core.

sudo apt-get install dotnet-sdk-2.1.3

5.Run the dotnet --version command to prove the installation succeeded.

dotnet --version

Copy over the app

Deploy your application using Build -> Deploy Application and then and copy it to the target linux.

From command line positioned in web directory (or equivalent) run:

dotnet bin/GxNetCoreStartup.dll

Your application can be tested at for example: http://localhost:5000/Webpanel1.aspx (see dotnet command output to check port)

Configure Nginx as a reverse proxy server

1.Install Nginx

sudo apt-get install nginx

2. Start it by running:

sudo service nginx start

3.Configure Nginx

To configure Nginx as a reverse proxy to forward requests to our ASP.NET Core app, modify /etc/nginx/sites-available/default. Open it in a text editor, and replace the contents with the following:

server {
    listen 80;
    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 $http_host;
        proxy_cache_bypass $http_upgrade;
    }
}

This Nginx configuration file forwards incoming public traffic from port 80 to port 5000.

Once the Nginx configuration is established, run sudo nginx -t to verify the syntax of the configuration files. If the configuration file test is successful, force Nginx to pick up the changes by running sudo nginx -s reload

 

Multiple applications on one domain with Nginx

 

Let's suppose you have two applications SmallDemo0 and SmallDemo1 (*) and want to host both on the same server and access at: http://localhost/SmallDemo0/WebPanel1.aspx and http://localhost/SmallDemo1/WebPanel1.aspx.

(*) For example /home/deploy/dev/dotnet/SmallDemo0 and /home/deploy/dev/dotnet/SmallDemo1

1. Configure nginx to handle boths ports. Open /etc/nginx/sites-available/default in a text editor and replace the contents with the following:

server {
    listen 80;
    # Make site accessible from http://localhost/
    server_name localhost;
     location /SmallDemo0/ {
           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;
    }
    location /SmallDemo1/ {
           proxy_pass http://localhost:5001/;
           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;
    } 
}

Once the Nginx configuration is established, run sudo nginx -t to verify the syntax of the configuration files. If the configuration file test is successful, force Nginx to pick up the changes by running sudo nginx -s reload

Or start it with: 

sudo service nginx start

 

2. Run your apps with the following commands:

/home/deploy/dev/dotnet/SmallDemo1$ dotnet bin/GxNetCoreStartup.dll "" /home/deploy/dev/dotnet/SmallDemo1 5001

/home/deploy/dev/dotnet/SmallDemo0$ dotnet bin/GxNetCoreStartup.dll "" /home/deploy/dev/dotnet/SmallDemo0 5000


Test your sites at http://localhost/SmallDemo0/WebPanel1.aspx  and http://localhost/SmallDemo1/WebPanel1.aspx

 

 

Another valid configuration using a different BaseURL (but getting the same final urls) could be:

1. Configuration por nginx

server {
    listen 80;
    # Make site accessible from http://localhost/
    server_name localhost;
     location /SmallDemo0/ {
           proxy_pass http://localhost:5000/SmallDemo0/;
           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;
    }
    location /SmallDemo1/ {
           proxy_pass http://localhost:5001/SmallDemo1/;
           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;
    } 
}

2. Run commands:

/home/deploy/dev/dotnet/SmallDemo1$ dotnet bin/GxNetCoreStartup.dll "SmallDemo1" /home/deploy/dev/dotnet/SmallDemo1 5001

/home/deploy/dev/dotnet/SmallDemo0$ dotnet bin/GxNetCoreStartup.dll "SmallDemo0" /home/deploy/dev/dotnet/SmallDemo0 5000

3. Test your sites at http://localhost/SmallDemo0/WebPanel1.aspx  and http://localhost/SmallDemo1/WebPanel1.aspx

 

 

GxNetCoreStartup.dll has 3 parameters: 

1. Virtual Dir (or BaseURL)

2. Physical application path

3. Port

default values are:

1. Empty

2. Current Directory where dotnet command is executed

3. 80