IIS URLrewrite:如何为内部重定向保留原始主机名?

IIS URLrewrite: How to preserve original host name for internal redirects?

我在一台服务器上有一个 Web 应用程序 运行,并在另一台服务器上使用带 rewriteURL 的 IIS 作为反向代理。

我配置了传入规则,将反向代理地址改写为web应用服务器的地址。这很好用。

但是,该应用程序允许用户下载一些内容并将他们重定向到下载地址。现在,用户被转发到 Web 应用程序服务器的本地 IP 地址,而不是反向代理的 public 地址。

我明白了,我也需要编辑出站规则来捕捉这个,但我不知怎么弄错了。

我按照此处的说明操作 https://docs.microsoft.com/en-us/iis/extensions/url-rewrite-module/modifying-http-response-headers

我当前的重写规则如下所示

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <rewrite>
            <rules>
                <rule name="er-platform" stopProcessing="true">
                    <match url="^er-platform(.*)" />
                    <conditions>
                        <add input="{CACHE_URL}" pattern="^(https?)://" />
                    </conditions>
                    <action type="Rewrite" url="{C:1}://192.168.80.6:8443/{R:0}" />
                    <serverVariables>
                        <set name="ORIGINAL_HOST" value="{HTTP_HOST}" />
                    </serverVariables>
                </rule>
            </rules>
            <outboundRules>
                <rule name="er-platform" preCondition="IsRedirection" enabled="true">
                    <match serverVariable="RESPONSE_LOCATION" pattern="^(https?)://[^/]+/(.*)" />
                    <conditions logicalGrouping="MatchAny">
                        <add input="{ORIGINAL_HOST}" pattern=".+" />
                    </conditions>
                    <action type="Rewrite" value="{R:1}://{ORIGINAL_HOST}/{R:2}" />
                </rule>
                <preConditions>
                    <preCondition name="IsRedirection">
                        <add input="{RESPONSE_STATUS}" pattern="3\d\d" />
                    </preCondition>
                </preConditions>
            </outboundRules>
        </rewrite>
    </system.webServer>
</configuration>

我在尝试下载内容时仍然被重定向到本地 IP。 我需要如何更改规则才能正确执行此操作?

谢谢,

托马斯

我认为您的出站规则没有错误。我对它失败的原因有几个猜测。

首先:您的入站规则 必须 匹配才能使出站规则生效。这是因为 ORIGINAL_HOST 在入站规则执行时被捕获。现在,您的入站规则匹配 URL 与 ^er-platform(.*)。我将假设这是一个反向代理,它正在工作,因为如果它不工作,您将无法开始下载。

其次:输出规则仅在 3xx 代码上触发。这不是重定向的唯一方法。您可能正在使用 JavaScript 进行重定向。例如。 `window.location = 'http://wrongaddress'。在这种情况下,您的出站规则将不起作用。

后续调试步骤: 为入站规则打开日志记录。 您的日志将写入 %SystemDrive%\inetpub\logs\LogFiles\。验证是否符合入站规则。

验证线路上发生了什么: Fiddler 是了解线路上实际发生的事情的好工具。使用它来确认 URL 重写规则应该命中。 IE。请求 URL 匹配 ^er-platform(.*) 并且响应代码在 300 中。

除了 Fiddler,您还可以使用 Chrome Developer Tools Network 选项卡。打开 'Preserve log' 复选框,以便在您被重定向后它不会被清除。验证您认为应该发生的事情确实发生了。