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).