Zstandard compression for NGINX, in Rust
ngx_mzstd is a drop-in replacement for tokers/zstd-nginx-module: the same two modules, directives, defaults and variable, built on the pure-Rust mzstd 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_mzstd
- Faster where it hurts. At high levels the C module stalls the worker: serving a 1.9 KB JSON response at level 15, ngx_mzstd handles 156× the requests per second in the event loop and 487× on a thread pool. On an 85 KB script at level 3, the thread pool serves 3.7× as many.
- Less memory. Encoders size their tables for the response, so the same JSON at level 19 peaks at 5 MiB per worker instead of 85 MiB.
- The event loop stays free.
zstd_thread_pool default;moves compression onto NGINX threads and spreads one long response over several of them. - Current zstd, no C dependency. The encoder is a Rust port of zstd 1.6.0 that writes the same bytes; nothing to keep in step with the system libzstd.
- Responses browsers accept. The window stays within the 8 MB that RFC 9659 and Chrome allow, even at levels 20–22.
- Nothing to rewrite. Same directives and defaults as
tokers/zstd-nginx-module: swap the
load_moduleline and reload.
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.
A configuration that works
load_module modules/ngx_mzstd.so;
http {
zstd on;
zstd_comp_level 3;
zstd_types text/plain text/css application/javascript application/json
image/svg+xml;
zstd_static on;
# Optional: compress on the built-in `default` thread pool
# (NGINX built with --with-threads).
zstd_thread_pool default;
}
Existing tokers/zstd-nginx-module configurations work unchanged. Replace its
load_module lines with the single line above: one dynamic library holds both
ngx_http_zstd_filter_module (compression on the fly) and
ngx_http_zstd_static_module (pre-compressed .zst files).
Up to date with zstd 1.6.0
mzstd is a pure-Rust port of the zstd 1.6.0 encoder. Compressed in the event loop, a response is byte-identical to what libzstd 1.6.0 writes with the same parameters. There is no C library to install or keep up to date, while tokers/zstd-nginx-module compresses with whichever libzstd the system ships.
Find your path
Vary, and running next to ngx_mbrotli.
Thread poolsKeep high levels and long responses out of the event loop.
MemoryEncoder reuse, retention policies and how much a worker keeps.
DictionariesSmaller short responses for clients you control.
MigratingEvery difference from tokers/zstd-nginx-module.
BenchmarksRequests per second, CPU time and peak memory against the C module.
Measured against the C module
Requests per second for one NGINX worker serving jquery.min.js (85 KB),
relative to tokers/zstd-nginx-module with libzstd 1.5.7:
| Level | Event loop | Thread pool, 4 tasks |
|---|---|---|
| 3 | 1.19× | 3.68× |
| 9 | 1.02× | 3.92× |
| 15 | 3.99× | 15.2× |
| 19 | 2.68× | 10.3× |
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.
What you can rely on
- Same configuration. Directives, defaults and the
$zstd_ratiovariable match tokers/zstd-nginx-module; the few deliberate differences are listed. - Browser-safe by default. The window is limited to 8 MB, as RFC 9659 requires, so levels 20–22 no longer produce responses that Chrome rejects.
- No C library. The encoder is Rust; there is no libzstd to install.
- 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_mbrotli. Load both modules and clients that accept zstd get zstd; the rest get Brotli or gzip. See Together with ngx_mbrotli.