Brotli compression for NGINX, in Rust
ngx_mbrotli is a drop-in replacement for google/ngx_brotli: the same two modules, directives, defaults and variable, built on the pure-Rust mbrotli encoder. It reuses encoders between responses and, with one directive, compresses on an NGINX thread pool instead of in the worker's event loop.
Why ngx_mbrotli
- Several times the throughput. With a 4-thread pool, one worker serves 3.5–4.4 times as many requests as google/ngx_brotli at levels 4–6 on 85 KB to 2.2 MB responses, and 4.5–14 times as many at levels 9–11.
- Faster even in the event loop. On an 85 KB script at level 9 it serves 4.2× the requests per second without a thread pool, and needs half the CPU time per response at level 11 on long ones.
- The event loop stays free.
brotli_thread_pool default;moves compression onto NGINX threads and spreads one long response over several of them. - Same bytes as Google’s encoder, no C dependency. mbrotli is a Rust port that produces the output of Google’s Brotli encoder at every level; there is no C Brotli library to vendor or build.
- Small extras that matter.
brotli_bufferstakes effect, encoders are reused between responses, and uncompressed fonts are compressed in Brotli’s font mode. - Nothing to rewrite. Same directives and defaults as google/ngx_brotli:
swap the
load_modulelines and reload.
A configuration that works
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;
# Optional: compress on the built-in `default` thread pool
# (NGINX built with --with-threads).
brotli_thread_pool default;
}
Existing google/ngx_brotli configurations work unchanged. Replace its two
load_module lines with the single line above: one dynamic library holds both
ngx_http_brotli_filter_module (compression on the fly) and
ngx_http_brotli_static_module (pre-compressed .br files).
NGINX, freenginx and Angie
The module builds against the source tree of any of the three servers and is tested on each of them in CI, with the same directives and behaviour:
| Server | Supported | Tested in CI | Packages |
|---|---|---|---|
| NGINX | 1.25.4 and newer | 1.28, 1.30, 1.31 | nginx.org stable and mainline, Debian, Ubuntu, RHEL, Fedora |
| freenginx | yes | 1.31 | build from source |
| Angie | yes | 1.12 | build from source |
A fork’s own modules, configuration and binary name (angie) are untouched:
configure its source tree with the arguments its binary reports, and load the
module as with NGINX. See Building from source.
Measured against the C module
Requests per second for one NGINX worker serving jquery.min.js (85 KB),
relative to google/ngx_brotli with Brotli 1.1.0:
| Level | Event loop | Thread pool, 4 tasks |
|---|---|---|
| 4 | 1.20× | 4.37× |
| 6 | 1.10× | 3.91× |
| 9 | 4.24× | 13.9× |
| 11 | 1.75× | 6.88× |
One worker on 4 CPUs of an Intel Core i7-13700KF under WSL2, 8 connections, median of three runs. See Benchmarks for every file, CPU time, peak memory and the method.
Find your path
Vary, and running next to ngx_mzstd.
Thread poolsKeep high levels and long responses out of the event loop.
MemoryEncoder reuse, retention policies and how much a worker keeps.
MigratingEvery difference from google/ngx_brotli.
BenchmarksRequests per second, CPU time and peak memory against the C module.
What you can rely on
- Same configuration. Directives, defaults and the
$brotli_ratiovariable match google/ngx_brotli; the few deliberate differences are listed. - No C library. The encoder is Rust; there is no Brotli C library to vendor or build.
- Tested hard. CI runs the integration tests against NGINX 1.28, 1.30 and 1.31, freenginx and Angie, under AddressSanitizer, and the body filter under Miri. See Testing.
- Works with ngx_mzstd. Load both modules and clients that accept zstd get zstd; the rest get Brotli or gzip. See Together with ngx_mzstd.
Installation from packages
Prebuilt packages for amd64 and arm64 are published to Cloudsmith, each built and tested against the exact nginx package it is for. Add the repository, then install the package that matches your nginx:
| Your nginx | Repository | Package | Enabled on install |
|---|---|---|---|
| nginx.org stable | ngx_mbrotli | nginx-module-mbrotli | no |
| nginx.org mainline | ngx_mbrotli-mainline | nginx-module-mbrotli | no |
| Debian’s or Ubuntu’s own | ngx_mbrotli | libnginx-mod-http-mbrotli | yes |
| RHEL-compatible or Fedora’s own | ngx_mbrotli | nginx-mod-mbrotli | yes |
Packages for nginx.org’s nginx are built for every distribution release that nginx.org currently packages (Debian, Ubuntu, RHEL 8 to 10 and compatible, Amazon Linux 2023). Packages for the distributions’ own nginx are built for Debian, Ubuntu, RHEL-compatible (the default AppStream nginx) and the current Fedora releases, where that nginx is 1.25.4 or newer, the oldest version the module builds against: today Debian 13, Ubuntu 26.04, RHEL 10 and Fedora.
Packages are built for NGINX only. For freenginx, Angie or any other build, build the module from source: both forks are tested in CI.
Add the repository
On Debian or Ubuntu:
curl -1sLf 'https://dl.cloudsmith.io/public/m-compressors/ngx_mbrotli/setup.deb.sh' | sudo -E bash
On RHEL, CentOS, AlmaLinux, Rocky Linux, Amazon Linux or Fedora:
curl -1sLf 'https://dl.cloudsmith.io/public/m-compressors/ngx_mbrotli/setup.rpm.sh' | sudo -E bash
For nginx.org mainline, add ngx_mbrotli-mainline instead:
curl -1sLf 'https://dl.cloudsmith.io/public/m-compressors/ngx_mbrotli-mainline/setup.deb.sh' | sudo -E bash
curl -1sLf 'https://dl.cloudsmith.io/public/m-compressors/ngx_mbrotli-mainline/setup.rpm.sh' | sudo -E bash
With nginx.org’s nginx
sudo apt-get install nginx-module-mbrotli # Debian, Ubuntu
sudo dnf install nginx-module-mbrotli # RHEL-compatible, Fedora
Like nginx.org’s own nginx-module-* packages, it installs the module without
enabling it. Add this line to the top of /etc/nginx/nginx.conf and reload
nginx:
load_module modules/ngx_mbrotli.so;
The package requires the exact nginx package it was built for, so upgrade both
together. Its version is <nginx version>+<module version>, as with nginx.org’s
module packages.
With the distribution’s nginx
sudo apt-get install libnginx-mod-http-mbrotli # Debian, Ubuntu
sudo dnf install nginx-mod-mbrotli # RHEL-compatible, Fedora
As with the distribution’s own module packages, the module is enabled on
installation: by /etc/nginx/modules-enabled/50-mod-http-mbrotli.conf on Debian
and Ubuntu, and /usr/share/nginx/modules/mod-mbrotli.conf on RHEL and Fedora.
Configure brotli and reload nginx.
Conflicts
Both modules, ngx_http_brotli_filter_module and
ngx_http_brotli_static_module, come in the one ngx_mbrotli.so. The packages
conflict with those of google/ngx_brotli (nginx-module-brotli, and the
distribution’s own where it has them, such as Debian’s
libnginx-mod-http-brotli-filter and libnginx-mod-http-brotli-static), which
provide the same directives. Remove those first; the configuration itself can
stay as it is (see Migrating).
The packages are built and published by the Publish workflow; see Development and releases.
Package repository hosting is graciously provided by Cloudsmith.
Building from source
Build the module yourself when no package matches your nginx: a custom build, freenginx, Angie, or an nginx.org version older than the packaged ones.
Requirements
- Rust 1.89 or newer;
- libclang (used by
nginx-systo generate bindings); - the NGINX source tree, configured with the same arguments as the NGINX binary
that will load the module, plus
--with-compat. Runnginx -Vto see those arguments. Only./configureis required, notmake. A freenginx or Angie source tree works the same way (for Angie, take the arguments fromangie -V).
The Brotli C library is not needed.
Build
cd nginx-1.30.8
./configure --with-compat <arguments from nginx -V>
cd /path/to/ngx_mbrotli
NGINX_BUILD_DIR=/path/to/nginx-1.30.8/objs cargo build --release
The module is written to target/release/libngx_mbrotli.so
(libngx_mbrotli.dylib on macOS). Load it at the top level of nginx.conf:
load_module /path/to/libngx_mbrotli.so;
brotli_thread_pool and
brotli_parallel_compressors are
compiled in only when the NGINX tree was configured with --with-threads.
NGINX forks
freenginx and Angie are
supported and tested in CI (freenginx 1.31, Angie 1.12) alongside NGINX 1.28,
1.30 and 1.31. Point NGINX_BUILD_DIR at the fork’s configured tree; nothing
else changes.
freenginx:
curl -sSfL https://freenginx.org/download/freenginx-1.31.6.tar.gz | tar xz
cd freenginx-1.31.6
./configure --with-compat <arguments from nginx -V>
cd /path/to/ngx_mbrotli
NGINX_BUILD_DIR=/path/to/freenginx-1.31.6/objs cargo build --release
Angie, which names its binary angie:
curl -sSfL https://download.angie.software/files/angie-1.12.6.tar.gz | tar xz
cd angie-1.12.6
./configure --with-compat <arguments from angie -V>
cd /path/to/ngx_mbrotli
NGINX_BUILD_DIR=/path/to/angie-1.12.6/objs cargo build --release
Then add load_module /path/to/libngx_mbrotli.so; to the fork’s main
configuration file (/etc/angie/angie.conf for Angie) and check it with
nginx -t or angie -t.
Vendored NGINX
To build against a vendored NGINX instead, enable the vendored feature (see
nginx-src). Prefer building against the exact
sources and configuration that run in production: NGINX refuses to load a
module that is not binary compatible with it.
Next
Configure the module, then check it with
nginx -t and a request:
curl -s -o /dev/null -D - -H 'Accept-Encoding: br' http://localhost/ | grep -i content-encoding
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.
Compressing on a thread pool
By default the module compresses in the worker’s event loop, as google/ngx_brotli does. While one response is being compressed, every other connection of that worker waits. At low levels and for short responses that is cheap; at levels 9–11, or for responses of a megabyte and more, it is not.
One directive moves compression onto an NGINX thread pool:
brotli_thread_pool default;
NGINX must be built with --with-threads (nginx.org’s packages and most
distributions’ are). In the benchmarks, one worker with 4
threads serves 3.5–4.4 times as many requests as google/ngx_brotli at levels
4–6 on 85 KB–2.2 MB responses, and 4.5–14 times as many at levels 9–11.
How a response is compressed
- The response is cut into segments of
brotli_parallel_segment_size(1mby default). - Up to
brotli_parallel_taskssegments (4) of one response are compressed at once, each by one of the pool’s threads. - Compressed segments are sent in order as they complete, while the next segments are read. When all tasks are busy or waiting to be sent, NGINX stops reading the response until the first of them has been sent.
A response that fits in one segment is compressed in one piece by one thread.
A flush (for example with proxy_buffering off) ends the current segment
early. The response is still one standard Brotli stream.
What it costs in ratio
| Level | Each segment but the first | Back-references across segments |
|---|---|---|
| 2–9 | Also runs through the input before it: a quarter of the segment size at most, never more than the window (256 KiB for 1m segments with the default 512k window) | Yes, into that overlap |
| 0, 1, 10, 11 | Starts from nothing | No |
Either way, a response longer than one segment compresses worse than in one piece: on the 1 MB and 2.2 MB files of the benchmarks, by 0.1–0.6% at levels 4–9 and by 3.8–5.2% at level 11. At levels 4–9 the overlap also costs CPU time; at level 11, 1 MiB segments compress faster than a whole file, so the pool uses less CPU per response than the event loop.
Larger segments compress better and, at levels 2–9, run less input twice, but fewer of a response’s segments are compressed at once, and each encoder needs more memory.
Choose a pool
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 in the main context and name it:
thread_pool brotli threads=4;
http {
brotli_thread_pool brotli;
}
Every worker process has its own threads, so worker_processes times the
pool’s threads run at once. A pool with as many threads as the worker has
CPUs to spare is a good start.
When to leave it off
Turn it off, per location, where compressing in the event loop is cheaper
than handing work to a thread:
location /api/ {
proxy_pass http://127.0.0.1:8080;
brotli_thread_pool off;
}
Short responses at low levels cost little CPU either way: in the
benchmarks, small.json (1.9 KB) at
level 4 takes 0.032 ms in the event loop and 0.051 ms on the pool.
Memory
A response being compressed holds up to brotli_parallel_tasks + 1 segments
of input, their compressed output, and an encoder for each segment. At high
levels on long responses this is the largest cost of the pool: in the
benchmarks one worker serving libraries.json
(2.2 MB) at level 9 peaks at 563 MiB with 1 task and 1,089 MiB with 4.
To bound it:
- lower
brotli_parallel_tasks, so fewer segments of one response are in flight; - lower
brotli_parallel_segment_size, so each segment and its encoder are smaller (at the cost of ratio); - set
brotli_parallel_compressors, which limits how many compressors each worker keeps segment encoders for; - pick a
brotli_retention_policyother thanaggressive.
See Memory and encoder reuse for how idle compressors are kept.
Tuning summary
| Goal | Change |
|---|---|
| Better ratio on long responses | Raise brotli_parallel_segment_size (up to 16m) |
| More parallelism per response | Raise brotli_parallel_tasks, and give the pool the threads |
| Less memory per response | Lower brotli_parallel_tasks or brotli_parallel_segment_size |
| Less memory kept between responses | Lower brotli_parallel_compressors or use a stricter brotli_retention_policy |
| More reuse on a busy, large pool | Raise brotli_parallel_compressors towards the pool’s threads, with brotli_idle_encoders |
Isolate compression from aio threads | Declare a pool of its own with thread_pool |
How it works describes how segments are scheduled inside NGINX.
Memory and encoder reuse
An encoder’s working memory (window, match finder and buffers) grows with the level and the window. google/ngx_brotli allocates it for every response and frees it when the response is complete. ngx_mbrotli keeps idle encoders in each worker, so the next response can reuse what a previous one allocated.
Three directives control this:
| Directive | Default | What it sets |
|---|---|---|
brotli_reuse_memory | on | Whether encoders are reused at all |
brotli_retention_policy | aggressive | How much memory an idle encoder keeps |
brotli_idle_encoders | 4 | How many idle encoders each worker keeps |
Peak memory still follows the number of responses being compressed at once,
times the working set that brotli_comp_level and brotli_window need. In the
benchmarks, a worker compressing in the event loop
peaks at most 4 MiB above google/ngx_brotli, and below it on
jquery.min.js at level 9 (8 MiB against 36 MiB).
Retention policies
| Policy | Kept between responses | Use when |
|---|---|---|
aggressive | Every buffer | Throughput matters most and workers may keep memory for their largest responses at level 11 with a 16m window |
current_config | Buffers that suit the current level and window | Locations use different levels or windows |
bounded <size> | Buffers while they fit in size | Memory per worker must stay below a known bound |
release_all | Nothing but the encoder itself | Memory matters more than allocation cost |
http {
brotli on;
# Keep at most 8 MB of buffers per idle encoder.
brotli_retention_policy bounded 8m;
location /downloads/ {
brotli_comp_level 11;
brotli_window 16m;
# Large, rare responses: do not keep their memory.
brotli_retention_policy release_all;
}
}
Idle encoders are reused only by locations with the same retention policy, so a
worker that serves locations with several policies may need a higher
brotli_idle_encoders. When a worker’s slots are full, the least recently used
encoder is freed.
Allocating per response
To allocate per response, as google/ngx_brotli does:
brotli_reuse_memory off;
Every response then gets a new encoder that is freed when the response is
complete. brotli_idle_encoders 0 has the same effect for the whole http
block.
With a thread pool
With brotli_thread_pool, the unit of reuse is a
compressor: an idle compressor keeps the encoders of up to
brotli_parallel_tasks segments, and the retention policy applies to all of
them together.
- A response holds its compressor until it has been sent, including while its segments wait for a free thread.
- Each worker keeps encoders for at most
brotli_parallel_compressorscompressors, idle or in use. Other responses allocate encoders for their segments and free them once they have been sent. - Idle compressors are kept separately from idle event-loop encoders, up to
brotli_idle_encodersof them.
A higher brotli_parallel_compressors lets more responses reuse encoders, and
costs the memory of their encoders: in single runs with a 4-thread pool and 32
connections on jquery.min.js at level 9, 32 served 8% more requests per
second than the default, and the worker peaked at 168 MiB instead of 91 MiB.
Migrating from google/ngx_brotli
ngx_mbrotli provides the same two modules, directives, defaults and variable as google/ngx_brotli, so an existing configuration keeps working unchanged.
Steps
-
Remove google/ngx_brotli’s packages (
nginx-module-brotli, or the distribution’s own, such as Debian’slibnginx-mod-http-brotli-filterandlibnginx-mod-http-brotli-static) or its twoload_modulelines. Its two.sofiles become one. -
Install or build ngx_mbrotli and load it:
load_module modules/ngx_mbrotli.so; -
Run
nginx -tand reload. The rest of the configuration stays as it is.
There is no Brotli C library or deps/brotli submodule to build any more.
What behaves differently
brotli_bufferstakes effect. google/ngx_brotli accepts the directive but ignores it; here it works the same way asgzip_buffers.- Encoders are reused by default
(
brotli_reuse_memory), each keeping all of its working memory between responses (brotli_retention_policy). Setbrotli_reuse_memory offto allocate per response as google/ngx_brotli does. Peak memory still follows the number of responses being compressed at once, times the working set thatbrotli_comp_levelandbrotli_windowneed. See Memory and encoder reuse. - Responses can be compressed on a thread pool
(
brotli_thread_pool), in segments that are compressed in parallel. See Compressing on a thread pool. - The size hint comes from
Content-Length. When the length is known it is passed to the encoder, which uses it to choose a match finder at levels 4–9. - Fonts are compressed in Brotli’s font mode. Responses whose type is an
uncompressed TrueType or OpenType font (
font/ttf,font/otf,font/sfnt,font/collection,application/vnd.ms-fontobject,application/x-font-ttfand similar) use font mode, which makes them about 0.1–0.3% smaller at levels 4–11 and does not affect speed. All other responses, including WOFF and WOFF2 fonts, which are already compressed, use generic mode as google/ngx_brotli does. Text mode is not used: it does not change Brotli’s output. - The pure-Rust mbrotli encoder replaces the C library. It produces the same bytes as Google’s encoder at every level, so you do not need to vendor or build the C Brotli library.
New directives
| Directive | Context | Default |
|---|---|---|
brotli_reuse_memory | http, server, location | on |
brotli_retention_policy | http, server, location | aggressive |
brotli_idle_encoders | http | 4 |
brotli_thread_pool | http, server, location | off |
brotli_parallel_segment_size | http, server, location | 1m |
brotli_parallel_tasks | http, server, location | 4 |
brotli_parallel_compressors | http | 4 |
Reporting a difference
If the module behaves differently from google/ngx_brotli with the same configuration, and the difference is not listed above, open an issue with the Differs from google/ngx_brotli template.
Directives
Every directive of ngx_http_brotli_filter_module and
ngx_http_brotli_static_module. Directives marked Not in google/ngx_brotli
are extensions; the rest behave as in
google/ngx_brotli, except where
Migrating says otherwise.
| Directive | Default | Context |
|---|---|---|
brotli_static | off | http, server, location |
brotli | off | http, server, location, if |
brotli_types | text/html | http, server, location |
brotli_buffers | 32 4k|16 8k | http, server, location |
brotli_comp_level | 6 | http, server, location |
brotli_window | 512k | http, server, location |
brotli_min_length | 20 | http, server, location |
brotli_reuse_memory | on | http, server, location |
brotli_retention_policy | aggressive | http, server, location |
brotli_idle_encoders | 4 | http |
brotli_thread_pool | off | http, server, location |
brotli_parallel_segment_size | 1m | http, server, location |
brotli_parallel_tasks | 4 | http, server, location |
brotli_parallel_compressors | 4 | http |
brotli_static
- Syntax
brotli_static on|off|always- Default
off- Context
http,server,location
Enables or disables checking for pre-compressed files with the .br extension.
With always, the pre-compressed file is served whether or not the client
supports it.
To serve file.js.br in place of file.js, create it next to the original with brotli -q 11 file.js (brotli keeps the input file).
brotli
- Syntax
brotli on|off- Default
off- Context
http,server,location,if
Enables or disables on-the-fly compression of responses.
brotli_types
- Syntax
brotli_types <mime_type> [..]- Default
text/html- Context
http,server,location
Compresses responses with the listed MIME types as well as text/html, which is
always compressed. The special value * matches any MIME type.
brotli_buffers
- Syntax
brotli_buffers <number> <size>- Default
32 4k|16 8k- Context
http,server,location
Sets the number and size of the buffers that hold compressed output. The default
is one memory page per buffer, up to 128k in total.
google/ngx_brotli accepts this directive but ignores it; here it works the same
way as gzip_buffers.
brotli_comp_level
- Syntax
brotli_comp_level <level>- Default
6- Context
http,server,location
Sets the Brotli quality (compression level), from 0 to 11.
brotli_window
- Syntax
brotli_window <size>- Default
512k- Context
http,server,location
Sets the Brotli window size. Acceptable values are 1k, 2k, 4k, 8k, 16k,
32k, 64k, 128k, 256k, 512k, 1m, 2m, 4m, 8m and 16m.
When the response length is known, a smaller window is used if it is enough.
brotli_min_length
- Syntax
brotli_min_length <length>- Default
20- Context
http,server,location
Sets the minimum length of a response that will be compressed. The length comes
only from the Content-Length response header.
brotli_reuse_memory
- Syntax
brotli_reuse_memory on|off- Default
on- Context
http,server,location
Enables or disables reusing encoders between responses. When on, each worker
keeps up to brotli_idle_encoders idle encoders, so a response can reuse the
window, match finder and buffers a previous one allocated. How much memory an idle encoder keeps is set by
brotli_retention_policy. When off, every response gets a new encoder that is
freed when the response is complete, as with google/ngx_brotli.
Not in google/ngx_brotli.
brotli_retention_policy
- Syntax
brotli_retention_policy aggressive | current_config | release_all | bounded <size>- Default
aggressive- Context
http,server,location
Sets how much working memory an encoder keeps between responses. It matters only
when brotli_reuse_memory is on.
aggressivekeeps every buffer, so a worker allocates only what its largest responses grow into. This is the fastest option, but each worker keeps memory for its largest responses atbrotli_comp_level11 with a 16mbrotli_window.current_configkeeps buffers only while they suit the current level and window. They are released when the encoder is reused for a location with different settings.bounded <size>keeps buffers while they fit withinsize, and releases them after a response that grew past it.release_allkeeps nothing between responses; only the encoder itself is reused.
Idle encoders are reused only by locations with the same retention policy; when a
worker’s brotli_idle_encoders slots are full, the least recently used encoder
is freed.
With brotli_thread_pool, an idle compressor keeps the encoders of up to
brotli_parallel_tasks segments, and the policy applies to all of them
together. A response holds its compressor until it has been sent, including
while its segments wait for a free thread, so a worker keeps encoders for at
most brotli_parallel_compressors compressors, idle or in use; other responses
allocate encoders for their segments and free them once they have been sent.
Not in google/ngx_brotli.
brotli_idle_encoders
- Syntax
brotli_idle_encoders <number>- Default
4- Context
http
Sets how many idle encoders each worker keeps for reuse with
brotli_reuse_memory on, from 0 to 1024; when they are all taken, the least
recently used one is freed. Compressors for brotli_thread_pool are kept
separately, up to the same number. With 0 nothing is kept, so every response
gets a new encoder.
Idle encoders are reused only by locations with the same
brotli_retention_policy, so a worker that serves locations with several
policies may need more of them.
Not in google/ngx_brotli.
brotli_thread_pool
- Syntax
brotli_thread_pool <name> | off- Default
off- Context
http,server,location
Compresses responses on the named thread pool instead of in the worker’s event
loop, so that a long response or a high brotli_comp_level does not hold up the
worker’s other connections. The pool is declared with
thread_pool in the
main context; a pool named default with 32 threads exists without being
declared. Requires NGINX built with --with-threads.
A response is cut into segments of brotli_parallel_segment_size, and up to
brotli_parallel_tasks of them are compressed on the pool’s threads at once. They
are sent in order as they complete, while the next segments are read. A response
that fits in one segment is compressed in one piece by one thread. At
brotli_comp_level 2–9, every segment but the first also runs through the input
before it, a quarter of brotli_parallel_segment_size at most and never more
than the window, so that its back-references can reach into it: 256 KiB for 1m
segments with the default 512k window. At other levels a segment starts from
nothing, and back-references do not cross segment boundaries. Either way, a
response longer than one segment compresses worse than in one piece: on the
1 MB and 2.2 MB files of the benchmarks, by 0.1–0.6% at levels
4–9 and by 3.8–5.2% at level 11. A flush (for example with proxy_buffering off) ends the current
segment early.
Every worker process has its own threads, so worker_processes times the pool’s
threads run at once. A response being compressed holds up to
brotli_parallel_tasks + 1 segments of input, their compressed output, and an
encoder for each segment.
Not in google/ngx_brotli.
See Compressing on a thread pool.
brotli_parallel_segment_size
- Syntax
brotli_parallel_segment_size <size>- Default
1m- Context
http,server,location
Sets the size of the segments a response is cut into for brotli_thread_pool,
from 64k to 16m. At levels 2–9 a segment can refer back to at most a quarter
of its size of the input before it, and at other levels to none of it (see
brotli_thread_pool), so the larger the segments, the
better a response compresses and, at levels 2–9, the less input is run through
twice, but the fewer of its segments are compressed at once, and the more memory
each of their encoders needs. Smaller segments spread responses over more
threads and compress worse.
Not in google/ngx_brotli.
brotli_parallel_tasks
- Syntax
brotli_parallel_tasks <number>- Default
4- Context
http,server,location
Sets how many segments of one response may be compressed, or wait to be sent, at
once with brotli_thread_pool, from 1 to 1024. When they all are, NGINX stops
reading the response until the first of them has been sent.
Not in google/ngx_brotli.
brotli_parallel_compressors
- Syntax
brotli_parallel_compressors <number>- Default
4- Context
http
Sets for how many compressors used with brotli_thread_pool each worker keeps
segment encoders, from 0 to 1024, counting both idle ones and ones held by
responses. A response holds its compressor until it has been sent, including
while its segments wait for a free thread, so without a limit every response in
flight could keep the encoders a previous response grew. Responses beyond the
limit allocate encoders for their segments and free them once they have been
sent. Idle compressors are also limited by brotli_idle_encoders. Requires
NGINX built with --with-threads.
A higher number lets more responses reuse encoders, and costs the memory of
their encoders: in single runs with a 4-thread pool and 32 connections on
jquery.min.js at level 9, 32 served 8% more requests per second than the
default, and the worker peaked at 168 MiB instead of 91 MiB. A pool whose
threads often all compress at once, such as default with 32 threads, may
benefit from a number closer to its threads, together with a higher
brotli_idle_encoders.
Not in google/ngx_brotli.
Variables
$brotli_ratio
The achieved compression ratio: the original size divided by the compressed
size, to two decimal places (for example 3.59).
It is available in the log phase, for example in
log_format;
see Logging the ratio.
Benchmarks
One NGINX worker compresses static files on the fly for 8 connections at once, in four setups:
- google/ngx_brotli: as shipped, commit
a71f9312with the Brotli 1.1.0 it pins as a submodule. - ngx_mbrotli, event loop: ngx_mbrotli with its default settings, compressing in the worker’s event loop like google/ngx_brotli.
- ngx_mbrotli, pool, 1 task and 4 tasks: ngx_mbrotli with
brotli_thread_poolon a pool of 4 threads andbrotli_parallel_tasks 1or4(the default), so that one response is compressed by up to 1 or 4 threads at once, in 1m segments.
Only brotli_comp_level changes from row to row. The worker and its pool share 4
CPUs. Each number is the median of three runs under WSL2 (see
Hardware, build and method): good for comparing
the setups with each other, less so as absolute numbers.
Requests per second
Higher is better. In brackets, relative to google/ngx_brotli.
| Response | Level | google/ngx_brotli | ngx_mbrotli, event loop | ngx_mbrotli, pool, 1 task | ngx_mbrotli, pool, 4 tasks |
|---|---|---|---|---|---|
| small.json, 1.9 KB | 4 | 28,370 | 31,114 (1.10×) | 41,653 (1.47×) | 42,238 (1.49×) |
| small.json, 1.9 KB | 6 | 12,750 | 28,301 (2.22×) | 41,624 (3.26×) | 41,483 (3.25×) |
| small.json, 1.9 KB | 9 | 23,218 | 24,194 (1.04×) | 40,078 (1.73×) | 36,406 (1.57×) |
| small.json, 1.9 KB | 11 | 613 | 965 (1.57×) | 3,717 (6.06×) | 3,714 (6.06×) |
| jquery.min.js, 85 KB | 4 | 955 | 1,145 (1.20×) | 4,196 (4.39×) | 4,177 (4.37×) |
| jquery.min.js, 85 KB | 6 | 655 | 717 (1.10×) | 2,603 (3.98×) | 2,559 (3.91×) |
| jquery.min.js, 85 KB | 9 | 104 | 442 (4.24×) | 1,552 (14.9×) | 1,452 (13.9×) |
| jquery.min.js, 85 KB | 11 | 14.5 | 25.4 (1.75×) | 100.0 (6.88×) | 99.9 (6.88×) |
| react-dom.development.js, 1.03 MB | 4 | 130 | 152 (1.17×) | 443 (3.41×) | 448 (3.45×) |
| react-dom.development.js, 1.03 MB | 6 | 61.5 | 79.4 (1.29×) | 261 (4.24×) | 265 (4.30×) |
| react-dom.development.js, 1.03 MB | 9 | 24.0 | 42.6 (1.78×) | 111 (4.63×) | 128 (5.33×) |
| react-dom.development.js, 1.03 MB | 11 | 1.00 | 1.70 (1.70×) | 9.93 (9.93×) | 9.96 (9.96×) |
| libraries.json, 2.2 MB | 4 | 67.1 | 76.9 (1.15×) | 232 (3.45×) | 240 (3.58×) |
| libraries.json, 2.2 MB | 6 | 31.9 | 40.8 (1.28×) | 135 (4.24×) | 133 (4.16×) |
| libraries.json, 2.2 MB | 9 | 14.0 | 21.6 (1.54×) | 65.1 (4.65×) | 63.5 (4.54×) |
| libraries.json, 2.2 MB | 11 | 0.520 | 0.900 (1.73×) | 5.27 (10.1×) | 5.39 (10.4×) |
CPU time per request
The worker’s user and system CPU time per request, including the pool’s threads, in ms. Lower is better. On the pool, responses longer than one segment (1 MB and more here) cost more at levels 4–9, because every segment but the first also runs through part of the input before it, and less at level 11, because 1 MiB segments compress faster than a whole file.
| Response | Level | google/ngx_brotli | ngx_mbrotli, event loop | ngx_mbrotli, pool, 1 task | ngx_mbrotli, pool, 4 tasks |
|---|---|---|---|---|---|
| small.json, 1.9 KB | 4 | 0.035 | 0.032 | 0.051 | 0.050 |
| small.json, 1.9 KB | 6 | 0.078 | 0.035 | 0.054 | 0.054 |
| small.json, 1.9 KB | 9 | 0.043 | 0.041 | 0.071 | 0.075 |
| small.json, 1.9 KB | 11 | 1.63 | 1.04 | 1.06 | 1.06 |
| jquery.min.js, 85 KB | 4 | 1.05 | 0.875 | 0.920 | 0.922 |
| jquery.min.js, 85 KB | 6 | 1.53 | 1.40 | 1.51 | 1.53 |
| jquery.min.js, 85 KB | 9 | 9.74 | 2.27 | 2.55 | 2.72 |
| jquery.min.js, 85 KB | 11 | 70.5 | 39.9 | 40.1 | 40.1 |
| react-dom.development.js, 1.03 MB | 4 | 7.76 | 6.60 | 9.00 | 8.92 |
| react-dom.development.js, 1.03 MB | 6 | 16.5 | 12.7 | 15.3 | 15.1 |
| react-dom.development.js, 1.03 MB | 9 | 43.4 | 23.9 | 35.1 | 31.4 |
| react-dom.development.js, 1.03 MB | 11 | 1,188 | 651 | 405 | 408 |
| libraries.json, 2.2 MB | 4 | 15.1 | 13.1 | 17.2 | 16.7 |
| libraries.json, 2.2 MB | 6 | 32.1 | 24.8 | 29.5 | 30.2 |
| libraries.json, 2.2 MB | 9 | 75.7 | 47.6 | 61.6 | 63.5 |
| libraries.json, 2.2 MB | 11 | 2,522 | 1,207 | 765 | 766 |
Peak memory
The worker’s peak resident set size (VmHWM), in MiB. Lower is better. On the
thread pool, every response in flight holds encoders for its segments.
| Response | Level | google/ngx_brotli | ngx_mbrotli, event loop | ngx_mbrotli, pool, 1 task | ngx_mbrotli, pool, 4 tasks |
|---|---|---|---|---|---|
| small.json, 1.9 KB | 4 | 5 | 6 | 9 | 9 |
| small.json, 1.9 KB | 6 | 5 | 6 | 15 | 15 |
| small.json, 1.9 KB | 9 | 6 | 7 | 22 | 22 |
| small.json, 1.9 KB | 11 | 6 | 6 | 21 | 21 |
| jquery.min.js, 85 KB | 4 | 5 | 6 | 22 | 22 |
| jquery.min.js, 85 KB | 6 | 7 | 8 | 46 | 45 |
| jquery.min.js, 85 KB | 9 | 36 | 8 | 48 | 43 |
| jquery.min.js, 85 KB | 11 | 9 | 11 | 67 | 68 |
| react-dom.development.js, 1.03 MB | 4 | 9 | 12 | 113 | 141 |
| react-dom.development.js, 1.03 MB | 6 | 11 | 14 | 149 | 169 |
| react-dom.development.js, 1.03 MB | 9 | 39 | 41 | 578 | 500 |
| react-dom.development.js, 1.03 MB | 11 | 25 | 28 | 238 | 264 |
| libraries.json, 2.2 MB | 4 | 9 | 13 | 137 | 220 |
| libraries.json, 2.2 MB | 6 | 10 | 14 | 174 | 273 |
| libraries.json, 2.2 MB | 9 | 39 | 42 | 563 | 1,089 |
| libraries.json, 2.2 MB | 11 | 28 | 30 | 376 | 492 |
Hardware, build and method
Hardware
- Intel Core i7-13700KF.
- Ubuntu 22.04.5 under WSL2 2.7.14.0, kernel 6.18.33.2-microsoft-standard-WSL2, on Windows 10.0.26300. WSL2 shows 12 identical cores of 2 threads each and does not expose which of them are performance cores.
Build
- NGINX 1.30.6,
./configure --with-compat --with-threads --without-http_rewrite_module, gcc 11.4.0, both modules dynamic. - google/ngx_brotli at commit
a71f9312c2deb28875acc7bacfdd5695a111aa53, with itsdeps/brotlisubmodule ated738e842d2fbdf2d6459e39267a633c4a9b2f5d(Brotli 1.1.0). - ngx_mbrotli 0.4.5 with mbrotli 0.5.20,
cargo build --release(rustc 1.99.0).
Configuration
worker_processes 1, pinned to 4 CPUs on different physical cores, and
thread_pool cp threads=4. On the pool, brotli_thread_pool cp with
brotli_parallel_tasks 1 or 4. Everything else is left at its default, including
brotli_parallel_segment_size 1m, brotli_parallel_compressors 4 and
brotli_retention_policy aggressive. Files are served from tmpfs.
Load
- wrk 4.1.0 over loopback, without TLS, from 4 other physical cores, with 2 threads and 8 connections, for 4 seconds (8 seconds for files over 1 MB, 5 times longer at level 11), after a 1-second warm-up.
- Each run starts NGINX anew and waits until the worker has been idle for 300 ms. Three runs in a shuffled order; the tables show the median.
- CPU time is read from
/proc/<pid>/statof the worker, including the pool’s threads, and peak memory fromVmHWMin/proc/<pid>/status. - Every response, for every setup, level and file, was decoded with curl’s Brotli decoder and compared with the original file by SHA-256.
Files
jQuery 3.7.1 (jquery.min.js) and React DOM 18.3.1
(react-dom.development.js) from cdnjs, the full cdnjs API listing
(libraries.json), and its first 10 entries (small.json), fetched on
2026-10-20. Measured on 2026-11-01.
How it works
The module is a Rust crate built with ngx-rust. It drives the mbrotli encoder from NGINX’s filter chain; the bytes the encoder writes are mbrotli’s concern, and changes to them belong in that repository.
Place in the filter chain
The filter sits in the same place in the filter chain as
google/ngx_brotli’s, just above the gzip filter. The dynamic library
exports an ngx_module_order table so that NGINX inserts it there.
C bit-fields
All access to C bit-fields (request, buffer, variable and open-file flags) goes
through src/bitfields.c, compiled against the same headers as NGINX. The bit-field accessors that bindgen generates for ngx_http_request_t
do not match the C compiler’s layout in NGINX 1.30 (they are two bits off from
uri_changes onwards), so they cannot be used safely.
The body filter
The body filter (src/body.rs) reaches NGINX only through a small Host
trait. In production the trait is implemented over NGINX’s buffers and chains;
in tests it is a model of that API (src/body/tests.rs), so the filter’s logic
runs, under Miri too, without NGINX. See Testing.
Thread pool tasks
With brotli_thread_pool, each segment becomes
an ngx_thread_task_t. Its completion handler runs in the event loop and calls
the brotli filter to send what is ready, then runs the connection’s write
handler, as a write event would.
While tasks are posted, the request is blocked (r->main->blocked), so that
terminating it waits for them, and stays buffered, so that it is not finalized
before the last completion handler has run, even when the response has already
been sent.
See Compressing on a thread pool for what this means for configuration.
Testing
Unit tests
Unit tests do not need a running NGINX, only a configured source tree:
NGINX_BUILD_DIR=/path/to/nginx/objs cargo test
Integration tests
The integration tests in t/ use the
nginx-tests framework and need the
brotli command-line tool and an nginx binary built with make:
git clone https://github.com/nginx/nginx-tests
TEST_NGINX_BINARY=/path/to/nginx/objs/nginx \
TEST_NGINX_GLOBALS="load_module $PWD/target/release/libngx_mbrotli.so;" \
prove -I nginx-tests/lib t/
For Angie, point TEST_NGINX_BINARY at objs/angie.
Miri
The body filter (src/body.rs) reaches NGINX only through a small Host
trait, so Miri runs it against a model of NGINX’s buffer and chain API
(src/body/tests.rs):
rustup +nightly component add miri rust-src
cargo +nightly miri test
The model frees drained input buffers and chain links, and leaves output buffers uninitialized, so stale pointers and reads of unwritten output show up as undefined behaviour. Its thread pool is either a queue that the tests run and report on their own schedule, or real threads, so that Miri also looks for data races between the filter and the segments being encoded.
What CI runs
CI (.github/workflows/ci.yml) runs on pull requests and pushes to main:
- rustfmt and clippy;
- unit and integration tests against NGINX 1.28, 1.30 and 1.31, freenginx 1.31
and Angie 1.12, built with
--with-threads; clippy also checks a build without it; - the integration tests under AddressSanitizer and LeakSanitizer, with NGINX
and the module (including the Rust standard library) instrumented, and
NGX_DEBUG_PALLOCso that each pool allocation is checked on its own; - the unit tests under Miri.
The documentation website is built and its links checked by a separate workflow.
Development and releases
Bug reports and pull requests are welcome. Read CONTRIBUTING.md for the setup and the checks a change must pass, and follow the Code of Conduct.
- If the module behaves differently from google/ngx_brotli with the same configuration, open an issue with the Differs from google/ngx_brotli template.
- Problems with the Brotli encoder itself belong in mbrotli.
- Report security vulnerabilities privately as described in SECURITY.md, not as public issues.
Changesets
Changelogs and versions are managed with Sampo.
Every user-facing change needs a changeset, a short Markdown file in
.sampo/changesets/:
cargo install sampo
sampo add # pick the bump level (patch, minor, major) and describe the change
Releases
On every push to main, the release workflow (.github/workflows/release.yml)
collects the pending changesets into a Release (main) pull request that
bumps the version in Cargo.toml and updates CHANGELOG.md. Merging that pull
request tags the release (vX.Y.Z) and publishes a GitHub Release with the
changelog entry. The crate is not published to crates.io.
For the workflow to open pull requests, enable Settings → Actions → General → Allow GitHub Actions to create and approve pull requests.
Packages
Packages are published by hand: run the Publish workflow
(.github/workflows/publish.yml) on the release tag. It builds a package for
every target, tests each in a clean container of its distribution (install,
nginx -t, a brotli-compressed response with and without a thread pool, a
pre-compressed .br file with brotli_static,
removal), and pushes them to Cloudsmith only if all of them pass.
- With
dry_runit builds and tests without pushing, also from a branch. - A rerun on the same tag skips the packages already published.
- Targets come from nginx.org and the distributions at run time
(
.github/scripts/publish-matrix.py, which also says how to add a distribution);nginx_stableandnginx_mainlineoverride the nginx.org versions.
License
MIT; see LICENSE. The directives and their behaviour follow google/ngx_brotli (BSD 2-Clause).
Documentation website
This book is built with mdBook from the
Markdown files in docs/ and published to
https://m-compressors.github.io/ngx_mbrotli/ by GitHub Pages. Edit those files
directly; docs/SUMMARY.md sets the order of the sidebar.
Build locally
cargo install mdbook --version 0.5.4 --locked
mdbook build
python3 scripts/check-book-links.py
The book is written to target/book/. To preview it while editing:
mdbook serve --open
scripts/check-book-links.py checks that every local link and every
#anchor in the rendered book points to a page and heading that exist. It
needs only Python 3 and does not fetch external links.
Writing a page
- A new page must be listed in
docs/SUMMARY.md; the build does not create missing files. - A new or changed directive goes in Directives, in the same form as the existing ones (syntax, default, context, behaviour), and in the table at the top of that page. A new extension also goes in Migrating.
- Link to a directive as
directives.md#brotli_window: mdBook makes each heading’s text its anchor. - Link to files outside
docs/by their URL on GitHub, since onlydocs/is published.
Theme
book-theme/custom.css extends mdBook’s built-in light and navy themes
with the palette of the mzstd and mbrotli books, and styles tables, the
directive summaries and the landing page (docs/introduction.md). It is plain
CSS with no build step.
Publishing
In the repository settings, select Pages → Build and deployment → Source →
GitHub Actions once. .github/workflows/pages.yml then:
- builds the book and checks its links on pull requests and pushes to
mainthat touch the documentation; - deploys it only on pushes to
mainor a manual run onmain.
Pull requests are validated without publishing and without Pages write permissions. There is no generated-content branch.