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;
}
}
}
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)
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.
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.
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.
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 :
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With