创建源站规则以更改目的端口。
以下源站规则为所有 URI 路径以 /team/calendar/ 开头的请求将目的端口重写为 8081。
表达式编辑器中的文本:
starts_with(http.request.uri.path, "/team/calendar/")Destination port(目的端口) > Rewrite to(重写至) 后面的值:
8081以下示例使用更新区域规则集操作,将现有阶段规则集 ($RULESET_ID) 的规则设置为单个源站规则——重写传入请求的端口。响应将包含您更新后的规则集的完整定义。
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": "calendar_app_change_port",
"expression": "starts_with(http.request.uri.path, \"/team/calendar/\")",
"description": "Origin rule for the team calendar application",
"action": "route",
"action_parameters": {
"origin": {
"port": 8081
}
}
}
]
}'{
"result": {
"id": "<RULESET_ID>",
"name": "Origin Rules ruleset",
"description": "Zone-level ruleset that will execute origin rules.",
"kind": "zone",
"version": "2",
"rules": [
{
"ref": "calendar_app_change_port",
"id": "<RULE_ID>",
"version": "1",
"action": "route",
"action_parameters": {
"origin": {
"port": 8081
}
},
"expression": "starts_with(http.request.uri.path, \"/team/calendar/\")",
"description": "Origin rule for the team calendar 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 文档)。