我有一个位于
https://blog.example.com
的 WordPress 网站和另一个单独托管在 Azure App Service (Windows) 中的站点,位于 https://www.example.com
。 Cloudflare 位于这两个站点的前面。
我已经设置了一个反向代理,将请求从
https://www.example.com/blog
指向https://blog.example.com
。这似乎主要是因为博客文章出现在预期的 URL 下(即https://www.example.com/blog/a-blog-post
),但有一些特点让我觉得有些地方设置不正确:
blog.example.com
)https://blog.example.com
https://blog.example.com
https://blog.example.com
-> https://www.example.com/blog
添加 301 重定向时,它会进入无限重定向循环。根据我的阅读,我相信所有这些问题都会发生,因为当托管 WordPress 的服务器处理请求时,
Host
标头是 https://blog.example.com
而不是 https://www.example.com
。有几个地方(例如这里)WordPress使用主机头来构造某些URL,而不是WordPress网站URL或主页URL(都设置为https://www.example.com/blog
)。 Microsoft 建议 保留原始主机标头以解决这些问题。
IIS 上的应用程序请求路由 (ARR) 有一个
preserveHostHeader
选项,大概用于保留原始主机标头。我试过启用它但代理完全停止工作:
https://www.example.com/blog
(博客根目录)显示https://www.example.com
主页https://www.example.com/blog/a-blog-post
显示 404(由 https://www.example.com
的站点生成)这是我现有的设置:
applicationHost.xdt (在 Azure 应用服务上启用 ARR,因为它默认处于禁用状态)
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<system.webServer>
<proxy xdt:Transform="InsertIfMissing" enabled="true" preserveHostHeader="false" reverseRewriteHostInResponseHeaders="false"/>
<rewrite xdt:Transform="InsertIfMissing">
<allowedServerVariables xdt:Transform="InsertIfMissing">
<add name="HTTP_X_ORIGINAL_HOST" xdt:Transform="InsertIfMissing" xdt:Locator="Match(name)"/>
<add name="HTTP_X_UNPROXIED_URL" xdt:Transform="InsertIfMissing" xdt:Locator="Match(name)"/>
<add name="HTTP_X_ORIGINAL_ACCEPT_ENCODING" xdt:Transform="InsertIfMissing" xdt:Locator="Match(name)"/>
<add name="HTTP_ACCEPT_ENCODING" xdt:Transform="InsertIfMissing" xdt:Locator="Match(name)"/>
</allowedServerVariables>
</rewrite>
</system.webServer>
</configuration>
web.config (从子目录重写请求 -> 子域)
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<location path="." inheritInChildApplications="false">
<system.webServer>
<handlers>
<add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModuleV2" resourceType="Unspecified" />
</handlers>
<aspNetCore processPath="dotnet" arguments=".\Example.dll" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" hostingModel="inprocess" />
<rewrite>
<rules>
<clear />
<rule name="Blog Proxy" stopProcessing="false">
<match url="^blog(?:$|/)(.*)" />
<action type="Rewrite" url="https://blog.example.com/{R:1}" appendQueryString="true" logRewrittenUrl="false" />
<serverVariables>
<set name="HTTP_X_UNPROXIED_URL" value="https://blog.example.com/{R:1}" />
<set name="HTTP_X_ORIGINAL_ACCEPT_ENCODING" value="{HTTP_ACCEPT_ENCODING}" />
<set name="HTTP_X_ORIGINAL_HOST" value="{HTTP_HOST}" />
<set name="HTTP_ACCEPT_ENCODING" value="" />
</serverVariables>
</rule>
</rules>
</rewrite>
</system.webServer>
</location>
</configuration>
这似乎是反向代理的一个非常标准的设置,但是唉。这是因为我落后于 Cloudflare 吗?
preserveHostHeader
不适用于 Azure 应用服务吗?我怎样才能设置这个反向代理来处理我的用例?
问题原来是网络主机如何设置他们的环境。
服务器的配置方式,当请求进入特定主机名时,将检查该主机名的虚拟主机文件。找到匹配项后,将从虚拟主机文件中设置的 DocRoot 加载内容。
在我的情况下,没有配置虚拟主机文件来处理对 www.example.com 的请求。在这种情况下,默认的 catchall vhost 文件然后会将此请求转发到服务器上的 catchall DocRoot,因此当我们在末尾附加 /blog 时,catchall DocRoot 的路径不存在,因此它返回 404。添加 www.example.com 作为帐户上的指针域告诉服务器哪个 DocRoot 为该主机名提供服务请求似乎解决了我正在处理的关于 404 响应的问题。
我认为“指针域”概念可能是这个特定网络主机所独有的,这个问题或答案可能不那么普遍适用。如果您遇到同样的事情,希望它能提供一些帮助。