What is Brotli
Brotli is a lossless data compression algorithm developed by Google in 2015 . It was originally created to compress WOFF2 fonts, then made available for use in HTTP compression. In the HTTP protocol, it is identified by the code br.
Brotli compresses text data 20-30% more efficiently than Gzip, with comparable decompression speed.
Brotli vs. Gzip: comparison
| Parameter | Gzip | Brotli |
|---|---|---|
| HTML compression ratio | 75% | 82% |
| JS compression ratio | 70% | 78% |
| Browser support | 100% | ~96% (all modern browsers) |
| HTTPS support | HTTP and HTTPS | HTTPS only |
| Decompression speed | Fast | Comparable to Gzip |
How Brotli works
Brotli uses an improved version of the LZ77 algorithm, Huffman coding, and a predefined dictionary (context modeling). The built-in dictionary contains common words and HTML, JS, and CSS constructs, making it particularly effective for web content.
How to enable Brotli
Nginx (ngx_brotli module)
brotli on;
brotli_comp_level 6;
brotli_types
text/plain
text/css
application/javascript
application/json
image/svg+xml;To install the module:
apt install libnginx-mod-brotli
# В nginx.conf:
load_module modules/ngx_brotli.so;Cloudflare
Cloudflare automatically applies Brotli to compatible browsers when the proxy is enabled. Settings are in Dashboard → Speed → Optimization.
Node.js
const shrinkRay = require('shrink-ray-current');
app.use(shrinkRay()); // поддерживает и Brotli и GzipOr use iltorb (the official Node.js Brotli binding):
const { createBrotliCompress } = require('zlib');Node.js 10.16+ includes native Brotli through the zlib module.
Brotli compression levels
Brotli supports compression levels from 0 to 11:
- Levels 1-4 — fast compression, for dynamic content
- Levels 5-6 — a balance between speed and compression (recommended)
- Levels 9-11 — maximum compression, slow (for static content with caching)
For dynamic HTML pages: level 4. For static files: level 11 (it is cached, so the CPU cost is incurred only once).
Check Brotli on reChecker
Use HTTP/2 & Brotli to check whether your site supports Brotli. The tool sends a request with Accept-Encoding: br, gzip and shows:
- The compression algorithm used (br / gzip / deflate / none)
- The HTTP protocol version
- Caching headers
FAQ
If Brotli is better than Gzip, should I keep Gzip?
Yes. IE, older browsers, and some proxy servers do not support Brotli. The server should support both options and select one based on the browser's Accept-Encoding header.
Does Brotli work over HTTP without SSL? No. Browsers do not support Brotli over unencrypted connections. This is intentional for security. HTTPS is required.
Can files be pre-compressed?
Yes — pre-compressed Brotli. Nginx and Apache can serve pre-compressed .br files instead of compressing them on the fly. This reduces CPU load for popular static content.