UnZstd#

The unzstd module decompresses backend responses compressed with Zstandard (Content-Encoding: zstd) for clients that do not accept this encoding. The backend can then store its data compressed, saving disk space and I/O, while every client still receives a response it can read.

Installation#

To install the module, use one of the following packages:

  • Angie: angie-module-unzstd

  • Angie PRO: angie-pro-module-unzstd

Directives#

The module provides the following directives:

  • unzstd — similar to gunzip

  • unzstd_buffers — similar to gunzip_buffers

  • unzstd_force — decompresses responses even for clients that accept Zstandard

  • unzstd_dict_file — sets the dictionary file for responses compressed with a Zstandard dictionary; allowed only in the http context

Loading the Module#

To use the module, it must be loaded in the main{} context:

load_module modules/ngx_http_unzstd_filter_module.so;

Configuration Example#

server {
    listen 80 default_server;
    location / {
        root  /usr/share/angie/html;
        index index.html;
    }

    location /storage {
        unzstd on;
        proxy_pass http://127.0.0.1:8080;
    }
}

# Backend
server {
    listen 8080;
    location /storage {
        root   /usr/share/angie;
        rewrite ^(.*)$ $1.zst break;  # Return the compressed file with .zst suffix
        add_header Content-Encoding zstd; # Indicate Zstandard compression in the response header
    }
}

Demonstration#

Let's compress the test file sample.txt with the zstd utility and place the result where the backend serves it from:

$ zstd -19 sample.txt -o sample.txt.zst
sample.txt           : 24.11%   (  1.43 MiB =>    353 KiB, sample.txt.zst)

$ sudo mkdir -p /usr/share/angie/storage
$ sudo cp sample.txt.zst /usr/share/angie/storage/
$ wc -c /usr/share/angie/storage/sample.txt.zst
361662 /usr/share/angie/storage/sample.txt.zst

If the client supports Zstandard, it receives the compressed file unchanged:

$ curl -s -H 'Accept-Encoding: zstd' -o received.zst localhost/storage/sample.txt

$ wc -c received.zst
361662 received.zst

If the client does not support Zstandard, the unzstd module decompresses the file before sending it:

$ curl -s -o received.txt localhost/storage/sample.txt

$ wc -c received.txt
1500092 received.txt

The file received.txt holds the original 1500092-byte text, identical to sample.txt, rather than the 361662-byte compressed data.

Additional Information#

Version: 658990e

Detailed documentation and source code are available at: HanadaLee/ngx_http_unzstd_filter_module