HTTP requests データセットには、標準的なログ情報の多くがデフォルトで含まれます。追加のリクエストヘッダー、レスポンスヘッダー、Cookie を取得したい場合は、カスタムフィールドでログを用途に合わせて調整できます。
カスタムフィールドはゾーン単位で設定します。設定すると、そのゾーンで HTTP requests データセットを使い、リクエストヘッダー、レスポンスヘッダー、Cookie フィールドを含むすべての Logpush ジョブで有効になります。これらのフィールドは raw のまま、または transformed(変換後)の値として記録できます。
このデフォルトは変更できます。リクエストヘッダーまたはレスポンスヘッダーを、必要に応じて raw または transformed として記録できます。ただし、同じヘッダーに対して両方は選べません。
カスタムフィールドは API または Cloudflare ダッシュボードで有効にできます。
Rulesets API を使い、カスタムフィールドを設定するルールを作成します。フェーズ、ルールセット、ルールの概念と、使える API 操作の詳細は Ruleset Engine のドキュメントを参照してください。
カスタムフィールドを設定する手順は次のとおりです。
- カスタムフィールドの一覧を設定するルールを作成します。
- Logpush ジョブに
Cookies、RequestHeaders、ResponseHeadersフィールドのいずれかを含めます。
ゾーンレベルの http_log_custom_fields フェーズで、カスタムフィールドの一覧を設定するルールを作成します。ルールのアクションは log_custom_field、式は true にします。
カスタムフィールドの一覧を設定するルールに含める action_parameters オブジェクトの構造は次のとおりです。
"action_parameters": {
//select raw (default) or transformed request header
"request_fields": [
{ "name": "<http_request_header_raw>" }
],
"transformed_request_fields": [
{ "name": "<http_request_header_transformed>" }
],
//select raw or transformed (default) response header
"response_fields": [
{ "name": "<http_response_header_transformed>" }
],
"raw_response_fields": [
{ "name": "<http_response_header_raw>" }
],
"cookie_fields": [
{ "name": "<cookie_name>" }
]
}ルール定義は次の条件を満たしてください。
action_parametersオブジェクトには、次のいずれかの配列を少なくとも 1 つ含める必要があります。request_fields、transformed_request_fields、response_fields、raw_response_fields、cookie_fieldsです。- HTTP のリクエストヘッダー名とレスポンスヘッダー名は小文字で入力してください。
- Cookie 名は大文字と小文字を区別します。HTTP リクエストと同じ表記で入力してください。
- ルールの式は
trueにしてください。 - リクエストヘッダーまたはレスポンスヘッダーについて、raw と transformed の両方を同じヘッダーに対して記録することはできません。
ルールを作成する手順は次のとおりです。
-
List zone rulesets 操作で、ゾーンレベルに
http_log_custom_fieldsフェーズの エントリーポイントルールセット がすでにあるか確認します(フェーズごとにエントリーポイントルールセットは 1 つだけです)。List zone rulesetsbash curl "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/rulesets" \ --request GET \ --header "Authorization: Bearer $CLOUDFLARE_API_TOKEN"http_log_custom_fieldsフェーズのエントリーポイントルールセット("kind": "zone"かつ"phase": "http_log_custom_fields"のルールセット)がある場合は、そのルールセット ID を控えます。 -
(任意)レスポンスに
"kind": "zone"かつ"phase": "http_log_custom_fields"のルールセットが含まれない場合は、Create a zone ruleset 操作でフェーズのエントリーポイントルールセットを作成します。Create a zone rulesetbash curl "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/rulesets" \ --request POST \ --header "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \ --json '{ "name": "Zone-level phase entry point", "kind": "zone", "description": "This ruleset configures custom log fields.", "phase": "http_log_custom_fields" }'レスポンスに含まれるルールセット ID を控えます。
-
Update a zone ruleset 操作で、見つかった(または前の手順で作成した)エントリーポイントルールセットのルールを定義し、カスタムフィールド設定のルールを追加します。リクエストに含めたルールが、ルールセット内のすべてのルールを置き換えます。
次の例では、Logpush ログに含めたい HTTP リクエストヘッダー、HTTP レスポンスヘッダー、Cookie の名前でカスタムフィールドを設定します。
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": [ { "action": "log_custom_field", "expression": "true", "description": "Set Logpush custom fields for HTTP requests", "action_parameters": { "request_fields": [ { "name": "content-type" }, { "name": "x-forwarded-for" } ], "transformed_request_fields": [ { "name": "host" } ], "response_fields": [ { "name": "server" }, { "name": "content-type" } ], "raw_response_fields": [ { "name": "allow" } ], "cookie_fields": [ { "name": "__ga" }, { "name": "accountNumber" }, { "name": "__cfruid" } ] } } ] }'{ "result": { "id": "<RULESET_ID>", "name": "Zone-level phase entry point", "description": "This ruleset configures custom log fields.", "kind": "zone", "version": "2", "rules": [ { "id": "<RULE_ID_1>", "version": "1", "action": "log_custom_field", "action_parameters": { "request_fields": [ { "name": "content-type" }, { "name": "x-forwarded-for" } ], "transformed_request_fields": [{ name: "host" }], "response_fields": [ { "name": "server" }, { "name": "content-type" } ], "raw_response_fields": [{ name: "allow" }], "cookie_fields": [ { "name": "__ga" }, { "name": "accountNumber" }, { "name": "__cfruid" } ] }, "expression": "true", "description": "Set Logpush custom fields for HTTP requests", "last_updated": "2021-11-21T11:02:08.769537Z", "ref": "<RULE_REF_1>", "enabled": true } ], "last_updated": "2021-11-21T11:02:08.769537Z", "phase": "http_log_custom_fields" }, "success": true, "errors": [], "messages": [] }
オリジンから送られる一部のヘッダー(set-cookie など)は、複数の値を持つことがあります。Rulesets API で、すべての値を記録するヘッダーを指定できます。
curl "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/rulesets/$RULESET_ID" \
--request PUT \
--header "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
--json '{
"rules": [
{
"action": "log_custom_field",
"expression": "true",
"description": "Set Logpush custom fields for HTTP requests",
"action_parameters": {
"response_fields": [
{
"name": "set-cookie",
"preserve_duplicates": true
}
]
}
}
]
}'preserve_duplicates は response_fields と raw_response_fields の両方に適用されます。ヘッダーに影響する Transform ルールがない場合、preserve_duplicates を response_fields または raw_response_fields のいずれかに指定しても、結果は同じになります。
この例では、set-cookie ヘッダーのすべての値が記録されます。ResponseFields の下に文字列の配列として現れます。例:
{
// ...
"ResponseFields": {
"set-cookie": ["name1=val1", "name2=val2", ...]
}
}これらの値の取り出しには、Worker または Logpush 送信先側のカスタム処理を使えます。
次に、ジョブの作成時または更新時に、output_options ジョブパラメーターのフィールド一覧へ、カスタムフィールドの設定に応じて Cookies、RequestHeaders、ResponseHeaders、ResponseFields のいずれかを含めます。ログには、設定したカスタムフィールドとその値がリクエスト / レスポンスとして含まれます。
たとえば、カスタムフィールドを含むジョブを作成する次のリクエストを考えます。
Required API token permissions
At least one of the following token permissions is required:Logs Write
curl "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/logpush/jobs" \
--request POST \
--header "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
--json '{
"name": "<DOMAIN_NAME>",
"destination_conf": "s3://<BUCKET_PATH>?region=us-west-2",
"dataset": "http_requests",
"output_options": {
"field_names": [
"RayID",
"EdgeStartTimestamp",
"Cookies",
"RequestHeaders",
"ResponseHeaders"
],
"timestamp_format": "rfc3339"
},
"ownership_challenge": "<OWNERSHIP_CHALLENGE_TOKEN>"
}'-
Cloudflare ダッシュボードで Logpush ページを開きます。
Logpush を開く ↗ -
Custom log fields セクションで Edit Custom Fields を選択します。
-
Set new Custom Field を選択します。
-
Field Type のドロップダウンから Request Header、Response Header、Cookies のいずれかを選び、Field Name を入力します。
-
完了したら Save を選択します。
Logpush で mTLS 証明書の詳細(cf-cert-subject-dn や cf-cert-issuer-dn など)を記録するには、次の手順が必要です。
- 証明書ヘッダーを挿入する Add TLS client auth headers の Managed Transform を有効にします。
- これらの Cloudflare が挿入するヘッダーを取得するため、Logpush のカスタムフィールドを
request_fieldsではなくtransformed_request_fieldsで設定します。 - Logpush ジョブに
RequestHeadersフィールドを含めます。
mTLS ヘッダーは、クライアントリクエスト受信後に Cloudflare が挿入します。そのため request_fields ではなく transformed_request_fields で取得する必要があります。
クライアント証明書の設定の詳細は mTLS 認証 を参照してください。
- カスタムフィールドはフィールドタイプごとにヘッダーを 100 個まで指定できます。この上限は
request_fields、transformed_request_fields、response_fields、raw_response_fields、cookie_fieldsそれぞれに適用されます。 - リクエストヘッダー
Rangeは、現時点ではカスタムフィールドに対応していません。 - リクエストヘッダーとレスポンスヘッダーの transformed 値と raw 値は API でのみ設定でき、UI からは設定できません。