APIを介してルールを作成する
Rulesets APIを使用して、APIを介してオリジンルールを作成します。
APIを介してオリジンルールを作成する際は、以下の点に注意してください。
- ルールアクションを
routeに設定します。 - オリジンオーバーライドのタイプに応じて、
action_parametersフィールドにパラメータを定義します。 - ルールをゾーンレベルの
http_request_originフェーズにデプロイします。
Follow this workflow to create オリジンルール for a given zone via API:
-
Use the List zone rulesets operation to check if there is already a ruleset for the
http_request_originphase at the zone level. -
If the phase ruleset does not exist, create it using the Create a zone ruleset operation. In the new ruleset properties, set the following values:
- kind:
zone - phase:
http_request_origin
- kind:
-
Use the Update a zone ruleset operation to add オリジンルール to the list of ruleset rules. Alternatively, include the rule in the Create a zone ruleset request mentioned in the previous step.
API操作を実行するために、APIトークンに必要な権限があることを確認してください。
例: HTTP Host ヘッダーをオーバーライドするルールを追加する
Host ヘッダーをオーバーライドするルールを追加する以下の例では、既存のフェーズルールセット({ruleset_id})のルールを単一のオリジンルールに設定し、HTTP Host ヘッダーをオーバーライドします。ゾーンルールセットを更新する操作を使用します。
curl --request PUT \https://api.cloudflare.com/client/v4/zones/{zone_id}/rulesets/{ruleset_id} \--header "Authorization: Bearer <API_TOKEN>" \--header "Content-Type: application/json" \--data '{ "rules": [ { "expression": "http.request.uri.query contains \"/eu/\"", "description": "私の最初のオリジンルール", "action": "route", "action_parameters": { "host_header": "eu_server.example.net" } } ]}'レスポンスには、更新したルールセットの完全な定義が含まれます。
{ "result": { "id": "<RULESET_ID>", "name": "オリジンルールルールセット", "description": "オリジンルールを実行するゾーンレベルのルールセット。", "kind": "zone", "version": "2", "rules": [ { "id": "<RULE_ID>", "version": "1", "action": "route", "action_parameters": { "host_header": "eu_server.example.net" }, "expression": "http.request.uri.query contains \"/eu/\"", "description": "私の最初のオリジンルール", "last_updated": "2022-06-02T14:42:04.219025Z", "ref": "<RULE_REF>" } ], "last_updated": "2022-06-02T14:42:04.219025Z", "phase": "http_request_origin" }, "success": true, "errors": [], "messages": []}例: 受信リクエストのSNI値をオーバーライドするルールを追加する
以下の例では、既存のフェーズルールセット({ruleset_id})のルールを単一のオリジンルールに設定し、admin.example.com宛ての受信リクエストのSNI値をオーバーライドします。ゾーンルールセットを更新する操作を使用します。
curl --request PUT \https://api.cloudflare.com/client/v4/zones/{zone_id}/rulesets/{ruleset_id} \--header "Authorization: Bearer <API_TOKEN>" \--header "Content-Type: application/json" \--data '{ "rules": [ { "expression": "http.host eq \"admin.example.com\"", "description": "管理エリアのSNIオーバーライド", "action": "route", "action_parameters": { "sni": { "value": "sni.example.com" } } } ]}'例: 受信リクエストの解決されたDNSレコードとHostヘッダーをオーバーライドするルールを追加する
Hostヘッダーをオーバーライドするルールを追加する以下の例では、既存のフェーズルールセット({ruleset_id})のルールを単一のオリジンルールに設定し、受信リクエストの解決されたDNSレコードとHostヘッダーをオーバーライドします。ゾーンルールセットを更新する操作を使用します。
curl --request PUT \https://api.cloudflare.com/client/v4/zones/{zone_id}/rulesets/{ruleset_id} \--header "Authorization: Bearer <API_TOKEN>" \--header "Content-Type: application/json" \--data '{ "rules": [ { "expression": "starts_with(http.request.uri.path, \"/hr-app/\")", "description": "会社のHRアプリケーションのオリジンルール", "action": "route", "action_parameters": { "host_header": "hr-server.example.com", "origin": { "host": "hr-server.example.com" } } } ]}'レスポンスには、更新したルールセットの完全な定義が含まれます。
{ "result": { "id": "<RULESET_ID>", "name": "オリジンルールルールセット", "description": "オリジンルールを実行するゾーンレベルのルールセット。", "kind": "zone", "version": "2", "rules": [ { "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": "会社のHRアプリケーションのオリジンルール", "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": []}例: 受信リクエストのポートをオーバーライドするルールを追加する
以下の例では、既存のフェーズルールセット({ruleset_id})のルールを単一のオリジンルールに設定し、受信リクエストのポートをオーバーライドします。ゾーンルールセットを更新する操作を使用します。
curl --request PUT \https://api.cloudflare.com/client/v4/zones/{zone_id}/rulesets/{ruleset_id} \--header "Authorization: Bearer <API_TOKEN>" \--header "Content-Type: application/json" \--data '{ "rules": [ { "expression": "starts_with(http.request.uri.path, \"/team/calendar/\")", "description": "チームカレンダーアプリケーションのオリジンルール", "action": "route", "action_parameters": { "origin": { "port": 9000 } } } ]}'レスポンスには、更新したルールセットの完全な定義が含まれます。
{ "result": { "id": "<RULESET_ID>", "name": "オリジンルールルールセット", "description": "オリジンルールを実行するゾーンレベルのルールセット。", "kind": "zone", "version": "2", "rules": [ { "id": "<RULE_ID>", "version": "1", "action": "route", "action_parameters": { "origin": { "port": 9000 } }, "expression": "starts_with(http.request.uri.path, \"/team/calendar/\")", "description": "チームカレンダーアプリケーションのオリジンルール", "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": []}オリジンルールを管理するためにAPIリクエストで使用されるAPIトークンは、少なくとも以下の権限を持っている必要があります。
- Origin > Edit