Skip to content

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

API でルールを作成する

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

Rulesets API を使って、API から Cache Response Rule を作成します。Cloudflare API の設定は、API ドキュメント を参照してください。

基本的なルール設定

API で Cache Response Rule を作成するときは、次を確認してください。

  • ルールのアクションを、利用可能なアクション のいずれかにする。
  • 一致するレスポンスに適用したい 設定 に合わせて、action_parameters フィールドのパラメーターを定義する。
  • ルールを http_response_cache_settings フェーズのエントリポイントルールセットにデプロイする。

手順

  1. List zone rulesets メソッドで、http_response_cache_settings フェーズのルールセットがすでにあるかを確認します。
  2. フェーズのルールセットがない場合は、Create a zone ruleset で作成します。新しいルールセットのプロパティには、次の値を設定します。
    • kind: zone
    • phase: http_response_cache_settings
  3. Update a zone ruleset で、ルールセットにルールを追加します。または、前の手順の Create a zone ruleset リクエストにルールを含めても構いません。

リクエストの例

これらの例は、リクエストとレスポンスの一致条件を使った、Cache Response Rules の利用可能なアクションを示します。これらの例をそのまま使うと、フェーズ内の既存ルールは置き換わります。

例: キャッシュ前に JS ファイルのレスポンスヘッダーを削除する

Update a zone entry point rulesetbash
curl "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/rulesets/phases/http_response_cache_settings/entrypoint" \
	--request PUT \
	--header "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
	--json '{
		"rules": [
				{
						"expression": "http.request.uri.path.extension eq \"js\"",
						"description": "Strip caching headers from JS files",
						"action": "set_cache_settings",
						"action_parameters": {
								"strip_etags": true,
								"strip_set_cookie": true,
								"strip_last_modified": true
						}
				}
		]
	}'

例: API レスポンスに静的なキャッシュタグを設定する

Update a zone entry point rulesetbash
curl "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/rulesets/phases/http_response_cache_settings/entrypoint" \
	--request PUT \
	--header "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
	--json '{
		"rules": [
				{
						"expression": "http.request.uri.path starts_with \"/api/\"",
						"description": "Tag API responses for targeted purging",
						"action": "set_cache_tags",
						"action_parameters": {
								"operation": "set",
								"values": [
										"api-response",
										"dynamic-content"
								]
						}
				}
		]
	}'

例: 式を使ってレスポンスヘッダーからキャッシュタグを追加する

Update a zone entry point rulesetbash
curl "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/rulesets/phases/http_response_cache_settings/entrypoint" \
	--request PUT \
	--header "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
	--json '{
		"rules": [
				{
						"expression": "any(http.response.headers.names[*] == \"Surrogate-Keys\")",
						"description": "Extract cache tags from alternative CDN response header",
						"action": "set_cache_tags",
						"action_parameters": {
								"operation": "add",
								"expression": "split(http.response.headers[\"Surrogate-Keys\"][0], \",\", 1)"
						}
				}
		]
	}'

例: max-age で cache-control を上書きする

Update a zone entry point rulesetbash
curl "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/rulesets/phases/http_response_cache_settings/entrypoint" \
	--request PUT \
	--header "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
	--json '{
		"rules": [
				{
						"expression": "http.response.code eq 200",
						"description": "Override cache-control for successful responses",
						"action": "set_cache_control",
						"action_parameters": {
								"max-age": {
										"operation": "set",
										"value": 3600,
										"cloudflare_only": true
								}
						}
				}
		]
	}'

例: 修飾子付きで private ディレクティブを設定する

Update a zone entry point rulesetbash
curl "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/rulesets/phases/http_response_cache_settings/entrypoint" \
	--request PUT \
	--header "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
	--json '{
		"rules": [
				{
						"expression": "http.request.uri.path starts_with \"/user/\"",
						"description": "Mark user content as private",
						"action": "set_cache_control",
						"action_parameters": {
								"private": {
										"operation": "set",
										"qualifiers": [
												"X-User-Id",
												"X-Session-Token"
										]
								},
								"no-cache": {
										"operation": "set"
								}
						}
				}
		]
	}'

例: 静的フォントアセットに immutable を設定する

Update a zone entry point rulesetbash
curl "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/rulesets/phases/http_response_cache_settings/entrypoint" \
	--request PUT \
	--header "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
	--json '{
		"rules": [
				{
						"expression": "http.request.uri.path.extension in {\"woff2\" \"woff\" \"ttf\"}",
						"description": "Mark fonts as immutable",
						"action": "set_cache_control",
						"action_parameters": {
								"immutable": {
										"operation": "set"
								},
								"max-age": {
										"operation": "set",
										"value": 31536000
								}
						}
				}
		]
	}'

例: ヘッダー削除、タグ付け、cache control 設定を組み合わせた複数ルール

Update a zone entry point rulesetbash
curl "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/rulesets/phases/http_response_cache_settings/entrypoint" \
	--request PUT \
	--header "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
	--json '{
		"rules": [
				{
						"expression": "http.response.code eq 200 and http.request.uri.path.extension eq \"html\"",
						"description": "Strip tracking headers from HTML responses",
						"action": "set_cache_settings",
						"action_parameters": {
								"strip_etags": true,
								"strip_set_cookie": true
						}
				},
				{
						"expression": "http.request.uri.path starts_with \"/products/\"",
						"description": "Tag product pages for purging",
						"action": "set_cache_tags",
						"action_parameters": {
								"operation": "add",
								"values": [
										"product-catalog",
										"storefront"
								]
						}
				},
				{
						"expression": "http.response.code eq 200",
						"description": "Set cache control for all 200 responses",
						"action": "set_cache_control",
						"action_parameters": {
								"s-maxage": {
										"operation": "set",
										"value": 86400,
										"cloudflare_only": true
								},
								"must-revalidate": {
										"operation": "set"
								}
						}
				}
		]
	}'

必要な API トークン権限

Cache Response Rules を管理する API リクエストで使う API トークンには、次の権限が必要です。

  • Zone > Cache Rules > Edit
  • Account Rulesets > Edit
  • Account Filter Lists > Edit

役に立ちましたか?