IIS Rewrite ARR配置子站点反向代理后根URL无法指向旧应用咨询
问题根因排查
你当前的配置存在两个核心问题导致根路径代理失效:
- 根路径访问触发目录判断逻辑
访问www.mysite.com/时,IIS会将请求识别为站点根目录,匹配Legacy规则中的IsDirectory条件后返回true,由于你设置了negate="true"(排除存在的目录),整个规则条件不满足,因此不会执行根路径的代理转发。 - 规则顺序不合理
HTTP转HTTPS规则放在/app1、/app2代理规则之后,导致HTTP访问子站点时不会跳转到HTTPS,直接走HTTP协议转发到后端,不符合全站HTTPS的配置预期。
修复方案
调整规则顺序,同时拆分Legacy代理规则单独处理根路径,修正后的完整配置如下:
<?xml version="1.0" encoding="UTF-8"?> <configuration> <system.webServer> <rewrite> <rules> <!-- 全站HTTP转HTTPS放到最前面 --> <rule name="Redirect HTTP to HTTPS" stopProcessing="true"> <match url="(.*)" /> <conditions> <add input="{HTTPS}" pattern="off" /> </conditions> <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="Permanent" /> </rule> <rule name="Reverse Proxy to App1" stopProcessing="true"> <match url="^app1/?(.*)" /> <!-- 优化正则,兼容不带斜杠的/app1访问 --> <conditions logicalGrouping="MatchAll"> <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" /> <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" /> </conditions> <action type="Rewrite" url="http://pc1.local/{R:1}" /> </rule> <rule name="Reverse Proxy to App2" stopProcessing="true"> <match url="^app2/?(.*)" /> <!-- 优化正则,兼容不带斜杠的/app2访问 --> <conditions logicalGrouping="MatchAll"> <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" /> <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" /> </conditions> <action type="Rewrite" url="http://pc2.local/{R:1}" /> </rule> <!-- 单独处理根路径代理,不校验文件/目录存在性 --> <rule name="Reverse Proxy to Legacy Root" stopProcessing="true"> <match url="^$" /> <action type="Rewrite" url="http://legacypc.local/" /> </rule> <!-- 处理旧应用非根路径的其他请求 --> <rule name="Reverse Proxy to Legacy App" stopProcessing="true"> <match url="(.*)" /> <conditions logicalGrouping="MatchAll"> <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" /> <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" /> </conditions> <action type="Rewrite" url="http://legacypc.local/{R:1}" /> </rule> </rules> <outboundRules> <rule name="Add STS headers to HTTPS response"> <match serverVariable="RESPONSE_Strict_Transport_Security" pattern=".*"/> <conditions> <add input="{HTTPS}" pattern="on"/> </conditions> <action type="Rewrite" value="max-age=31536000"/> </rule> <rule name="Ensure secure Cookies" preCondition="Missing secure cookie"> <match serverVariable="RESPONSE_Set_Cookie" pattern=".*" negate="false" /> <action type="Rewrite" value="{R:0}; secure" /> </rule> <preConditions> <preCondition name="Missing secure cookie"> <add input="{RESPONSE_Set_Cookie}" pattern="." /> <add input="{RESPONSE_Set_Cookie}" pattern="; secure" negate="true" /> </preCondition> </preConditions> </outboundRules> </rewrite> <defaultDocument enabled="false" /> </system.webServer> </configuration>
注意事项
配置更新后需要清空浏览器缓存(301重定向是永久缓存的),再重新测试访问即可。
内容的提问来源于stack exchange,提问作者Richard Plaatjes
相关产品推荐
相关产品推荐

