【问题标题】:IIS UrlRewrite: How to rewrite from just a domain to domain and pathIIS UrlRewrite:如何从域重写为域和路径
【发布时间】:2020-08-05 07:52:09
【问题描述】:

我已经成功编写了一个 web.config,它可以让我反向代理一个站点并让它出现在服务器的 IFRAME 甚至我自己的本地主机上。以下是它的简化/编辑版本。

<system.webServer>
    <rewrite>
        <rules>
            <rule name="ReverseProxyInboundRule1" stopProcessing="false">
                <match url="(.*)" />
                <action type="Rewrite" url="https://www.example.com/{R:1}" logRewrittenUrl="true" />
                <serverVariables>
                    <set name="HTTP_X_ORIGINAL_ACCEPT_ENCODING" value="{HTTP_ACCEPT_ENCODING}" />
                    <set name="HTTP_ACCEPT_ENCODING" value="" />
                </serverVariables>
            </rule>
            <rule name="Capture Origin Header">
                <match url=".*" />
                <conditions>
                    <add input="{HTTP_ORIGIN}" pattern=".+" />
                </conditions>
                <serverVariables>
                    <set name="CAPTURED_ORIGIN" value="{C:0}" />
                </serverVariables>
                <action type="None" />
            </rule>
        </rules>
        <outboundRules>
            <rule name="Rule1" patternSyntax="Wildcard" stopProcessing="false">
                <match serverVariable="RESPONSE_X-Frame-Options" pattern="*" />
                <action type="Rewrite" value="" />
            </rule>
            <rule name="ReverseProxyOutboundRule1" preCondition="ResponseIsTextHtml" stopProcessing="false">
                <match filterByTags="None" pattern="^http(s)?://www.example.com/(.*)" />
                <action type="Rewrite" value="http{R:1}://thing.our-server.com/{R:2}" />
            </rule>
            <rule name="RestoreAcceptEncoding" preCondition="NeedsRestoringAcceptEncoding">
                <match serverVariable="HTTP_ACCEPT_ENCODING" pattern="^(.*)" />
                <action type="Rewrite" value="{HTTP_X_ORIGINAL_ACCEPT_ENCODING}" />
            </rule>
            <rule name="ContentDam" preCondition="ResponseIsTextHtml">
                <match filterByTags="None" pattern="^/(content/dam/.*)" />
                <action type="Rewrite" value="https://www.example.com/{R:1}" />
            </rule>
            <rule name="Set-Access-Control-Allow-Origin for known origins">
                <match serverVariable="RESPONSE_Access-Control-Allow-Origin" pattern=".+" negate="true" />
                <action type="Rewrite" value="{CAPTURED_ORIGIN}" />
            </rule>
            <rule name="source srcset" preCondition="ResponseIsTextHtml">
                <match filterByTags="CustomTags" customTags="sourceSrcset" pattern=",?\/(content\/dam\/\S+\s\d+w)" />
                <action type="Rewrite" value="https://www.example.com/{R:1}" />
            </rule>
            <rule name="Change GTM" preCondition="ResponseIsTextAnything" patternSyntax="ExactMatch">
                <match pattern="GTM-5GFGV2" />
                <action type="Rewrite" value="GTM-5XVB5D" />
            </rule>
            <preConditions>
                <preCondition name="ResponseIsTextHtml">
                    <add input="{RESPONSE_CONTENT_TYPE}" pattern="^text/html" />
                </preCondition>
                <preCondition name="ResponseIsTextAnything">
                    <add input="{RESPONSE_CONTENT_TYPE}" pattern="^text/(.+)" />
                </preCondition>
                <preCondition name="NeedsRestoringAcceptEncoding">
                    <add input="{HTTP_X_ORIGINAL_ACCEPT_ENCODING}" pattern=".*" />
                </preCondition>
            </preConditions>
            <customTags>
                <tags name="sourceSrcset">
                    <tag name="source" attribute="srcset" />
                </tags>
            </customTags>
        </outboundRules>
    </rewrite>
    <httpProtocol>
        <customHeaders>
            <add name="Access-Control-Allow-Origin" value="*" />
        </customHeaders> 
    </httpProtocol>
</system.webServer>

目前,我将 IFRAME 指向 https://thing.our-server.com/,反向代理获取路径和查询字符串并将它们交给 https://www.example.com/ 的请求,并将结果(或多或少)重写为 this.our-server.com

我希望我可以将我的 IFRAME 指向 https://thing.our-server.com/example/(加上路径和查询)并让一切都以相同的方式工作。

但是,当我进行以下更改时,它不起作用:

<rule name="ReverseProxyInboundRule1" stopProcessing="false">
      <match url="example/(.*)" />
      <action type="Rewrite" url="https://www.example.com/{R:1}" logRewrittenUrl="true" />
      <serverVariables>
          <set name="HTTP_X_ORIGINAL_ACCEPT_ENCODING" value="{HTTP_ACCEPT_ENCODING}" />
          <set name="HTTP_ACCEPT_ENCODING" value="" />
      </serverVariables>
</rule>

                <rule name="ReverseProxyOutboundRule1" preCondition="ResponseIsTextHtml">
                    <match filterByTags="None" pattern="^http(s)?://www.example.com/(.*)" />
                    <action type="Rewrite" value="http{R:1}://thing.out-server.com/examle/{R:2}" />
                </rule>

我认为这是一个错误的规格,但我现在看不到它。

【问题讨论】:

  • example/(.*) 的正则表达式与example 的字符串不匹配
  • 嗯...好点。现已修复。

标签: iis url-rewriting web-config


【解决方案1】:

如果您在 example/ 之后有任何内容,那么您可以使用 example/(.*) 正则表达式。否则,只需在匹配规则中使用示例。

另外一点是以下规则中的输入错误:

 <rule name="ReverseProxyOutboundRule1" preCondition="ResponseIsTextHtml">
                    <match filterByTags="None" pattern="^http(s)?://www.example.com/(.*)" />
                    <action type="Rewrite" value="http{R:1}://thing.out-server.com/examle/{R:2}" />
                </rule>

http{R:1}://thing.out-server.com/examle/{R:2} 使用示例。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-04-03
    • 2014-09-18
    • 1970-01-01
    • 2022-01-05
    • 1970-01-01
    • 2014-06-21
    • 2021-08-07
    • 2018-04-15
    相关资源
    最近更新 更多