NGINX is an open-source web server, offering a wide range of functionalities to serve different purposes. Managing high concurrent requests has made it a developer favorite for hosting applications. NGINX can also act as a load balancer, reverse proxy etc.
We can think of a reverse proxy as a middleman who sits between clients and the internet. As requests come in from clients, it swiftly passes them to backend servers, seamlessly distributing the workload.
In this article, we will walk through the process of deploying a .NET Application to Nginx and learn about the mechanism of load balancing. We will use Ubuntu as a linux distribution and .NET 8 for our app.
Hosting .NET Apps with NGINX
Hosting .NET apps with NGINX can improve the performance and reliability of your applications. Here is a step-by-step guide to help you understand the basics of setting up your hosting environment.
Install .NET Module on Linux(Ubuntu)
We will be setting up the environment for the first time hosting. As we are about to deploy and host a .NET application to linux, we need to install a .NET environment.
We can install both .NET SDK and .NET runtime. This will ease our deployment process if we are planning to deploy more .NET applications. Without the runtime .NET app will not run. You can install a runtime version depending on your application .NET version.
We will also install https support. Though we will not add SSL in our localhost environment.
Update and install HTTPS tools
sudo apt-get update
sudo apt-get install -y apt-transport-https
Installing .NET SDK
sudo apt-get update
sudo apt-get install -y dotnet-sdk-8.0
Install .NET runtime
sudo apt-get install aspnetcore-runtime-8.0
DOTNET installation status
dotnet --info
whereis dotnet
Build and Deploy App to Linux
Create folder for application files
We will create a folder inside /var/www for hosting our application. Also we will give read and write permission in this directory.
cd /var/www # navigating to the location
sudo mkdir blazorapp # This will create a folder named blazorapp
sudo chmod 755 blazorapp # This will grant RW permission
Copy application DLL files
You can use any FTP/SFTP client to copy files to your desired location. For example you can use WinSCP, Filezilla. For localhost testing we will directly copy the files to the location.
Config NGINX for Deployment
So for this final part we have to install and config for NGINX deployment. We have to first install nginx and then we have to create a service that will always run for our app. This service will ensure our apps keep running without any disturbance.
For long-term stability and optimal performance, hiring .NET developers who are skilled in configuring NGINX is essential.
Installing NGINX
sudo apt-get install nginx
sudo apt-get update
Check the status of Nginx
service status nginx
This will give results like below if the service is running.
Fig: NGINX status
If Nginx service is not running, we will start it by the command below.
sudo service nginx start
We will use our nginx as a reverse proxy. To config Nginx as a reverse proxy that will forward the request to our app we have to update the default configuration. We will use the below command and update the file.
vim /etc/nginx/sites-available/default # we can use vim/nano as our preference for editing text
Replace the content of the default file with this text. This will create a config for the HTTP site.
server {
listen 80 default_server;
listen [::]:80 default_server;
# SSL configuration
#
# listen 443 ssl default_server;
# listen [::]:443 ssl default_server;
#
# Note: You should disable gzip for SSL traffic.
# See: https://bugs.debian.org/773332
#
# Read up on ssl_ciphers to ensure a secure configuration.
# See: https://bugs.debian.org/765782
#
root /var/www/html;
index index.html index.htm index.nginx-debian.html;
server_name _;
location / {
proxy_pass http://127.0.0.1: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;
}
}
Validate the config
After changing the file we have to validate the config changes. It is recommended to validate the changes every time we make any change of the config files.
sudo nginx -t
Fig : validate status
We will get a syntax message if the configuration is okay. Otherwise it will generate a fail message.
Register Service
Now for running the application, we have to create a service for our application. Ubuntu and other linux distributions like Centos, Fedora contain a service manager named systemd which is responsible for deciding what service should run. To run our app we have to tell systemd how to handle it during boot-up or shutdown.
Now let us create a service file for it.
nano /etc/systemd/system/blazor-app.service
Update the file with this configuration.
[Unit]
Description=Blazor app is running
[Service]
WorkingDirectory=/var/www/blazorapp
ExecStart=/usr/bin/dotnet /var/www/blazorapp/BlazorApp.dll
Restart=always
RestartSec=10
KillSignal=SIGINT
SyslogIdentifier=dotnet-web-app
User=www-data
Environment=ASPNETCORE_ENVIRONMENT=Production
[Install]
WantedBy=multi-user.target
This will create a new service in systemd named blazor-app.service. Keep in mind for this to work you have to exactly give the directory value in both ‘WorkingDirectory’ and ‘ExecStart’. Otherwise it will not work.
Starting the service
We will restart the nginx.
sudo systemctl restart nginx
Now we will enable the service we have created and start it. After that we will check the status of it.
sudo systemctl enable blazor-app.service
sudo systemctl start blazor-app.service
sudo systemctl status blazor-app.service
If there are no issues, then we will see that service is running successfully. That means our application is ready for us to see. As we have defined our proxy pass to http://127.0.0.1:5000/.
We can see our blazor app here.
Fig : app.service status
Fig: Blazor app hosted in nginx
Hosting Note
You may face some difficulties depending on permission.
You may need to provide permission that is needed if you face any permission related issue. For permission related information you may look into here https://shorturl.at/oqtAN
Also you see logs of systemd if any error occurred during hosting. Use the below command
journalctl -xe