创建一条 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.comDNS 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 WriteConfig Settings WriteDynamic URL Redirects WriteCache Settings WriteCustom Errors WriteOrigin WriteManaged headers WriteZone Transform Rules WriteMass URL Redirects WriteMagic Firewall WriteL4 DDoS Managed Ruleset WriteHTTP DDoS Managed Ruleset WriteSanitize WriteTransform Rules WriteSelect Configuration WriteBot Management WriteZone WAF WriteAccount WAF WriteAccount Rulesets WriteLogs WriteLogs Write
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 文档)。