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.