top of page

Host ASP.NET Core on Linux with Nginx

Updated: Jul 5

Today we are going to tell in more detail how to host an ASP.NET Core application on Ubuntu (open source Linux distribution) using Ngninx.


Before that, let's get familiar with Nginx and what it is.


Nginx is a popular web server known for its high performance, stability, rich features and easy configuration. It was created by Igor Sisoev and was originally released in 2004. Since then, Nginx has become one of the most used web servers in the world.

Key features of Nginx:

  1. Servicing static files: Nginx is extremely efficient at serving static files such as HTML, CSS, JavaScript, and images.

  2. Reverse proxy: Nginx can act as a reverse proxy server that redirects HTTP requests to one or more backend servers. This makes it ideal for load balancing and traffic management.

  3. Load balance: Nginx can load balance between multiple servers, which improves the performance and resiliency of web applications.

  4. Caching: Nginx provides powerful caching capabilities, which can significantly speed up service for frequently requested resources.

  5. Security: Nginx offers a variety of security mechanisms, including SSL/TLS support, access restriction by IP addresses, and more.

Benefits of Nginx


  1. High performance: ginx is designed to handle multiple concurrent connections using an asynchronous, event-driven model, making it much more efficient than traditional web servers like Apache.

  2. Lightweight and stable: Nginx uses minimal system resources, making it suitable for use on servers with limited resources.

  3. Easy configuration: Nginx configuration files are easy to read and write, making it easy to set up and manage.

  4. Broad support: Nginx supports multiple protocols and technologies, including HTTP/2, WebSocket, and gRPC.


After making a brief introduction about Nginx we will tell in more detail how to host an ASP.NET Core application on Ubuntu (open source Linux distribution) using Ngninx.

First we need to prepare our operating system (Ubuntu) to be able to work with it.


Step 1: Install the .NET SDK


wget https://download.visualstudio.microsoft.com/download/pr/1fcd238e-7c75-4b0c-a52a-f8dfbcdcb747/e99eb0b66d0c17ea27e9be0046d8d62a/dotnet-sdk-8.0.100-linux-x64.tar.gz 

mkdir -p $HOME/dotnet && tar zxf dotnet-sdk-8.0.100-linux-x64.tar.gz -C $HOME/dotnet

export DOTNET_ROOT=$HOME/dotnet

export PATH=$PATH:$HOME/dotnet

Step 2: Create a .NET 8 application


We can create a new .NET 8 application or use an existing one. In this case, we will create a new application called WebApp.


dotnet new webapp -o WebApp

cd WebApp

dotnet publish --configuration Release --output ./publish --runtime linux-x64

Step 3: Install Nginx


Install Nginx on our machine. You can do it through the package manager of our operating system.

sudo apt update

sudo apt install nginx

Step 4: Configure Nginx


Creating new configuration file for our .NET application.

sudo nano /etc/nginx/sites-available/webapp

Add the following configuration code to the configuration file:


server {
              listen 80;
              server_name your_domain_or_IP;
              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;
				}
		}

Reload Nginx to apply the new configuration:


sudo systemctl reload nginx’

Step 5: Create a systemd service to automatically start the application


sudo nano /etc/systemd/system/webapp.service

Add the following code to the new file:

[Unit] 

Description=Example .NET Web API Application running on Nginx

[Service] 

WorkingDirectory=/path/to/your/app/publish

ExecStart=/path/to/your/dotnet WebApp.dll

Restart=always

# Restart service after 10 seconds if the dotnet service crashes: RestartSec=10 KillSignal=SIGINT

SyslogIdentifier=dotnet-example

User=www-data

Environment=ASPNETCORE_ENVIRONMENT=Production Environment=DOTNET_PRINT_TELEMETRY_MESSAGE=false 

[Install] 

WantedBy=multi-user.target

Start and activate the service:



sudo systemctl enable webapp.service

sudo systemctl start webapp.service


After following these steps, our .NET application should be accessible via Nginx. You can verify that everything is working properly by opening a web browser and entering the domain or IP address of the server.






Amexis Team

22 views0 comments

Recent Posts

See All

Comments


bottom of page