使用ARR的IIS反向代理与目录级别有问题

Sha*_*een 7 asp.net iis asp.net-mvc reverse-proxy url-rewriting

我正在设置IIS 7.5来为我的站点的子目录执行反向代理.

这是web.config url-rewrite:

<clear />
<rule name="Reverse Proxy to Community" stopProcessing="true">
<match url="^community/qa/(.*)" />
<action type="Rewrite" url="http://xxx.xxx.xxx.xxx/{R:1}" logRewrittenUrl="true" />
</rule>
Run Code Online (Sandbox Code Playgroud)

ip地址指向一个带有apache和django站点的联网linux盒子.

我想要的
所有请求/ community/qa/*被重定向到指定的内部IP.

会发生什么
/ community/qa/ - 给出404(在主IIS服务器上)
/ community/qa/questions/ - 给出404(在主IIS服务器上)
- 但是 -
/ community/qa/questions/ask/Works !!!
/ community/qa/questions/unanswered/Works !!

所以它似乎适用于从起点开始的2个子目录的所有URL.

这对我来说似乎很奇怪,我无法弄明白.

在此先感谢您的帮助.

Dao*_*Dao 14

我很确定在你的情况下问题是在UrlRoutingModule设置中.如果您查看Ordered View中的IIS模块设置,您会看到它UrlRoutingModule位于更高的位置RewriteApplicationRequestRouting放置了模块.这意味着,如果您的应用程序中有ASP.NET MVC的Route-setup.此设置将影响服务器拦截它们并将它们转发到MVC-Route-Handler的请求,而不允许反向代理执行它的工作.例如,如果您有这样的常见路由设置:

routes.MapRoute(
  "Default", // Route name
  "{controller}/{action}/{id}", // URL with parameters
  new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
Run Code Online (Sandbox Code Playgroud)

在你的情况下/community/qa/,/community/qa/questions/并不会工作,因为它将匹配给定的Url模式,并将被解释为:

/ community/qa/---> Controller =" community ",Action =" qa "

/ community/qa/questions/---> Controller =" community ",Action =" qa ",参数:Id =" questions "

如果你没有这样的控制器和动作,你将得到Http 404 Not Found.

/community/qa/questions/ask/并且/community/qa/questions/unanswered/可以工作,因为它们与您系统中的任何UrlRouting模式都不匹配.

如此简单的解决方案是添加您的UrlRouting配置(当Web应用程序启动时)忽略您的网址规则:

routes.IgnoreRoute("community/qa/{*pathInfo}");
Run Code Online (Sandbox Code Playgroud)