Photo by Clark Tibbs on Unsplash

Dotnet Core Api with Nginx load balancer in Linux

Go to VS PMC or open CMD in app directory and exec following command

dotnet publish


Copy published files to Linux

I assume Nginx load balancer already configured.

Load balancer config

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 $host;
proxy_cache_bypass $http_upgrade;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;


restart nginx

systemctl restart nginx

To keep api running

Create service

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

Service configuration

Description= NET Core application on Ubuntu

ExecStart=/usr/bin/dotnet /var/www/html/Example/Example.Api.dll
RestartSec=10 # Restart service after 10 seconds if dotnet service crashes


Service commands

sudo systemctl enable Example.service
sudo systemctl start Example.service
sudo systemctl status Example.service

Do not forget to follow me :)
Congratz. You did it.