如何强制 IIS 在重写期间为 http 主机 header 使用特定值
How to force IIS to use a specific value for the http host header during a rewrite
我正在尝试创建一个重写规则,将网站配置为用作反向代理,但我在将主机 http header 设置为不同于重写中使用的域(objective 是使用 IIS 作为反向代理,用于将 urls 转发到具有自定义主机值的 sendgrid)。
为了说明问题,我创建了 2 个网站,分别名为 url 和 sendgrid,绑定了 url.com 和 sendgrid.net(我为这些网站添加了自定义条目hosts 文件上的名称,以便可以解析名称)。现在,我需要将 url 网站上收到的所有请求重定向到 sendgrid 网站,确保对 sendgrid 网站的请求将主机 http header 设置为 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>
为了查看发生了什么,我激活了失败请求跟踪,我注意到未应用通过先前规则定义的主机 header 文件。我可以看到规则已处理并且 HTTP_HOST header 已处理 (SET_SERVER_VARIABLE),但是当请求被重写时,它将始终将 http 主机设置为 sendgrid.net(而不是将其设置为 url.com):
那么,当 IIS 网站配置为用作反向代理时,有没有办法强制主机 header 使用特定值?
我正在尝试创建一个重写规则,将网站配置为用作反向代理,但我在将主机 http header 设置为不同于重写中使用的域(objective 是使用 IIS 作为反向代理,用于将 urls 转发到具有自定义主机值的 sendgrid)。
为了说明问题,我创建了 2 个网站,分别名为 url 和 sendgrid,绑定了 url.com 和 sendgrid.net(我为这些网站添加了自定义条目hosts 文件上的名称,以便可以解析名称)。现在,我需要将 url 网站上收到的所有请求重定向到 sendgrid 网站,确保对 sendgrid 网站的请求将主机 http header 设置为 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>
为了查看发生了什么,我激活了失败请求跟踪,我注意到未应用通过先前规则定义的主机 header 文件。我可以看到规则已处理并且 HTTP_HOST header 已处理 (SET_SERVER_VARIABLE),但是当请求被重写时,它将始终将 http 主机设置为 sendgrid.net(而不是将其设置为 url.com):
那么,当 IIS 网站配置为用作反向代理时,有没有办法强制主机 header 使用特定值?