cloudflare worker重写主机头

时间:2019-12-30 07:46:05

标签: cloudflare cloudflare-workers

如何在cloudflare工作者中设置另一个主机标头? 例如,我为网站的www记录设置了1.2.3.4 ip 默认情况下,www请求带有标头www.ex.com发送,但我想发送带有new.ex.com标头的www请求

2 个答案:

答案 0 :(得分:0)

您需要为new.ex.com配置DNS记录,以使其指向相同的IP地址。然后,您可以使用URL中的fetch()发出一个new.ex.com请求。

如果您无法将new.ex.com指向正确的IP,另一种选择是使用fetch()选项发出resolveOverride请求,以指定要使用的其他主机名的IP地址:

fetch("https://new.ex.com", {cf: {resolveOverride: "www.ex.com"}});

请注意,这仅在涉及的两个主机名都在您的区域下时才有效。 Documentation about resolveOverride can be found here.

您无法直接设置Host标头,因为这样做可能会在向也使用Cloudflare的第三方服务器发出请求时绕过安全设置。

答案 1 :(得分:0)

// Parse the URL.
let url = new URL(request.url)

// Change the hostname.
url.hostname = "check-server.example.com"

// Construct a new request
request = new Request(url, request)

请注意,这将影响来源看到的Host标头 (它将是check-server.example.com)。有时人们希望Host标头保持不变。

// Tell Cloudflare to connect to `check-server.example.com`
// instead of the hostname specified in the URL.

request = new Request(request,
    {cf: {resolveOverride: "check-server.example.com"}})