Running .NET Core 2 MVC Application in NGINX Location

NGINX Configuration

In the following paragraphs, we suppose the ASP.NET MVC application is accessible from the URL: http://example.com/mvcmovie

In the /etc/nginx/sites-available/example.com configuration file, set the following location.

location /mvcmovie/ {
   proxy_pass http://127.0.0.1:5000/mvcmovie/;
   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;
}

The slash characters at the end of the location name and “proxy_pass” URL are necessary.

ASP.NET MVC Code

In the Configure method of the Startup class (Startup.cs file), add app.UsePathBase with the name of the location set up in the NGINX configuration file. Do not add a slash character at the end.

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
   app.UsePathBase("/mvcmovie");
   app .UseForwardedHeaders(newForwardedHeadersOptions
   {  ForwardedHeaders=ForwardedHeaders.XForwardedFor|ForwardedHeaders.XForwardedProto
   });
   if (env.IsDevelopment())
   {
      app.UseDeveloperExceptionPage();
   }
   else
   {
      app.UseExceptionHandler("/Home/Error");
   }
   app.UseStaticFiles();
   app.UseMvc(routes =>
   {
      routes.MapRoute(
         name: "default",
         template: "{controller=Home}/{action=Index}/{id?}");
   });
}

 

Leave a Reply