次のルール例は、Cloudflare がデフォルトで圧縮する レスポンスのコンテンツタイプに対して、Zstandard 圧縮を有効にします。クライアントが Zstandard 圧縮に対応していない場合は、フォールバックとして Brotli または Gzip 圧縮を使います。
受信リクエストが次に一致する場合
- カスタムフィルター式:
- Media Type is in
text/html, text/richtext, text/plain, text/css, text/x-script, text/x-component, text/x-java-source, text/x-markdown, application/javascript, application/x-javascript, text/javascript, text/js, image/x-icon, image/vnd.microsoft.icon, application/x-perl, application/x-httpd-cgi, text/xml, application/xml, application/rss+xml, application/vnd.api+json, application/x-protobuf, application/json, multipart/bag, multipart/mixed, application/xhtml+xml, font/ttf, font/otf, font/x-woff, image/svg+xml, application/vnd.ms-fontobject, application/ttf, application/x-ttf, application/otf, application/x-otf, application/truetype, application/opentype, application/x-opentype, application/font-woff, application/eot, application/font, application/font-sfnt, application/wasm, application/javascript-binast, application/manifest+json, application/ld+json, application/graphql+json, application/geo+json
- Media Type is in
その場合
- Compression options: Custom
- Define a custom order for compression types:
Zstandard,Brotli,Gzip
次の例は、既存の エントリポイントルールセット(ID は {ruleset_id})の http_response_compression フェーズのルールを、圧縮ルール 1 件に置き換えます。ゾーンルールセットの更新 操作を使います。
Required API token permissions
At least one of the following token permissions is required:Response Compression WriteConfig Settings WriteDynamic URL Redirects WriteCache Settings WriteCustom Errors WriteOrigin WriteManaged headers WriteZone Transform Rules WriteMass URL Redirects WriteMagic Firewall WriteL4 DDoS Managed Ruleset WriteHTTP DDoS Managed Ruleset WriteSanitize WriteTransform Rules WriteSelect Configuration WriteBot Management WriteZone WAF WriteAccount WAF WriteAccount Rulesets WriteLogs WriteLogs Write
curl "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/rulesets/$RULESET_ID" \
--request PUT \
--header "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
--json '{
"rules": [
{
"ref": "use_zstd_compression",
"expression": "(http.response.content_type.media_type in {\"text/html\" \"text/richtext\" \"text/plain\" \"text/css\" \"text/x-script\" \"text/x-component\" \"text/x-java-source\" \"text/x-markdown\" \"application/javascript\" \"application/x-javascript\" \"text/javascript\" \"text/js\" \"image/x-icon\" \"image/vnd.microsoft.icon\" \"application/x-perl\" \"application/x-httpd-cgi\" \"text/xml\" \"application/xml\" \"application/rss+xml\" \"application/vnd.api+json\" \"application/x-protobuf\" \"application/json\" \"multipart/bag\" \"multipart/mixed\" \"application/xhtml+xml\" \"font/ttf\" \"font/otf\" \"font/x-woff\" \"image/svg+xml\" \"application/vnd.ms-fontobject\" \"application/ttf\" \"application/x-ttf\" \"application/otf\" \"application/x-otf\" \"application/truetype\" \"application/opentype\" \"application/x-opentype\" \"application/font-woff\" \"application/eot\" \"application/font\" \"application/font-sfnt\" \"application/wasm\" \"application/javascript-binast\" \"application/manifest+json\" \"application/ld+json\" \"application/graphql+json\" \"application/geo+json\"})",
"action": "compress_response",
"action_parameters": {
"algorithms": [
{
"name": "zstd"
},
{
"name": "brotli"
},
{
"name": "gzip"
}
]
}
}
]
}'Terraform を使う場合、更新をまたいでルール ID を安定させるには ref フィールドを使います。このフィールドを追加すると、変更時に Terraform がルールを再作成するのを防げます。詳細は Terraform ドキュメントの トラブルシューティング を参照してください。