Configuration
Load the module once, at the top level of nginx.conf, then turn compression
on in http, server or location. Every directive is described in the
directive reference.
In the event loop
The smallest useful configuration compresses common text types at level 6 and
serves .br files that already exist next to the originals:
load_module modules/ngx_mbrotli.so;
http {
brotli on;
brotli_comp_level 6;
brotli_types text/plain text/css application/javascript application/json
image/svg+xml;
brotli_static on;
}
This is how google/ngx_brotli compresses: in the worker’s event loop,
one response at a time. text/html is always compressed; brotli_types adds
to it.
On a thread pool
Compression on a thread pool, so that long responses and high levels do not
hold up the worker’s event loop. NGINX must be built with --with-threads:
load_module modules/ngx_mbrotli.so;
# The pool named `default` exists without being declared: 32 threads per worker
# process, shared with `aio threads`. To size compression on its own, declare
# a pool of its own and name it in `brotli_thread_pool`:
#
# thread_pool brotli threads=4;
http {
brotli on;
brotli_comp_level 6;
brotli_types text/plain text/css application/javascript application/json
image/svg+xml;
brotli_static on;
# Responses are cut into 1m segments; up to 4 of them per response are
# compressed at once on the pool's threads.
brotli_thread_pool default;
brotli_parallel_segment_size 1m;
brotli_parallel_tasks 4;
server {
listen 80;
location / {
root /var/www;
}
# Small API responses: compressing them in the event loop is cheaper.
location /api/ {
proxy_pass http://127.0.0.1:8080;
brotli_thread_pool off;
}
}
}
See Compressing on a thread pool for how responses are cut into segments and how to size the pool.
Sample configuration
A complete brotli_types list for web content:
load_module modules/ngx_mbrotli.so;
http {
brotli on;
brotli_comp_level 6;
brotli_static on;
brotli_types application/atom+xml application/javascript application/json application/vnd.api+json application/rss+xml
application/vnd.ms-fontobject application/x-font-opentype application/x-font-truetype
application/x-font-ttf application/x-javascript application/xhtml+xml application/xml
font/eot font/opentype font/otf font/truetype image/svg+xml image/vnd.microsoft.icon
image/x-icon image/x-win-bitmap text/css text/javascript text/plain text/xml;
}
Vary: Accept-Encoding
As with google/ngx_brotli, the Vary: Accept-Encoding header is added
only when gzip_vary
is on. Turn it on whenever a cache sits between NGINX and its clients:
gzip_vary on;
Logging the ratio
$brotli_ratio holds the compression ratio of the
response, for example in an access log:
log_format compression '$request "$status" $bytes_sent ratio=$brotli_ratio';
access_log /var/log/nginx/compression.log compression;
Together with ngx_mzstd
ngx_mzstd, the Zstandard module
with the same extensions, can be loaded next to this one, in either order. When
a client accepts both encodings, zstd is chosen, for .zst files over .br
ones and for compression on the fly; clients that accept only Brotli or gzip
get those.
load_module modules/ngx_mzstd.so;
load_module modules/ngx_mbrotli.so;
http {
zstd on;
brotli on;
gzip on;
}
Check that it works
nginx -t && nginx -s reload
curl -s -o /dev/null -D - -H 'Accept-Encoding: br' http://localhost/ | grep -i content-encoding
# content-encoding: br
A response is not compressed when it is shorter than
brotli_min_length, when its type is not in
brotli_types, or when the client’s
Accept-Encoding does not list br.