<!-- review: finished -->

<a id="external-zstd"></a>

# Zstandard (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.

<a id="installation-33"></a>

## Installation

To [install](https://en.angie.software//angie/docs/installation/index.md#install-packages) the module, use one of the following packages:

- Angie: `angie-module-zstd`
- Angie PRO: `angie-pro-module-zstd`

<a id="module-loading"></a>

## Module Loading

Enable the modules in the `main{}` context:

```nginx
load_module modules/ngx_http_zstd_filter_module.so;
load_module modules/ngx_http_zstd_static_module.so;
```

<a id="example-configuration-2"></a>

## Example Configuration

```nginx
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;
    }
}
```

<a id="how-it-works"></a>

## 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 it doesn't exist, it will compress the response dynamically.

Compression is only performed when the `Accept-Encoding` header includes `zstd`, for example:

```none
Accept-Encoding: gzip, zstd
```

If compression is applied or a precompressed file is found, the following response header is added:

```none
Content-Encoding: zstd
```

<a id="more-information"></a>

## More Information

Full documentation and source code are available at:
[https://github.com/tokers/zstd-nginx-module](https://github.com/tokers/zstd-nginx-module)
