Introduction
ASP.NET is a popular web application framework developed by Microsoft. ASP.NET applications can also be hosted on Linux using open-source web servers like Nginx, and Apache. In a previous article, we covered how to host ASP.NET Core on Linux with Nginx. In this article, we will discuss how to host multiple ASP.NET applications on the same Linux server using Nginx.
Prerequisites
Before we dive into the details, Prerequisites must be installed on the Linux server to ensure successful operation.
- .NET Core SDK
- Nginx web server
Install Required Packages
Do check out the previous article. Including Package Installation and Publishing.
Create A Service File
Now that we have transferred our project and runtime is installed on the server, we will need to create a service file which will help the project to run on localhost:5001
port. And restart the process if the app is crashed or the server was rebooted. Create service file by running the following command:
sudo nano /etc/systemd/system/testapi2.service
You can choose any service file name, I have choosed testapi
. Copy the following configuration in and paste it:
[Unit]
Description= Test API 2
[Service]
WorkingDirectory=/var/www/TestAPI2
ExecStart=/usr/bin/dotnet /var/www/TestAPI2/TestAPI2.dll
Restart=always
# Restart service after 10 seconds if the dotnet service crashes:
RestartSec=10
SyslogIdentifier=testapi2
User=www-data
Environment=ASPNETCORE_ENVIRONMENT=Production
Environment=DOTNET_PRINT_TELEMETRY_MESSAGE=false
Environment=ASPNETCORE_URLS=http://127.0.0.1:5001
[Install]
WantedBy=multi-user.target
Here in the service file the more you adding change the port number to an unused one. Now we will enable, start, restart the service file and check the status of the service file.
sudo systemctl enable testapi2.service
sudo systemctl start testapi2.service
sudo systemctl status testapi2.service
sudo systemctl restart testapi2.service
Configure Nginx
So we have all the required .NET packages and the Nginx server installed. Now you’ll need to configure it to route requests to your ASP.NET Core application. Create a new Nginx configuration file at /etc/nginx/sites-available/testapi
2 (replace testapi
2 with a name that makes sense for your application) with the following contents: I have shared Nginx config with SSL if you don’t have SSL remove the 443 parts from the config file.
server {
listen 443 ssl; # managed by Cloudflare
ssl_certificate /home/serverguy/keys/ssl/testapi2.pem;
ssl_certificate_key /home/serverguy/keys/ssl/testapi2.key;
server_name testapi2.serverguy.dev www.testapi2.serverguy.dev;
add_header X-Frame-Options DENY;
add_header X-Content-Type-Options nosniff;
location / {
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 $http_host;
proxy_cache_bypass $http_upgrade;
}
}
server {
if ($host = www.testapi2.serverguy.dev) {
return 301 https://$host$request_uri;
}
if ($host = testapi2.serverguy.dev) {
return 301 https://$host$request_uri;
}
listen 80;
listen [::]:80;
server_name testapi2.serverguy.dev www.testapi2.serverguy.dev;
return 404;
}
Enable the Nginx configuration
Now we will have to enable the Nginx config file by following this command:
sudo ln -s /etc/nginx/sites-available/testapi2.conf /etc/nginx/sites-enabled
Then, test the Nginx configuration by running the following command:
sudo nginx -t
If the configuration is valid, restart Nginx by running the following command:
sudo systemctl restart nginx
That’s it! Your ASP.NET Core application should now be accessible through Nginx at the URL https://testapi.serverguy.dev
.
Hope this will be helpful, I tried to keep the article as simple as possible. There might be some mistakes or extra words, pardon them and feel free to comment if you are stuck at any point. If you are planning to host a single ASP.NET application follow this article.