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.
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_mzstd | nginx-module-mzstd | no |
| nginx.org mainline | ngx_mzstd-mainline | nginx-module-mzstd | no |
| Debian’s or Ubuntu’s own | ngx_mzstd | libnginx-mod-http-mzstd | yes |
| RHEL-compatible or Fedora’s own | ngx_mzstd | nginx-mod-mzstd | 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_mzstd/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_mzstd/setup.rpm.sh' | sudo -E bash
For nginx.org mainline, add ngx_mzstd-mainline instead:
curl -1sLf 'https://dl.cloudsmith.io/public/m-compressors/ngx_mzstd-mainline/setup.deb.sh' | sudo -E bash
curl -1sLf 'https://dl.cloudsmith.io/public/m-compressors/ngx_mzstd-mainline/setup.rpm.sh' | sudo -E bash
With nginx.org’s nginx
sudo apt-get install nginx-module-mzstd # Debian, Ubuntu
sudo dnf install nginx-module-mzstd # 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_mzstd.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-mzstd # Debian, Ubuntu
sudo dnf install nginx-mod-mzstd # 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-mzstd.conf on Debian
and Ubuntu, and /usr/share/nginx/modules/mod-mzstd.conf on RHEL and Fedora.
Configure zstd and reload nginx.
Conflicts
The packages conflict with those of tokers/zstd-nginx-module
(nginx-module-zstd, and the distribution’s own where it has one), 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 zstd C library is not needed.
Build
cd nginx-1.30.8
./configure --with-compat <arguments from nginx -V>
cd /path/to/ngx_mzstd
NGINX_BUILD_DIR=/path/to/nginx-1.30.8/objs cargo build --release
The module is written to target/release/libngx_mzstd.so
(libngx_mzstd.dylib on macOS). Load it at the top level of nginx.conf:
load_module /path/to/libngx_mzstd.so;
zstd_thread_pool and
zstd_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_mzstd
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_mzstd
NGINX_BUILD_DIR=/path/to/angie-1.12.6/objs cargo build --release
Then add load_module /path/to/libngx_mzstd.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: zstd' 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 3 and
serves .zst files that already exist next to the originals:
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;
}
This is how tokers/zstd-nginx-module compresses: in the worker’s event loop,
one response at a time. text/html is always compressed; zstd_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_mzstd.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 `zstd_thread_pool`:
#
# thread_pool zstd threads=4;
http {
zstd on;
zstd_comp_level 3;
zstd_types text/plain text/css application/javascript application/json
image/svg+xml;
zstd_static on;
# Responses are cut into 1m segments; up to 4 of them per response are
# compressed at once on the pool's threads.
zstd_thread_pool default;
zstd_parallel_segment_size 1m;
zstd_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;
zstd_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 zstd_types list for web content:
load_module modules/ngx_mzstd.so;
http {
zstd on;
zstd_comp_level 3;
zstd_static on;
zstd_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 tokers/zstd-nginx-module, 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
$zstd_ratio holds the compression ratio of the
response, for example in an access log:
log_format compression '$request "$status" $bytes_sent ratio=$zstd_ratio';
access_log /var/log/nginx/compression.log compression;
Together with ngx_mbrotli
Both libraries can be loaded at once, 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;
}
See the ngx_mbrotli documentation for its directives.
Check that it works
nginx -t && nginx -s reload
curl -s -o /dev/null -D - -H 'Accept-Encoding: zstd' http://localhost/ | grep -i content-encoding
# content-encoding: zstd
A response is not compressed when it is shorter than
zstd_min_length, when its type is not in
zstd_types, or when the client’s
Accept-Encoding does not list zstd (or lists it with q=0).
Compressing on a thread pool
By default the module compresses in the worker’s event loop, as tokers/zstd-nginx-module 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 15–19, or for responses of a megabyte and more, it is not.
One directive moves compression onto an NGINX thread pool:
zstd_thread_pool default;
NGINX must be built with --with-threads (nginx.org’s packages and most
distributions’ are).
How a response is compressed
- The response is cut into segments of
zstd_parallel_segment_size(1mby default). - Up to
zstd_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. Each segment is compressed with only a limited overlap of the input
before it, so a response longer than one segment compresses slightly worse,
and a flush (for example with proxy_buffering off) ends the current segment
early. The response is still one standard zstd frame, which any decoder reads.
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 zstd threads=4;
http {
zstd_thread_pool zstd;
}
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;
zstd_thread_pool off;
}
- Short responses at low levels. In the benchmarks,
small.json(1.9 KB) at level 3 is served faster from the event loop. At level 9 and above even short responses gain from the pool. - Responses compressed with a dictionary. They are always compressed in the event loop: the parallel compressor does not take dictionaries.
Memory
A response being compressed holds up to zstd_parallel_tasks + 1 segments of
input, their compressed output, and an encoder for each segment being
compressed. 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 15 peaks at 523 MiB with 1 task and
1,119 MiB with 4.
To bound it:
- lower
zstd_parallel_tasks, so fewer segments of one response are in flight; - lower
zstd_parallel_segment_size, so each segment and its encoder are smaller (at the cost of ratio); - set
zstd_parallel_compressors, which limits how many compressors each worker keeps segment encoders for; - pick a
zstd_retention_policyother thanaggressive.
See Memory and encoder reuse for how idle compressors are kept.
Tuning summary
| Goal | Change |
|---|---|
| Better ratio on long responses | Raise zstd_parallel_segment_size (up to 16m) |
| More parallelism per response | Raise zstd_parallel_tasks, and give the pool the threads |
| Less memory per response | Lower zstd_parallel_tasks or zstd_parallel_segment_size |
| Less memory kept between responses | Lower zstd_parallel_compressors or use a stricter zstd_retention_policy |
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 (match finder tables, window and buffers) grows with the level and the window. tokers/zstd-nginx-module allocates it for every response and frees it when the response is complete. ngx_mzstd 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 |
|---|---|---|
zstd_reuse_memory | on | Whether encoders are reused at all |
zstd_retention_policy | aggressive | How much memory an idle encoder keeps |
zstd_idle_encoders | 4 | How many idle encoders each worker keeps |
Retention policies
| Policy | Kept between responses | Use when |
|---|---|---|
aggressive | Every buffer | Throughput matters most and workers may keep memory for their largest responses at their highest level |
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 |
With aggressive, a worker allocates only what its largest responses grow
into, and keeps it. That is the fastest option. In the
benchmarks, a worker compressing in the event loop
with it peaks at about the memory of the C module on long responses, and at far
less on short ones at high levels (5 MiB against 85 MiB for small.json at
level 19), because the encoder sizes its tables for the declared response
length.
http {
zstd on;
# Keep at most 8 MB of buffers per idle encoder.
zstd_retention_policy bounded 8m;
location /downloads/ {
zstd_comp_level 19;
# Large, rare responses: do not keep their memory.
zstd_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
zstd_idle_encoders. When a worker’s slots are full, the least recently used
encoder is freed.
Allocating per response
To allocate per response, as tokers/zstd-nginx-module does:
zstd_reuse_memory off;
Every response then gets a new encoder that is freed when the response is
complete. zstd_idle_encoders 0 has the same effect for the whole http
block.
With a thread pool
With zstd_thread_pool, the unit of reuse is a compressor:
an idle compressor keeps the encoders its segments used, up to
zstd_parallel_tasks of them, 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
zstd_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
zstd_idle_encodersof them. - An idle compressor is reused only for the same level, window and segment size.
Dictionaries
A dictionary primes the encoder with content that responses are likely to share, so short responses compress much better. It helps most with short responses that resemble it, such as small JSON documents; on long ones the gain is small.
Only clients that already have the dictionary can decode these responses.
They are sent with Content-Encoding: zstd, as with tokers/zstd-nginx-module,
and nothing in the response tells a client which dictionary it needs; browsers
do not have it and fail to decode them. Use it only for clients you control.
Compression dictionary transport (RFC 9842,
Content-Encoding: dcz) is not supported.
Train and configure
Train a dictionary on samples of your responses with the zstd tool, then
point zstd_dict_file at it:
zstd --train samples/*.json -o /etc/nginx/api.dict
location /internal-api/ {
proxy_pass http://127.0.0.1:8080;
zstd on;
zstd_types application/json;
zstd_dict_file api.dict; # relative to the directory of nginx.conf
}
Clients decode with the same file:
curl -s -H 'Accept-Encoding: zstd' http://localhost/internal-api/items \
| zstd -d -D /etc/nginx/api.dict
Which files are accepted
Both kinds of dictionary that zstd reads are accepted:
- one in the Zstandard format, such as
zstd --trainproduces, whose ID each frame names; - raw content, any file without the dictionary magic number, which the frames do not name.
How it is loaded
- The file is read once, when the configuration is loaded, and a malformed Zstandard dictionary is rejected there.
- It is prepared once for each
zstd_comp_levelandzstd_windowthat locations withzstd onuse it with, and the prepared copies are shared by all such locations. - With
zstd_dict_file off, a location inherits no dictionary. - Responses compressed with a dictionary are compressed in the event loop even
with
zstd_thread_pool: the parallel compressor does not take dictionaries.
In tokers/zstd-nginx-module the directive is only allowed in http and has
no off.
Migrating from tokers/zstd-nginx-module
ngx_mzstd provides the same two modules, directives, defaults and variable as tokers/zstd-nginx-module, so an existing configuration keeps working unchanged.
Steps
-
Remove tokers/zstd-nginx-module’s package (
nginx-module-zstd, or the distribution’s own) or itsload_modulelines. Its two.sofiles become one. -
Install or build ngx_mzstd and load it:
load_module modules/ngx_mzstd.so; -
Run
nginx -tand reload. The rest of the configuration stays as it is.
The libzstd C library is no longer needed by NGINX once nothing else uses it.
What behaves differently
- The window is limited to 8 MB by default
(
zstd_window). The C module never declares a response’s length, so levels 20–22 declare windows of 32, 64 and 128 MB on every response, however short, and browsers reject them. Here they stay within 8 MB, where long responses compress exactly as at level 19, as they do with the C library given the same window. - The length comes from
Content-Length. When it is known, it is declared to the encoder, which writes it into the frame header and chooses its parameters for that size, so short responses use smaller windows and tables. The C module never declares it, so its output differs on such responses. A response whose body does not match its declared length is aborted. - Accept-Encoding is parsed more strictly.
zstd;q=0is a refusal, and only a wholezstdtoken counts: the C module accepts any header that starts withzstd, such aszstdx. zstd_comp_level -1works. The C module takes -1 for “unset” and compresses at level 1 instead.zstd_dict_filecan be set per server and location, and turned off withoff. A Zstandard-format dictionary is checked when the configuration is loaded.- Encoders are reused by default
(
zstd_reuse_memory), each keeping all of its working memory between responses (zstd_retention_policy). Setzstd_reuse_memory offto allocate per response as the C module does. See Memory and encoder reuse. - Responses can be compressed on a thread pool
(
zstd_thread_pool), in segments that are compressed in parallel. See Compressing on a thread pool. - The pure-Rust mzstd encoder replaces the C library. Driven with the same calls and parameters, it writes the same bytes as zstd 1.6.0; output compressed on a thread pool is a valid frame but differs.
New directives
| Directive | Context | Default |
|---|---|---|
zstd_window | http, server, location | 8m |
zstd_reuse_memory | http, server, location | on |
zstd_retention_policy | http, server, location | aggressive |
zstd_idle_encoders | http | 4 |
zstd_thread_pool | http, server, location | off |
zstd_parallel_segment_size | http, server, location | 1m |
zstd_parallel_tasks | http, server, location | 4 |
zstd_parallel_compressors | http | 4 |
Reporting a difference
If the module behaves differently from tokers/zstd-nginx-module with the same configuration, and the difference is not listed above, open an issue with the Differs from tokers/zstd-nginx-module template.
Directives
Every directive of ngx_http_zstd_filter_module and
ngx_http_zstd_static_module. Directives marked Not in
tokers/zstd-nginx-module are extensions; the rest behave as in
tokers/zstd-nginx-module, except
where Migrating says otherwise.
| Directive | Default | Context |
|---|---|---|
zstd_static | off | http, server, location |
zstd | off | http, server, location, if |
zstd_types | text/html | http, server, location |
zstd_buffers | 32 4k|16 8k | http, server, location |
zstd_comp_level | 1 | http, server, location |
zstd_window | 8m | http, server, location |
zstd_min_length | 20 | http, server, location |
zstd_dict_file | off | http, server, location |
zstd_reuse_memory | on | http, server, location |
zstd_retention_policy | aggressive | http, server, location |
zstd_idle_encoders | 4 | http |
zstd_thread_pool | off | http, server, location |
zstd_parallel_segment_size | 1m | http, server, location |
zstd_parallel_tasks | 4 | http, server, location |
zstd_parallel_compressors | 4 | http |
zstd_static
- Syntax
zstd_static on|off|always- Default
off- Context
http,server,location
Enables or disables checking for pre-compressed files with the .zst extension.
With always, the pre-compressed file is served whether or not the client
supports it. With on and
gzip_vary,
Vary: Accept-Encoding is added whenever the .zst file exists, as with
gzip_static.
To serve file.js.zst in place of file.js, create it next to the original with zstd -19 file.js (zstd keeps the input file).
zstd
- Syntax
zstd on|off- Default
off- Context
http,server,location,if
Enables or disables on-the-fly compression of responses.
zstd_types
- Syntax
zstd_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.
zstd_buffers
- Syntax
zstd_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. It works the same way as
gzip_buffers.
zstd_comp_level
- Syntax
zstd_comp_level <level>- Default
1- Context
http,server,location
Sets the compression level, from 1 to 22, or a negative level from -1 to
-131072 for faster compression at a lower ratio. Level 0 is not accepted.
Levels 20–22 would need a window larger than 8 MB, which browsers reject; they
are limited by zstd_window.
zstd_window
- Syntax
zstd_window <size>- Default
8m- Context
http,server,location
Sets the largest window, the distance back that matches may reach. Acceptable
values are 1k, 2k, 4k, 8k, 16k, 32k, 64k, 128k, 256k, 512k,
1m, 2m, 4m and 8m. A level whose own window is no larger is not
affected, and when the response length is known, a smaller window is used if it
is enough.
RFC 9659 limits the window of the
zstd content coding to 8 MB, and Chrome rejects responses that need more. With
the default, levels 1–19 compress exactly as without a limit, and levels 20–22
stay within 8 MB.
Not in tokers/zstd-nginx-module.
zstd_min_length
- Syntax
zstd_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.
zstd_dict_file
- Syntax
zstd_dict_file /path/to/dict | off- Default
off- Context
http,server,location
Compresses responses with the dictionary in the file. A relative path is
taken relative to the directory of nginx.conf. Both kinds of dictionary
that zstd reads are accepted: one in the Zstandard format, such as
zstd --train produces, whose ID each frame names, and raw content, any file
without the dictionary magic number, which the frames do not name.
Only clients that already have the dictionary can decode these responses.
They are sent with Content-Encoding: zstd, as with tokers/zstd-nginx-module,
and nothing in the response tells a client which dictionary it needs; browsers
do not have it and fail to decode them. Use it only for clients you control.
Compression dictionary transport (RFC 9842,
Content-Encoding: dcz) is not supported.
The file is read once, when the configuration is loaded, and a malformed
Zstandard dictionary is rejected there. It is prepared once for each
zstd_comp_level and zstd_window that locations with zstd on use it with,
and the prepared copies are shared by all such locations. With off, a
location inherits no dictionary.
A dictionary helps most with short responses that resemble it, such as small
JSON documents; on long ones the gain is small. Responses compressed with a
dictionary are compressed in the event loop even with zstd_thread_pool:
the parallel compressor does not take dictionaries.
In tokers/zstd-nginx-module it is only allowed in http and has no off.
See Dictionaries.
zstd_reuse_memory
- Syntax
zstd_reuse_memory on|off- Default
on- Context
http,server,location
Enables or disables reusing encoders between responses. When on, each worker
keeps up to zstd_idle_encoders idle encoders, so a response can reuse the
match finder tables, window and buffers a previous one allocated. How much
memory an idle encoder keeps is set by zstd_retention_policy. When off, every
response gets a new encoder that is freed when the response is complete, as
with tokers/zstd-nginx-module.
Not in tokers/zstd-nginx-module.
zstd_retention_policy
- Syntax
zstd_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 zstd_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 at its highestzstd_comp_level.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 zstd_idle_encoders slots are full, the least recently used encoder is
freed.
With zstd_thread_pool, an idle compressor keeps the encoders its segments
used, up to zstd_parallel_tasks of them, 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 zstd_parallel_compressors compressors, idle or in use; other responses
allocate encoders for their segments and free them once they have been sent.
An idle compressor is reused only for the same level, window and segment size.
Not in tokers/zstd-nginx-module.
zstd_idle_encoders
- Syntax
zstd_idle_encoders <number>- Default
4- Context
http
Sets how many idle encoders each worker keeps for reuse with
zstd_reuse_memory on, from 0 to 1024; when they are all taken, the least
recently used one is freed. Compressors for zstd_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
zstd_retention_policy, so a worker that serves locations with several
policies may need more of them.
Not in tokers/zstd-nginx-module.
zstd_thread_pool
- Syntax
zstd_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 zstd_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 zstd_parallel_segment_size, and up to
zstd_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. Each segment
is compressed with only a limited overlap of the input before it, so a response
longer than one segment compresses slightly worse, and a flush (for example with
proxy_buffering off) ends the current segment early. The response is still
one standard zstd frame.
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
zstd_parallel_tasks + 1 segments of input, their compressed output, and an
encoder for each segment being compressed.
Not in tokers/zstd-nginx-module.
See Compressing on a thread pool.
zstd_parallel_segment_size
- Syntax
zstd_parallel_segment_size <size>- Default
1m- Context
http,server,location
Sets the size of the segments a response is cut into for zstd_thread_pool,
from 64k to 16m. The larger the segments, the better a response compresses,
but the fewer of its segments are compressed at once. Smaller segments spread
responses over more threads and compress worse.
Not in tokers/zstd-nginx-module.
zstd_parallel_tasks
- Syntax
zstd_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 zstd_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 tokers/zstd-nginx-module.
zstd_parallel_compressors
- Syntax
zstd_parallel_compressors <number>- Default
4- Context
http
Sets for how many compressors used with zstd_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 zstd_idle_encoders. Requires NGINX
built with --with-threads.
Not in tokers/zstd-nginx-module.
Variables
$zstd_ratio
The achieved compression ratio: the original size divided by the compressed
size, truncated to three decimal places (for example 3.254), as in
tokers/zstd-nginx-module.
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:
- tokers/zstd-nginx-module: as shipped, commit
057a7d3with libzstd 1.5.7. - ngx_mzstd, event loop: ngx_mzstd with its default settings, compressing in the worker’s event loop like tokers/zstd-nginx-module.
- ngx_mzstd, pool, 1 task and 4 tasks: ngx_mzstd with
zstd_thread_poolon a pool of 4 threads andzstd_parallel_tasks 1or4(the default), so that one response is compressed by up to 1 or 4 threads at once, in 1m segments.
Only zstd_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 tokers/zstd-nginx-module.
| Response | Level | tokers/zstd-nginx-module | ngx_mzstd, event loop | ngx_mzstd, pool, 1 task | ngx_mzstd, pool, 4 tasks |
|---|---|---|---|---|---|
| small.json, 1.9 KB | 3 | 33,243 | 47,777 (1.44×) | 36,934 (1.11×) | 42,453 (1.28×) |
| small.json, 1.9 KB | 9 | 3,968 | 36,116 (9.10×) | 43,643 (11.0×) | 38,706 (9.76×) |
| small.json, 1.9 KB | 15 | 58.4 | 9,130 (156×) | 28,397 (487×) | 28,112 (482×) |
| small.json, 1.9 KB | 19 | 46.0 | 5,070 (110×) | 16,393 (357×) | 16,152 (351×) |
| jquery.min.js, 85 KB | 3 | 3,466 | 4,138 (1.19×) | 12,967 (3.74×) | 12,762 (3.68×) |
| jquery.min.js, 85 KB | 9 | 841 | 855 (1.02×) | 3,312 (3.94×) | 3,298 (3.92×) |
| jquery.min.js, 85 KB | 15 | 50.2 | 200 (3.99×) | 763 (15.2×) | 762 (15.2×) |
| jquery.min.js, 85 KB | 19 | 31.2 | 83.8 (2.68×) | 321 (10.3×) | 323 (10.3×) |
| react-dom.development.js, 1.03 MB | 3 | 359 | 423 (1.18×) | 1,325 (3.69×) | 1,325 (3.69×) |
| react-dom.development.js, 1.03 MB | 9 | 91.3 | 92.9 (1.02×) | 257 (2.81×) | 244 (2.67×) |
| react-dom.development.js, 1.03 MB | 15 | 12.1 | 15.6 (1.29×) | 46.2 (3.82×) | 45.7 (3.77×) |
| react-dom.development.js, 1.03 MB | 19 | 4.99 | 6.34 (1.27×) | 16.3 (3.27×) | 16.0 (3.21×) |
| libraries.json, 2.2 MB | 3 | 175 | 209 (1.19×) | 647 (3.69×) | 665 (3.79×) |
| libraries.json, 2.2 MB | 9 | 48.4 | 49.7 (1.03×) | 118 (2.43×) | 114 (2.35×) |
| libraries.json, 2.2 MB | 15 | 6.99 | 7.74 (1.11×) | 18.5 (2.64×) | 15.5 (2.21×) |
| libraries.json, 2.2 MB | 19 | 2.37 | 2.95 (1.24×) | 6.29 (2.65×) | 6.39 (2.70×) |
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, because every segment but the first also runs through part of the input before it.
| Response | Level | tokers/zstd-nginx-module | ngx_mzstd, event loop | ngx_mzstd, pool, 1 task | ngx_mzstd, pool, 4 tasks |
|---|---|---|---|---|---|
| small.json, 1.9 KB | 3 | 0.030 | 0.021 | 0.039 | 0.035 |
| small.json, 1.9 KB | 9 | 0.251 | 0.028 | 0.041 | 0.045 |
| small.json, 1.9 KB | 15 | 17.7 | 0.110 | 0.121 | 0.120 |
| small.json, 1.9 KB | 19 | 21.9 | 0.197 | 0.210 | 0.210 |
| jquery.min.js, 85 KB | 3 | 0.289 | 0.242 | 0.267 | 0.268 |
| jquery.min.js, 85 KB | 9 | 1.19 | 1.17 | 1.18 | 1.18 |
| jquery.min.js, 85 KB | 15 | 20.5 | 5.04 | 5.25 | 5.23 |
| jquery.min.js, 85 KB | 19 | 32.4 | 12.0 | 12.5 | 12.4 |
| react-dom.development.js, 1.03 MB | 3 | 2.78 | 2.37 | 2.90 | 2.93 |
| react-dom.development.js, 1.03 MB | 9 | 10.9 | 10.8 | 15.6 | 16.4 |
| react-dom.development.js, 1.03 MB | 15 | 83.9 | 64.7 | 86.8 | 88.6 |
| react-dom.development.js, 1.03 MB | 19 | 201 | 159 | 246 | 252 |
| libraries.json, 2.2 MB | 3 | 5.71 | 4.78 | 5.79 | 6.01 |
| libraries.json, 2.2 MB | 9 | 20.7 | 20.1 | 34.0 | 35.3 |
| libraries.json, 2.2 MB | 15 | 146 | 130 | 221 | 269 |
| libraries.json, 2.2 MB | 19 | 424 | 342 | 641 | 640 |
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 | tokers/zstd-nginx-module | ngx_mzstd, event loop | ngx_mzstd, pool, 1 task | ngx_mzstd, pool, 4 tasks |
|---|---|---|---|---|---|
| small.json, 1.9 KB | 3 | 5 | 5 | 6 | 6 |
| small.json, 1.9 KB | 9 | 14 | 5 | 6 | 6 |
| small.json, 1.9 KB | 15 | 68 | 5 | 7 | 7 |
| small.json, 1.9 KB | 19 | 85 | 5 | 7 | 7 |
| jquery.min.js, 85 KB | 3 | 5 | 6 | 17 | 17 |
| jquery.min.js, 85 KB | 9 | 15 | 6 | 18 | 18 |
| jquery.min.js, 85 KB | 15 | 68 | 8 | 31 | 31 |
| jquery.min.js, 85 KB | 19 | 85 | 8 | 28 | 30 |
| react-dom.development.js, 1.03 MB | 3 | 6 | 7 | 51 | 66 |
| react-dom.development.js, 1.03 MB | 9 | 16 | 16 | 159 | 209 |
| react-dom.development.js, 1.03 MB | 15 | 69 | 38 | 413 | 549 |
| react-dom.development.js, 1.03 MB | 19 | 86 | 39 | 443 | 562 |
| libraries.json, 2.2 MB | 3 | 7 | 8 | 59 | 86 |
| libraries.json, 2.2 MB | 9 | 17 | 17 | 173 | 291 |
| libraries.json, 2.2 MB | 15 | 70 | 71 | 523 | 1,119 |
| libraries.json, 2.2 MB | 19 | 87 | 56 | 505 | 937 |
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. - tokers/zstd-nginx-module at commit
057a7d3with libzstd 1.5.7. - ngx_mzstd 0.2.3 with mzstd 0.2.9,
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, zstd_thread_pool cp with
zstd_parallel_tasks 1 or 4. Everything else is left at its default, including
zstd_parallel_segment_size 1m, zstd_parallel_compressors 4 and
zstd_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 19), 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 zstd 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 mzstd encoder from NGINX’s filter chain; the bytes the encoder writes are mzstd’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
tokers/zstd-nginx-module’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, location 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 zstd_thread_pool, each segment becomes
an ngx_thread_task_t. Its completion handler runs in the event loop and calls
the zstd 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
zstd 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_mzstd.so;" \
prove -I nginx-tests/lib t/
They decode large responses with zstd -M8MB, which rejects a window over 8 MB
as browsers do. 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 memory 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 tokers/zstd-nginx-module with the same configuration, open an issue with the Differs from tokers/zstd-nginx-module template.
- Problems with the Zstandard encoder itself belong in mzstd.
- 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 zstd-compressed response with and without a thread pool,
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 tokers/zstd-nginx-module (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_mzstd/ 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#zstd_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.