Brotli is a compression algorithm from Google that provides 15–25% better compression than Gzip at a comparable speed. This article covers configuring Brotli for Nginx and Apache, comparing it with Gzip, and verifying that it is working.
You can check whether a website supports Brotli with HTTP/2 Checker. To generate an Nginx configuration, use Nginx Generator on rechecker.ru.
Brotli vs. Gzip
| Parameter | Gzip | Brotli |
|---|---|---|
| Compression ratio | Baseline | 15–25% better |
| Compression speed | Faster | Comparable at levels 1–5 |
| Compression levels | 1–9 | 0–11 |
| Browser support | 100% | 97%+ |
| Static compression | Yes | Yes (pre-compressed) |
| Dynamic compression | Yes | Yes |
Brotli is especially effective for text resources: HTML, CSS, JavaScript, JSON, and SVG.
Brotli compression levels
| Level | Compression ratio | CPU usage | Recommendation |
|---|---|---|---|
| 0–3 | Low | Minimal | Dynamic compression |
| 4–6 | Medium | Moderate | Balanced |
| 7–11 | High | High | Static only |
Level 4–5 is typically used for dynamic compression. For static files, use 9–11.
Nginx: configuring Brotli
Installing the module
Nginx does not include Brotli by default. Options:
Ubuntu/Debian (package):
apt install libnginx-mod-http-brotli-filter libnginx-mod-http-brotli-staticBuild from source:
git clone https://github.com/google/ngx_brotli
cd ngx_brotli
git submodule update --init
# При сборке Nginx: --add-module=/path/to/ngx_brotliNginx configuration
# Загрузка модулей (если отдельные .so)
load_module modules/ngx_http_brotli_filter_module.so;
load_module modules/ngx_http_brotli_static_module.so;
http {
# Brotli для динамического сжатия
brotli on;
brotli_comp_level 5;
brotli_types text/plain text/css application/json application/javascript
application/x-javascript text/xml application/xml application/xml+rss
text/javascript image/svg+xml;
# Brotli для статических файлов (если есть .br)
brotli_static on;
server {
listen 443 ssl http2;
server_name example.com;
root /var/www/html;
location / {
try_files $uri $uri/ /index.html;
}
# Статические ресурсы с предварительным сжатием
location ~* \.(js|css|svg|json)$ {
brotli_static on;
add_header Cache-Control "public, max-age=31536000";
}
}
}Brotli directives
| Directive | Default value | Description |
|---|---|---|
brotli | off | Enable/disable |
brotli_comp_level | 6 | Compression level |
brotli_types | text/html | MIME types to compress |
brotli_min_length | 20 | Minimum size (bytes) |
brotli_static | off | Use .br files |
brotli_buffers | 32 4k | Buffers |
Apache: configuring Brotli
mod_brotli
# Ubuntu/Debian
apt install libapache2-mod-brotli
a2enmod brotli
systemctl restart apache2Configuration
<IfModule mod_brotli.c>
AddOutputFilterByType BROTLI_COMPRESS text/html text/plain text/css
AddOutputFilterByType BROTLI_COMPRESS application/javascript application/json
AddOutputFilterByType BROTLI_COMPRESS application/xml image/svg+xml
BrotliCompressionLevel 5
BrotliWindowSize 22
</IfModule>Pre-compressing static files
For the highest compression ratio, compress files during the build:
# Установка brotli CLI
apt install brotli # или npm install -g brotli
# Сжатие с уровнем 11
brotli -k -q 11 style.css -o style.css.br
brotli -k -q 11 app.js -o app.js.brNginx with brotli_static on will serve .br files if the client supports Brotli and requests the corresponding URL.
Verifying that it works
Content-Encoding header
curl -H "Accept-Encoding: br" -I https://example.com/style.cssExpected header: Content-Encoding: br
Response size
# Без сжатия
curl -s -o /dev/null -w "%{size_download}" https://example.com/style.css
# С Brotli
curl -s -H "Accept-Encoding: br" -o /dev/null -w "%{size_download}" https://example.com/style.cssOnline check
HTTP/2 Checker on rechecker.ru checks Brotli and HTTP/2 support for any domain. Nginx Generator helps generate a configuration with Brotli enabled.
Using Brotli and Gzip together
The recommended configuration supports both algorithms:
# Gzip как fallback
gzip on;
gzip_vary on;
gzip_types text/plain text/css application/json application/javascript;
# Brotli с приоритетом
brotli on;
brotli_types text/plain text/css application/json application/javascript;The server checks the Accept-Encoding header and serves Brotli if the client supports it (br in the header); otherwise, it serves Gzip (gzip).
Performance impact
| Resource | Uncompressed | Gzip | Brotli |
|---|---|---|---|
| HTML 50 KB | 50 KB | 12 KB | 10 KB |
| CSS 100 KB | 100 KB | 18 KB | 14 KB |
| JS 200 KB | 200 KB | 55 KB | 42 KB |
Bandwidth savings directly affect LCP and FCP: less data means faster loading.
Recommendations
- Enable Brotli alongside Gzip — a fallback for older clients
- Use level 5 for dynamic compression — a balance between compression and CPU usage
- Pre-compress static files — level 11, with minimal server load
- Check Accept-Encoding — serve Brotli only when the client supports it
- Add Vary: Accept-Encoding — for correct proxy caching
Brotli is a simple way to reduce traffic by 15–25% without changing code. Check your current configuration with HTTP/2 Checker and, if needed, use Nginx Generator to generate a configuration.