Skip to content

非公式本サイトは非公式の日本語ドキュメントであり、Cloudflare 公式サイトではありません。最新情報はdevelopers.cloudflare.comをご確認ください。

API で設定ルールを作成する

最終更新 Markdown で表示Agent セットアップ

Rulesets API を使って、API から設定ルールを作成します。

基本的なルール設定

API で設定ルールを作成するときは、次を確認してください。

  • ルールアクションを set_config に設定する。
  • 一致するリクエストで上書きしたい 設定 に応じて、action_parameters フィールドにパラメーターを定義する。
  • ゾーンレベルで、ルールを http_config_settings フェーズにデプロイする。

手順

API を使って、指定したゾーンに a configuration rule を作成するには、次のワークフローに従います。

  1. ゾーンルールセットを一覧する 操作を使い、ゾーンレベルで http_config_settings フェーズのルールセットがすでにあるかを確認します。

  2. フェーズのルールセットが存在しない場合は、ゾーンルールセットを作成する 操作で作成します。新しいルールセットのプロパティには、次の値を設定します。

    • kind: zone
    • phase: http_config_settings
  3. ゾーンルールセットを更新する 操作を使い、ルールセットのルール一覧に a configuration rule を追加します。または、前のステップで説明した ゾーンルールセットを作成する リクエストにルールを含めます。

API トークンに、API 操作を実行するための 必要な権限 があることを確認してください。

リクエストの例

例: Email Obfuscation と Browser Integrity Check を有効にするルールを追加する

次の例では、Update a zone ruleset 操作を使い、既存のフェーズルールセット({ruleset_id})のルールを、1 件の設定ルールに設定します。このルールは、お問い合わせページで Email Obfuscation と Browser Integrity Check を有効にします。

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": "enable_email_obfuscation_bic",
						"expression": "starts_with(http.request.uri.path, \"/contact-us/\")",
						"description": "Obfuscates email addresses and enables BIC in contacts page",
						"action": "set_config",
						"action_parameters": {
								"email_obfuscation": true,
								"bic": true
						}
				}
		]
	}'

Terraform を使う場合、更新をまたいでルール ID を安定させるには ref フィールドを使います。このフィールドを追加すると、変更時に Terraform がルールを再作成するのを防げます。詳細は Terraform ドキュメントの トラブルシューティング を参照してください。

例: 管理エリアで Under Attack モードをオンにするルールを追加する

次の例では、Update a zone ruleset 操作を使い、既存のフェーズルールセット({ruleset_id})のルールを、1 件の設定ルールに設定します。このルールは、管理エリアで Under Attack モードをオンにします。

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": "enable_under_attack_in_admin",
						"expression": "http.host eq \"admin.example.com\"",
						"description": "Turn on Under Attack mode for admin area",
						"action": "set_config",
						"action_parameters": {
								"security_level": "under_attack"
						}
				}
		]
	}'

Terraform を使う場合、更新をまたいでルール ID を安定させるには ref フィールドを使います。このフィールドを追加すると、変更時に Terraform がルールを再作成するのを防げます。詳細は Terraform ドキュメントの トラブルシューティング を参照してください。


必要な API トークン権限

設定ルールを管理する API リクエストで使う API トークンには、少なくとも次の権限が必要です。

  • Zone > Config Rules > Edit

役に立ちましたか?