Skip to content

非公式 / UnofficialCloudflare, Inc. 非提携。アカウント情報は入力しないでください。Not affiliated with Cloudflare, Inc. Do not enter Cloudflare credentials here.Official docsこのサイトについてプライバシー

カスタムルールセットにルールを追加する

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

既存のカスタムルールセットにルールを追加するには、Update an account or zone ruleset 操作を使い、ルールを配列で渡します。各ルールには式とアクションがあります。

Terraform を使っている場合は、カスタムルールセットの作成とデプロイの例について Terraform による WAF カスタムルールの設定 を参照してください。

Cloudflare ダッシュボードを使っている場合は、ダッシュボードでカスタムルールセットを操作する を参照してください。

ルールを追加する

次のリクエストは、アカウントレベルのカスタムルールセット(ID は $RULESET_ID)へルールを 2 件追加します。これらがルールセット内の唯一の 2 件になります。

応答の id フィールドに、新しいルールのルール ID が含まれます。

Required API token permissions

At least one of the following token permissions is required:
  • Mass URL Redirects Write
  • Magic Firewall Write
  • L4 DDoS Managed Ruleset Write
  • Transform Rules Write
  • Select Configuration Write
  • Account WAF Write
  • Account Rulesets Write
  • Logs Write
Update an account rulesetbash
curl "https://api.cloudflare.com/client/v4/accounts/$ACCOUNT_ID/rulesets/$RULESET_ID" \
	--request PUT \
	--header "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
	--json '{
		"rules": [
				{
						"expression": "(ip.src.country in {\"GB\" \"FR\"} and cf.bot_management.score < 20 and not cf.bot_management.verified_bot)",
						"action": "challenge",
						"description": "challenge GB and FR based on bot score"
				},
				{
						"expression": "not http.request.uri.path matches \"^/api/.*$\"",
						"action": "challenge",
						"description": "challenge not /api"
				}
		]
	}'
{
	"result": {
		"id": "<CUSTOM_RULESET_ID>",
		"name": "Custom Ruleset 1",
		"kind": "custom",
		"version": "2",
		"rules": [
			{
				"id": "<CUSTOM_RULE_ID_1>",
				"version": "1",
				"action": "challenge",
				"expression": "(ip.src.country in {\"GB\" \"FR\"} and cf.bot_management.score < 20 and not cf.bot_management.verified_bot)",
				"description": "challenge GB and FR based on bot score",
				"last_updated": "2021-03-18T18:25:08.122758Z",
				"ref": "<CUSTOM_RULE_REF_1>",
				"enabled": true
			},
			{
				"id": "<CUSTOM_RULE_ID_2>",
				"version": "1",
				"action": "challenge",
				"expression": "not http.request.uri.path matches \"^/api/.*$\"",
				"description": "challenge not /api",
				"last_updated": "2021-03-18T18:25:08.122758Z",
				"ref": "<CUSTOM_RULE_REF_2>",
				"enabled": true
			}
		],
		"last_updated": "2021-03-18T18:25:08.122758Z",
		"phase": "http_request_firewall_custom"
	},
	"success": true,
	"errors": [],
	"messages": []
}

ルールを更新する

カスタムルールセット内の 1 件以上のルールを更新するには、Update an account or zone ruleset 操作を使います。変更したいルールの ID を rules 配列に含め、更新したいフィールドを追加します。リクエストはルールセット全体を新しいバージョンで置き換えます。そのため、残したいルールの ID をすべて含める必要があります。

次の PUT リクエストは、アカウントレベルのカスタムルールセット内のルールを 1 件編集し、ルールの実行順を更新します。

応答には、変更後のカスタムルールセットが含まれます。更新したルールとルールセットのバージョン番号が増える点に注意してください。

Required API token permissions

At least one of the following token permissions is required:
  • Mass URL Redirects Write
  • Magic Firewall Write
  • L4 DDoS Managed Ruleset Write
  • Transform Rules Write
  • Select Configuration Write
  • Account WAF Write
  • Account Rulesets Write
  • Logs Write
Update an account rulesetbash
curl "https://api.cloudflare.com/client/v4/accounts/$ACCOUNT_ID/rulesets/$RULESET_ID" \
	--request PUT \
	--header "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
	--json '{
		"rules": [
				{
						"id": "<CUSTOM_RULE_ID_2>",
						"expression": "not http.request.uri.path matches \"^/api/.*$\"",
						"action": "js_challenge",
						"description": "js_challenge when not /api"
				},
				{
						"id": "<CUSTOM_RULE_ID_1>"
				}
		]
	}'
{
	"result": {
		"id": "<CUSTOM_RULESET_ID>",
		"name": "Custom Ruleset 1",
		"kind": "custom",
		"version": "3",
		"rules": [
			{
				"id": "<CUSTOM_RULE_ID_2>",
				"version": "2",
				"action": "js_challenge",
				"expression": "not http.request.uri.path matches \"^/api/.*$\"",
				"description": "js_challenge when not /api",
				"last_updated": "2021-03-18T18:30:08.122758Z",
				"ref": "<CUSTOM_RULE_ID_2>",
				"enabled": true
			},
			{
				"id": "<CUSTOM_RULE_ID_1>",
				"version": "1",
				"action": "challenge",
				"expression": "(ip.src.country in {\"GB\" \"FR\"} and cf.bot_management.score < 20 and not cf.bot_management.verified_bot)",
				"description": "challenge GB and FR based on bot score",
				"last_updated": "2021-03-18T18:25:08.122758Z",
				"ref": "<CUSTOM_RULE_ID_1>",
				"enabled": true
			}
		],
		"last_updated": "2021-03-18T18:30:08.122758Z",
		"phase": "http_request_firewall_custom"
	},
	"success": true,
	"errors": [],
	"messages": []
}

役に立ちましたか?