次の 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)のルールをオリジンルール 1 件に設定します。このルールは、受信リクエストの 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 を使う場合、更新をまたいでルール ID を安定させるには ref フィールドを使います。このフィールドを追加すると、変更時に Terraform がルールを再作成するのを防げます。詳細は Terraform ドキュメントの トラブルシューティング を参照してください。