Cloudflare Docs
Rules
Rules
Edit this page on GitHub
Set theme to dark (⇧+D)

Compression rules examples

The following examples cover common patterns for configuring compression rules.

​​ Disable Brotli compression for all requests of a zone

The following example rule will disable Brotli compression for all incoming requests of a given zone. The only available compression algorithm will be Gzip.

When incoming requests match

  • All incoming requests

Then

  • Compression options: Custom
  • Define a custom order for compression types: Gzip

If the client does not support Gzip compression, the response will be uncompressed.

Example API request

The following example sets the rules of an existing entry point ruleset (with ID {ruleset_id}) for the http_response_compression phase to a single compression rule, using the Update a zone ruleset operation:

curl --request PUT \
https://api.cloudflare.com/client/v4/zones/{zone_id}/rulesets/{ruleset_id} \
--header "Authorization: Bearer <API_TOKEN>" \
--header "Content-Type: application/json" \
--data '{
"rules": [
{
"expression": "true",
"action": "compress_response",
"action_parameters": {
"algorithms": [
{ "name": "gzip" }
]
}
}
]
}'

​​ Do not apply compression to AVIF images

The following example rule will disable compression for AVIF images, based on either the content type or the file extension specified in the request.

When incoming requests match

  • Custom filter expression:

    • Media Type equals image/avif OR
    • File extension equals avif

Then

  • Compression options > Disable compression
Example API request

The following example sets the rules of an existing entry point ruleset (with ID {ruleset_id}) for the http_response_compression phase to a single compression rule, using the Update a zone ruleset operation:

curl --request PUT \
https://api.cloudflare.com/client/v4/zones/{zone_id}/rulesets/{ruleset_id} \
--header "Authorization: Bearer <API_TOKEN>" \
--header "Content-Type: application/json" \
--data '{
"rules": [
{
"expression": "http.response.content_type.media_type eq \"image/avif\" or http.request.uri.path.extension eq \"avif\"",
"action": "compress_response",
"action_parameters": {
"algorithms": [
{ "name": "none" }
]
}
}
]
}'

​​ Use Gzip compression for CSV files or other algorithm if not available

The following example rule will configure Gzip compression as the preferred compression method for CSV files. If the visitor does not support this algorithm, Cloudflare will try to compress the response using a different algorithm supported by the visitor.

When incoming requests match

  • Custom filter expression:

    • File extension equals csv

Then

  • Compression options: Custom
  • Define a custom order for compression types: Gzip, Auto
Example API request

The following example sets the rules of an existing entry point ruleset (with ID {ruleset_id}) for the http_response_compression phase to a single compression rule, using the Update a zone ruleset operation:

curl --request PUT \
https://api.cloudflare.com/client/v4/zones/{zone_id}/rulesets/{ruleset_id} \
--header "Authorization: Bearer <API_TOKEN>" \
--header "Content-Type: application/json" \
--data '{
"rules": [
{
"expression": "http.request.uri.path.extension eq \"csv\"",
"action": "compress_response",
"action_parameters": {
"algorithms": [
{ "name": "gzip" },
{ "name": "auto" }
]
}
}
]
}'

​​ Use only Brotli compression for a specific URI path

The following example rule will configure only Brotli compression for a specific URI path.

When incoming requests match

  • Custom filter expression:

    • URI Path equals /download/assets.tar

Then

  • Compression options: Custom
  • Define a custom order for compression types: Brotli

Since the rule configuration does not include Auto at the end of the custom algorithms list, the response will be uncompressed if the web visitor does not support Brotli.

Example API request

The following example sets the rules of an existing entry point ruleset (with ID {ruleset_id}) for the http_response_compression phase to a single compression rule, using the Update a zone ruleset operation:

curl --request PUT \
https://api.cloudflare.com/client/v4/zones/{zone_id}/rulesets/{ruleset_id} \
--header "Authorization: Bearer <API_TOKEN>" \
--header "Content-Type: application/json" \
--data '{
"rules": [
{
"expression": "http.request.uri.path eq \"/download/assets.tar\"",
"action": "compress_response",
"action_parameters": {
"algorithms": [
{ "name": "brotli" }
]
}
}
]
}'