ASP.NET MVC behind NGINX reverse proxy ASP.NET MVC behind NGINX reverse proxy nginx nginx

ASP.NET MVC behind NGINX reverse proxy


I have now found two solutions for the above...

I have opted for solution two as it requires no code changes but have successfully tested both solutions

Solution One

Apologies, I dont have access to the working test code on this machine but it goes something like the below :

  1. Create a base controller and override the ControllerBase.RedirectToAction method.
  2. Add a base URL setting to your webconfig (or a db setting etc).
  3. Create custom redirect result object and append baseurl to URL. Return custom result object from overridden method.

    protected override RedirectToRouteResult RedirectToAction(string actionName, string controllerName, RouteValueDictionary routeValues)

Solution Two

Using IIS, run the application within a virtual directory(s) or child application to match the location of the proxy. MVC will then automatically correctly control all the routing without having to override any base methods.

NB. You will need be careful with any relative paths/links as with any proxy.

I am currently using this method in production without any problems. See below example.

Example


you can use the UsePathBase code like:

 app.UsePathBase($"ApplicationFolder").UseMvc(routes =>                {                     routes.MapRoute(                           "default", GetDefaultRoutePath());                });