Zstd#
The module adds support for Zstandard compression of responses, both dynamic (on-the-fly) and static (precompressed files). Compared to gzip, zstd offers higher compression and decompression speeds at comparable or better compression ratios.
The module consists of two components:
http_zstd_filter — for compressing responses dynamically;
http_zstd_static — for serving precompressed files.
Installation#
To install the module, use one of the following packages:
Angie:
angie-module-zstdAngie PRO:
angie-pro-module-zstd
Module Loading#
Enable the modules in the main{} context:
load_module modules/ngx_http_zstd_filter_module.so;
load_module modules/ngx_http_zstd_static_module.so;
Example Configuration#
server {
listen 80 default_server;
zstd_types text/plain text/css;
zstd_min_length 256; # Minimum response length for compression
zstd_comp_level 3; # Compression level (1 to 22)
# Dynamic file compression
location / {
zstd on;
root /usr/share/angie/html;
}
# Dynamic compression of backend responses
location /bk/ {
zstd on;
proxy_pass http://127.0.0.1:8081/;
}
# Using precompressed .zst files
location /static/ {
zstd_static on;
root /usr/share/angie;
}
}
server {
listen 8081;
location / {
root /usr/share/angie/html;
index index.html;
}
}
How It Works#
You can combine dynamic (zstd on) and static (zstd_static on) compression. In this case, the server will first look for a precompressed .zst file. If there is no such file or the file fails the check described below, the server compresses the response dynamically.
Compression is only performed when the Accept-Encoding header includes zstd, for example:
Accept-Encoding: gzip, zstd
If compression is applied or a precompressed file is served, the following response header is added:
Content-Encoding: zstd
The same request can thus get a compressed or an uncompressed response, depending on the client. So that caches keep these variants apart, the module adds the following header itself, even when the client does not accept zstd:
Vary: Accept-Encoding
With dynamic compression, every response that the module could compress gets this header; with static compression, every response for which a valid .zst file exists. The gzip_vary directive is not required for this; if it is enabled, the response still carries a single such header.
Dynamic compression is not applied to a response whose Cache-Control header contains no-transform, for example a response from a backend:
Cache-Control: no-transform
Static compression does not check this header.
Before serving a precompressed file, the module checks that the file is a zstd file whose declared decompression window is no more than 8 MB, the limit that browsers enforce for Content-Encoding: zstd. The module does not serve a file that fails the check, such as a file renamed to .zst without compression or one compressed with a larger window. It writes an error level message about the file to the error log and handles the request as if the file did not exist. To check the files in advance, use the zstd utility:
$ zstd -t --memory=8MB *.zst
More Information#
Version: 0.90.9
Full documentation and source code are available at: myguard-labs/nginx-zstd-module