注意:由于您是企业客户,我强烈建议您联系 Cloudflare 的客户成功经理和/或解决方案工程师。他们可以帮助解决这类问题。也就是说,为了自助服务客户的利益,我将在这里回答这个问题。
我认为当您说“渲染覆盖”时,您实际上是指“解决覆盖”。此设置更改请求的 DNS 查找,以便将其路由到与正常情况不同的源 IP 地址。
请注意,Resolve Override 不会以任何方式重写请求;它仅将其路由到不同的服务器。因此,一个请求www.example.com/app2/foo
将发送到服务器app2.example.com
,但路径仍然是/app2/foo
(不是/foo
),并且Host
标头仍然是Host: www.example.com
.
除了重定向到不同的来源之外,在您的情况下,您确实希望/app2/*
被重写为。/pa/*
您可以使用Cloudflare Workers来完成此操作,它允许您在 Cloudflare 的边缘执行任意 JavaScript。脚本可能如下所示:
addEventListener("fetch", event => {
event.respondWith(handle(event.request));
});
async function handle(request) {
let url = new URL(request.url) // parse the URL
if (url.pathname.startsWith("/app2/")) {
// Override the target hostname.
url.host = "app2.example.com"
// Replace /app2/ with /pb/ in the path.
url.pathname = "/pb/" + url.pathname.slice("/app2/".length)
// Send the request on to origin.
return fetch(url, request)
} else {
// Just override the hostname.
url.host = "app1.example.com"
// Send the request on to origin.
return fetch(url, request)
}
}
部署后,您可以删除 Resolve Override 页面规则,因为它们现在已被 Worker 脚本覆盖。
Host
请注意,除了路径之外,上述脚本实际上确实重写了标头。如果您希望Host
标题保持原样www.example.com
,那么您将需要使用该cf.resolveOverride
选项。这仅适用于企业客户;如果您在使用它时需要帮助,请咨询您的 CSM 或 SE。但是,在大多数情况下,您实际上希望Host
重写标头,因此您可能不需要它。