Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

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_module line 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:

ServerSupportedTested in CIPackages
NGINX1.25.4 and newer1.28, 1.30, 1.31nginx.org stable and mainline, Debian, Ubuntu, RHEL, Fedora
freenginxyes1.31build from source
Angieyes1.12build 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

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:

LevelEvent loopThread pool, 4 tasks
31.19×3.68×
91.02×3.92×
153.99×15.2×
192.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_ratio variable 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.