ASP.NET MVC behind NGINX reverse proxy

Robert Kitching

I am currently working on a project that requires one of our current ASP.NET MVC5 web applications to sit behind a NGINX reverse proxy that the client will control.

I am brand new to NGINX so am lacking in knowledge.

The reverse proxy will be placed at a sub path. (example below)

http://localhost:9999/foo/bar/

This will then proxy to the root of the MVC5 application (port 9998) I have set up NGINX locally to test that the site will work as expected. We use absolute paths to our resources (hosted in an internal CDN) so all these load as expected.

My Issue - The reverse proxy is working correctly and displaying the root page of the application. The problems start to arise when hitting any controller methods/page links that have been created using this.RedirectToAction() or @html.ActionLink() etc.

The MVC application does not realise it is running behind a reverse proxy and chops that subpath out of its derived URL.

So a redirect to a home controller looks like

http://localhost:9999/home

Instead of :

http://localhost:9999/foo/bar/home

Does anyone have any ideas the counteract this? I can see .NET core has a workaround but cant see anything for MVC5. I can use this.Redirect() and specify the absolute path but the application is large and used in other scenarios without the reverse proxy.

Can this be resolved through my NGINX configuration? I have included my config below :

#user  nobody;
worker_processes  1;

events {
worker_connections  1024;
}

http {
include       mime.types;
default_type  application/octet-stream;
sendfile        on;


keepalive_timeout  65;

server {
    listen       9999;
    server_name  localhost;

    location /foo/bar/ {

        rewrite ^/foo/bar/(.*)$ /$1 break;
        proxy_pass  http://127.0.0.1:9998/;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }

}
}
Robert Kitching

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Set Virtual Path to ASP NET Core MVC Application behind nginx reverse proxy

ASP.NET MVC application behind reverse proxy - using wrong host name after auth

grafana behind a nginx reverse proxy

Flask - SocketIO hidden behind reverse proxy with Nginx

Minio console not accessible behind nginx reverse proxy

Kubernetes Ingress running behind nginx reverse proxy

How to host websocket behind nginx reverse proxy

Configuration for Piwik behind nginx reverse proxy with rewrite

asp.net mvc get absolute url (behind proxy)

Hosting multiple ASP NET Core sites on unbuntu and nginx as reverse proxy

Angular App running on nginx and behind an additional nginx reverse proxy

`X-Forwareded-For` is not used by ASP.Net Core behind reverse proxy

ASP.NET Core app with Azure AD authentication behind a reverse proxy is setting the wrong redirect_uri

Docker Flask app behind nginx reverse-proxy sending 404

Wordpress on Docker behind nginx reverse proxy using SSL

Running Go server behind Nginx Reverse Proxy with SSL

Server behind nginx reverse proxy ignores relative path in URL

Jenkins behind nginx reverse proxy doesn't redirect in all situations

nodejs app accessible on port 3000 behind nginx reverse proxy

Jenkins using JNLP behind nginx SSL reverse proxy

Handling flask url_for behind nginx reverse proxy

Nexus docker repository behind nginx reverse-proxy

Nginx as a reverse proxy behind AWS ALB (self-signed)

Debugging node.js microservices behind nginx reverse proxy

iccube behind reverse proxy

Keycloak behind reverse proxy

Request hangs for nginx reverse proxy to an ASP.NET 5 web application on docker

ASP.NET 5 behind nginx

AppHarbor's Reverse Proxy causing issues with SSL and app.UseOAuthBearerTokens ASP.NET MVC 5