简体   繁体   English

NGINX反向代理背后的ASP.NET MVC

[英]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. 我目前正在从事一个项目,该项目要求我们当前的ASP.NET MVC5 Web应用程序之一位于客户端将控制的NGINX反向代理的后面。

I am brand new to NGINX so am lacking in knowledge. 我是NGINX的新手,所以知识不足。

The reverse proxy will be placed at a sub path. 反向代理将放置在子路径中。 (example below) (下面的示例)

http://localhost:9999/foo/bar/ HTTP://本地主机:9999 /富/酒吧/

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. 然后它将代理到我在本地设置的NGINX的MVC5应用程序的根目录(端口9998),以测试该站点将按预期运行。 We use absolute paths to our resources (hosted in an internal CDN) so all these load as expected. 我们使用到资源的绝对路径(托管在内部CDN中),因此所有这些负载均符合预期。

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. 当点击使用this.RedirectToAction()或@ html.ActionLink()等创建的任何控制器方法/页面链接时,就会开始出现问题。

The MVC application does not realise it is running behind a reverse proxy and chops that subpath out of its derived URL. MVC应用程序未意识到它正在反向代理后面运行,并且将子路径从其派生URL中砍掉。

So a redirect to a home controller looks like 所以重定向到家庭控制器看起来像

http://localhost:9999/home HTTP://本地主机:9999 /家

Instead of : 代替 :

http://localhost:9999/foo/bar/home HTTP://本地主机:9999 /富/酒吧/家

Does anyone have any ideas the counteract this? 有谁有想法抵消这一点? I can see .NET core has a workaround but cant see anything for MVC5. 我可以看到.NET core有解决方法,但看不到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. 我可以使用this.Redirect()并指定绝对路径,但是该应用程序很大并且可以在没有反向代理的其他情况下使用。

Can this be resolved through my NGINX configuration? 这可以通过我的NGINX配置解决吗? 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;
    }

}
}

you can use the UsePathBase code like: 您可以使用UsePathBase代码,例如:

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

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. 创建一个基本控制器,并重写ControllerBase.RedirectToAction方法。
  2. Add a base URL setting to your webconfig (or a db setting etc). 将基本URL设置添加到您的webconfig(或db设置等)。
  3. Create custom redirect result object and append baseurl to URL. 创建自定义重定向结果对象,并将baseurl附加到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. 使用IIS,在虚拟目录或子应用程序中运行该应用程序以匹配代理的位置。 MVC will then automatically correctly control all the routing without having to override any base methods. 然后,MVC将自动正确控制所有路由,而不必覆盖任何基本方法。

NB. 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. 请参见以下示例。

例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 asp.net mvc获取绝对URL(在代理之后) - asp.net mvc get absolute url (behind proxy) 反向代理后面的 ASP.Net Core 不使用 `X-Forwareded-For` - `X-Forwareded-For` is not used by ASP.Net Core behind reverse proxy ASP.NET Core 3.1 with google signin,nginx反向代理和docker资源暂时不可用 - ASP.NET Core 3.1 with google signin, nginx reverse proxy and docker Resource temporarily unavailable 在具有不同根相对路径的代理后面运行ASP.NET MVC应用程序 - Running ASP.NET MVC application behind a proxy with different root relative path 将ASP.NET MVC和反向代理与URL Rewrite v2结合使用 - Combining ASP.NET MVC and Reverse Proxy with URL Rewrite v2 How to set BaseUrl for Swagger in ASP.NET Core Web API using NSwag.AspNetCore (when hosted behind a reverse proxy) - How to set BaseUrl for Swagger in ASP.NET Core Web API using NSwag.AspNetCore (when hosted behind a reverse proxy) 在ASP.NET MVC 6中创建API代理 - Creating an API proxy in ASP.NET MVC 6 编辑: NGINX 反向代理 ASP.NET Core 5.0: localhost:5000 关闭连接但 www.google.com 工作 - EDIT: NGINX reverse proxy ASP.NET Core 5.0: localhost:5000 closes connection but www.google.com works 具有Apache和反向代理的Linux ASP.Net核心 - Linux ASP.Net core with Apache and reverse proxy 使用ASP.NET的简单反向代理,带有身份验证的C# - A simple reverse proxy using ASP.NET, C# with authentication
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM