【问题标题】:IIS reverse proxy rewrite encoded url segement (HTTP redirect)IIS 反向代理重写编码的 url 段(HTTP 重定向)
【发布时间】:2020-09-05 10:04:16
【问题描述】:

我使用 IIS 10 作为反向代理来托管网页(3 方,我无法修改代码)。

本地:localhost:26982

外部:sub.domain.com

外部请求被重定向到本地实例。为此,我使用以下重写规则(web.config):

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>      
        <rewrite>
            <rules>
                <clear />
                <rule name="ReverseProxyInboundRule1" enabled="true" stopProcessing="true">
                    <match url="(.*)" />
                    <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
                        <add input="{HTTP_HOST}" pattern="^sub.domain.com$" />
                    </conditions>
                    <action type="Rewrite" url="http://localhost:26982/{R:1}" />
                </rule>
            </rules>
            <outboundRules>

            </outboundRules>
        </rewrite>
        <httpProtocol>
            <customHeaders>
                <remove name="X-Powered-By" />
                <add name="strict-transport-security" value="max-age=16070400" />
            </customHeaders>
        </httpProtocol>
        <security>
            <requestFiltering allowDoubleEscaping="true" />
        </security>
    </system.webServer>
</configuration>

当用户尝试登录页面时,它会重定向到 OpenID 提供程序(Steam - 使用 HTTP302 - 标头中的返回 URL)。不幸的是,路径中编码的return url仍然包含http://localhost:26982(编码为:http%3A%2F%2Flocalhost%3A26982)而不是http://sub.domain.com。 请求/重定向如下所示:

https://steamcommunity.com/openid/login?openid.ns=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0&openid.mode=checkid_setup&openid.return_to=http%3A%2F%2Flocalhost%3A26982%2Fsession%2Fverify&openid.realm=http%3A%2F%2Flocalhost%3A26982&openid.identity=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select&openid.claimed_id=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select

当我在localhost 上尝试时,这看起来是一样的,而且它工作正常。尽管如此,当身份验证成功时,使用sub.domain.com 时重定向不起作用,原因很明显。

解码 => 重写 => 重新编码 URL 路径段的正确方法是什么?

【问题讨论】:

    标签: iis url-rewriting iis-10


    【解决方案1】:

    如果有人对此也有疑问,我通过反复试验找到了一个可行的解决方案。

    <outboundRules>
        <rule name="Rewrite Header" preCondition="IsRedirect">
            <match serverVariable="RESPONSE_Location" pattern="^https://(steamcommunity.com)/(.*)(return_to=http%3A%2F%2Flocalhost%3A26982)(.*)(realm=http%3A%2F%2Flocalhost%3A26982)(.*)" />
            <conditions>
                <add input="{R:1}" pattern="steamcommunity.com" />
            </conditions>
            <action type="Rewrite" value="https://{R:1}/{R:2}return_to=https%3A%2F%2Fsub.domain.com{R:4}realm=https%3A%2F%2Fsub.domain.com{R:6}" />
        </rule>
        <preConditions>
            <preCondition name="IsRedirect">
                <add input="{RESPONSE_STATUS}" pattern="3\d\d" />
            </preCondition>
        </preConditions>
    
    </outboundRules>
    

    这将使用新值重写任何 HTTP 3XX 重定向位置。

    如果有更好的解决方案,请告诉我,但目前这对我有用。

    【讨论】:

      猜你喜欢
      • 2020-05-23
      • 2014-03-24
      • 1970-01-01
      • 1970-01-01
      • 2014-02-01
      • 2019-02-11
      • 2016-11-13
      • 2020-07-29
      • 2019-07-05
      相关资源
      最近更新 更多