跳转到内容
搜索文档

向自定义规则集添加规则

最后更新 查看 MarkdownAgent 设置

要向现有自定义规则集添加规则,请使用 Update an account or zone ruleset 操作,并以数组形式传递规则。每条规则都有表达式和操作。

如果您正在使用 Terraform,请参阅使用 Terraform 配置 WAF 自定义规则,了解创建和部署自定义规则集的示例。

如果您正在使用 Cloudflare 仪表板,请参阅在仪表板中使用自定义规则集

添加规则

以下请求向账户级别、ID 为 $RULESET_ID 的自定义规则集添加两条规则。这两条规则将成为规则集中仅有的两条规则。

响应将在 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": []
}

更新规则

要更新自定义规则集中的一条或多条规则,请使用 Update an account or zone ruleset 操作。在规则数组中包含要修改的规则的 ID,并添加要更新的字段。该请求会用新版本完全替换整个规则集。因此,您必须包含所有希望保留的规则的 ID。

以下 PUT 请求编辑账户级别自定义规则集中的一条规则,并更新规则的执行顺序。

响应将包含修改后的自定义规则集。请注意,更新后的规则和规则集版本号会递增。

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": []
}

这篇文档对您有帮助吗?