Skip to content

Compression Rules

Use Compression Rules to customize the compression applied to responses from Cloudflare's global network to your website visitors, based on the file extension and content type. Compression Rules are powered by the Ruleset Engine.

Cloudflare compresses some responses by default, based on the content type. With Compression Rules, you can customize the default behavior, which includes defining preferred compression algorithms for particular file types.

When a compression rule matches and lists several compression algorithms (such as gzip and Brotli), Cloudflare selects the first algorithm from your list that the visitor's browser supports. Cloudflare determines browser support from the accept-encoding HTTP request header, which browsers send automatically to indicate which compression formats they can decompress. If multiple compression rules match the same request, the last matching rule takes precedence.

Get started

Cloudflare provides you with rules templates for common use cases.

  1. In the Cloudflare dashboard, go to the Rules Overview page.

    Go to Overview
  2. Select Templates, and then select one of the available templates.

You can also refer to the Examples gallery in the developer docs.

Alternatively, follow the instructions in the following pages to get started:


Availability

Compression Rules are available in all Cloudflare plans.

Free Pro Business Enterprise

Availability

Yes

Yes

Yes

Yes

Number of rules

10 25 50 300

Relevant fields

The following fields are commonly used in expressions of compression rules:

Field in Expression BuilderField name
Media Typehttp.response.content_type.media_type
File extensionhttp.request.uri.path.extension
N/Araw.http.request.uri.path.extension

Important remarks

  • If a compression rule matches but the visitor's browser does not support any of the compression algorithms configured in the rule (based on the accept-encoding request header), the response will not be compressed.

  • If a compression rule matches but the origin server's response includes a cache-control: no-transform HTTP header, the compression rule will not modify the response. Origin servers use this header to indicate that intermediaries (like Cloudflare) should not alter the response body.

Troubleshooting

When troubleshooting Compression Rules, use Cloudflare Trace to determine if a rule is triggering for a specific URL.