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
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.
# 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
doneEnable 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.
Related reading
- ERR_CONTENT_DECODING_FAILED: how to fix it: Fix ERR_CONTENT_DECODING_FAILED: wrong Content-Encoding headers, double gzip compression, broken Brotli, PHP output buffering and proxies, with tests.
- 422 Unprocessable Entity: how to fix it: Fix 422 Unprocessable Entity (Unprocessable Content): validation errors in APIs and forms, CSRF tokens in Rails and Laravel, and how to read the error body.
- 502 Bad Gateway: causes and how to fix it: What a 502 Bad Gateway means, the common causes behind Nginx, load balancers and CDNs, how to find yours and how to stop it happening again.
- 499 Client Closed Request in Nginx: what it means: What Nginx 499 Client Closed Request means: impatient clients, load balancer timeouts and slow backends, and how to tell a harmless 499 from a real outage.
- ERR_HTTP2_PROTOCOL_ERROR: causes and how to fix it: Fix ERR_HTTP2_PROTOCOL_ERROR in Chrome: invalid response headers, crashed backends behind HTTP/2 proxies, buffer limits and antivirus interference.
- Nginx monitoring: status, logs and alerts: Monitor Nginx with stub_status and JSON access logs: active and dropped connections, request rate, 5xx share, upstream time and alerts to set.
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.