Hosting asp.net core web apps in nginx

bartron on 1/1/2021

So let's say that we have an asp.net core application published and ready to deploy, and that we also have dotnet core installed on the server. This will take us through deploying the application and hosting it behind nginx in a very basic way, without supervisor or other managed servers; just the bare bones solution.

We'll assume that we've taken the published output from your .net core application and placed it under /var/www

I like to create a cron job to start the app from system start. You can either make a script or call something like this:

(cd /var/www && dotnet /var/www/mysite.dll)

 

Once you run that, you're asp.net core app will be running. We next need to configure nginx to forward requests to your app. In your nginx site config, you'll want something like this:

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

Tags