Skip to content

Troubleshooting guide

How to enable Brotli compression on Nginx and CDNs

Brotli produces smaller text responses than gzip, typically around 15 to 20 percent. It is one of the simplest page-weight wins available.

3 min read10 sectionsUpdated

On this page

Key points

  • Browsers offer it only over HTTPS.
  • Precompress static files at maximum level.
  • Use a moderate level for dynamic HTML.
  • Do not compress images and video that are already compressed.

What Brotli gives you

Browsers advertise Accept-Encoding: gzip, deflate, br, and servers choose one. Brotli benefits from a built-in dictionary of web text, so it does especially well on HTML, CSS and JavaScript. Level 11 is very slow to compress but quick to decompress, so it suits files built once and served many times.

Why it may not be working

  • The Nginx module is not installed, as it is not in core Nginx.
  • brotli_types does not include your content type.
  • A proxy strips the header or re-encodes.
  • Testing over plain HTTP, where browsers will not ask for it.
  • The CDN caches only one encoding.

Check what is served

Ask for Brotli explicitly and compare the sizes.

bash
# Is br being used?
curl -sI -H 'Accept-Encoding: br' https://example.com/app.js | grep -iE 'content-encoding|vary'

# Compare transferred sizes
for e in identity gzip br; do
  printf '%s: ' "$e"; curl -s -o /dev/null -H "Accept-Encoding: $e" -w '%{size_download}\n' https://example.com/app.js
done

Enable it

With ngx_brotli installed:

brotli on; brotli_comp_level 5; brotli_types text/css application/javascript application/json image/svg+xml;

Add brotli_static on; to serve precompressed .br files, generated at build time with brotli -q 11. Keep gzip enabled as a fallback. On most CDNs, including Cloudflare, Brotli is a simple toggle or on by default. Make sure Vary: Accept-Encoding is present, so that caches keep each version separately.

Choosing the level

Compression spends CPU to save bytes. For static assets, the cost is paid once at build time, so level 11 is an easy choice. For HTML generated per request, high levels add noticeable latency, which can cancel out the benefit: level 4 or 5 usually beats gzip in both size and speed. Very small responses gain nothing, so skip anything under about 1 KB. Finally, measure the page as a whole. Removing an unused library saves more than better compression of that library ever will.

Catch it automatically

A config or CDN change can silently turn compression off. An uptime check that asserts on Content-Encoding will notice, and real-user monitoring shows the effect on load times.

Frequently asked questions

Is Brotli better than gzip?

For text, it is usually smaller at comparable speed.

Do all browsers support it?

All modern browsers, over HTTPS.

Should I compress images?

No. Use modern image formats instead.

Watch your own site and services with VitalRix

Uptime checks, APM, logs, real-user monitoring, alerts and status pages in one place. Every product has a free tier and no credit card is needed.