Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC behind NGINX reverse proxy

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;
    }

}
}
like image 835
Robert Kitching Avatar asked Nov 08 '22 04:11

Robert Kitching


People also ask

Does Kestrel need reverse proxy?

Kestrel can be used by itself or with a reverse proxy server. A reverse proxy server receives HTTP requests from the network and forwards them to Kestrel. Examples of a reverse proxy server include: Internet Information Services (IIS)

What is reverse proxy in asp net core?

A reverse proxy is a special type of proxy server that hides the target server to the client. The client requests a resource to the proxy server which retrieves it from another server and provides it to the client. In this case, the client has no idea that the resource comes from another server.

What is the difference between ASP NET and ASP NET core?

Just like ASP.NET, ASP.NET Core is based on the Model-View-Controller framework, like most web development frameworks; ASP.NET Core has robust Cloud support, and it supports modular architecture better than ASP.NET does.

What is reverse proxy with example?

A reverse proxy server is a type of proxy server that typically sits behind the firewall in a private network and directs client requests to the appropriate backend server. A reverse proxy provides an additional level of abstraction and control to ensure the smooth flow of network traffic between clients and servers.


1 Answers

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

like image 102
Robert Kitching Avatar answered Nov 11 '22 16:11

Robert Kitching