IIS 反向代理重写编码的url segement(HTTP重定向)。

问题描述 投票:0回答:1

我使用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.请求的redirect如下。

https:/steamcommunity.comopenidlogin?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个回答
0
投票

如果有人也有这样的问题,我通过一些试验和错误找到了一个工作解决方案。

<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重定向位置与新的值。

如果有更好的解决方案,让我知道,但现在,这对我来说是可行的。

© www.soinside.com 2019 - 2024. All rights reserved.