【发布时间】:2021-02-19 15:38:52
【问题描述】:
我正在尝试创建一个重写规则,将网站配置为用作反向代理,但在将主机 http 标头设置为与重写中使用的域不同的预定义值时遇到了一些问题(目标是使用 IIS 作为反向代理,将 URL 转发到具有自定义主机值的 sendgrid)。
为了说明问题,我创建了 2 个名为 url 和 sendgrid 的网站,并使用绑定 url.com 和 sendgrid.net(我在 hosts 文件中为这些名称添加了自定义条目,以便名称可以得到解决)。现在,我需要将在 url 网站上收到的所有请求重定向到 sendgrid 网站,确保对 sendgrid 网站的请求将主机 http 标头设置为 url.com。我首先向 url 网站添加了一个重写规则,如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="ReverseProxyInboundRule1" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{CACHE_URL}" pattern="^(https?)://" />
</conditions>
<action type="Rewrite" url="http://sendgrid.net/{R:1}" logRewrittenUrl="true" />
<serverVariables>
<set name="HTTP_HOST" value="url.madeira.gov.pt" />
</serverVariables>
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
applicationhost.config 文件也已更新,因此可以更改 HTTP_HOST:
<location path="url">
<system.webServer>
<rewrite>
<allowedServerVariables>
<add name="HTTP_HOST" />
</allowedServerVariables>
</rewrite>
</system.webServer>
</location>
为了查看发生了什么,我激活了失败的请求跟踪,我注意到通过前面的规则定义的主机头文件没有应用。可以看到已经处理了规则并且处理了HTTP_HOST标头(SET_SERVER_VARIABLE),但是当请求被重写时,它总是将http主机设置为sendgrid.net(而不是设置为url.com):
那么,当 IIS 网站被配置为用作反向代理时,有没有办法强制对主机标头使用特定值?
【问题讨论】:
标签: iis url-rewriting http-headers