跳转到内容
搜索文档

更改 HTTP Host 标头和 DNS 记录

创建一条 origin rule,用于更改 HTTP Host 标头以及解析的 DNS 记录。

最后更新 查看 MarkdownAgent 设置

创建一条 origin rule,用于更改 HTTP Host 标头以及解析的 DNS 记录。 以下 origin rule 会将 URI 路径以 /hr-app/ 开头的所有请求的 HTTP Host 标头覆盖为 hr-server.example.com。同时也会将 DNS 记录覆盖为同一主机名。

Host 标头覆盖仅更新标头值;DNS 记录覆盖会处理传入请求的重新路由。有关这些覆盖的更多信息,请参阅 Origin Rules 设置

使用 Expression Builder 时的表达式:

Field Operator Value
URI Path starts with /hr-app/

使用 Expression Editor 时的表达式:

(starts_with(http.request.uri.path, "/hr-app/"))

Host Header(主机标头) > Rewrite to(重写为) 之后的值:

hr-server.example.com

DNS Record > Override to 之后的值:

hr-server.example.com

以下示例使用更新区域规则集操作,将现有阶段规则集 ($RULESET_ID) 的规则设置为单个源站规则——重写传入请求的 Host 标头和解析后的 DNS 记录。响应将包含您更新后的规则集的完整定义。

Required API token permissions

At least one of the following token permissions is required:
  • Response Compression Write
  • Config Settings Write
  • Dynamic URL Redirects Write
  • Cache Settings Write
  • Custom Errors Write
  • Origin Write
  • Managed headers Write
  • Zone Transform Rules Write
  • Mass URL Redirects Write
  • Magic Firewall Write
  • L4 DDoS Managed Ruleset Write
  • HTTP DDoS Managed Ruleset Write
  • Sanitize Write
  • Transform Rules Write
  • Select Configuration Write
  • Bot Management Write
  • Zone WAF Write
  • Account WAF Write
  • Account Rulesets Write
  • Logs Write
  • Logs Write
Update a zone rulesetbash
curl "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/rulesets/$RULESET_ID" \
	--request PUT \
	--header "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
	--json '{
		"rules": [
				{
						"ref": "hr_app_overrides",
						"expression": "starts_with(http.request.uri.path, \"/hr-app/\")",
						"description": "Origin rule for the company HR application",
						"action": "route",
						"action_parameters": {
								"host_header": "hr-server.example.com",
								"origin": {
										"host": "hr-server.example.com"
								}
						}
				}
		]
	}'
{
	"result": {
		"id": "<RULESET_ID>",
		"name": "Origin Rules ruleset",
		"description": "Zone-level ruleset that will execute origin rules.",
		"kind": "zone",
		"version": "2",
		"rules": [
			{
				"ref": "hr_app_overrides",
				"id": "<RULE_ID>",
				"version": "1",
				"action": "route",
				"action_parameters": {
					"host_header": "hr-server.example.com",
					"origin": {
						"host": "hr-server.example.com"
					}
				},
				"expression": "starts_with(http.request.uri.path, \"/hr-app/\")",
				"description": "Origin rule for the company HR application",
				"last_updated": "2022-06-03T14:42:04.219025Z",
				"ref": "<RULE_REF>"
			}
		],
		"last_updated": "2022-06-03T14:42:04.219025Z",
		"phase": "http_request_origin"
	},
	"success": true,
	"errors": [],
	"messages": []
}

使用 Terraform 时,通过 ref 字段可在更新后保持稳定的规则 ID。添加该字段可防止 Terraform 在变更时重建规则。更多信息请参阅 故障排除(Terraform 文档)。

这篇文档对您有帮助吗?