【发布时间】: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