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

Directives

Every directive of ngx_http_zstd_filter_module and ngx_http_zstd_static_module. Directives marked Not in tokers/zstd-nginx-module are extensions; the rest behave as in tokers/zstd-nginx-module, except where Migrating says otherwise.

DirectiveDefaultContext
zstd_staticoffhttp, server, location
zstdoffhttp, server, location, if
zstd_typestext/htmlhttp, server, location
zstd_buffers32 4k|16 8khttp, server, location
zstd_comp_level1http, server, location
zstd_window8mhttp, server, location
zstd_min_length20http, server, location
zstd_dict_fileoffhttp, server, location
zstd_reuse_memoryonhttp, server, location
zstd_retention_policyaggressivehttp, server, location
zstd_idle_encoders4http
zstd_thread_pooloffhttp, server, location
zstd_parallel_segment_size1mhttp, server, location
zstd_parallel_tasks4http, server, location
zstd_parallel_compressors4http

zstd_static

Syntax
zstd_static on|off|always
Default
off
Context
http, server, location

Enables or disables checking for pre-compressed files with the .zst extension. With always, the pre-compressed file is served whether or not the client supports it. With on and gzip_vary, Vary: Accept-Encoding is added whenever the .zst file exists, as with gzip_static.

To serve file.js.zst in place of file.js, create it next to the original with zstd -19 file.js (zstd keeps the input file).

zstd

Syntax
zstd on|off
Default
off
Context
http, server, location, if

Enables or disables on-the-fly compression of responses.

zstd_types

Syntax
zstd_types <mime_type> [..]
Default
text/html
Context
http, server, location

Compresses responses with the listed MIME types as well as text/html, which is always compressed. The special value * matches any MIME type.

zstd_buffers

Syntax
zstd_buffers <number> <size>
Default
32 4k|16 8k
Context
http, server, location

Sets the number and size of the buffers that hold compressed output. The default is one memory page per buffer, up to 128k in total. It works the same way as gzip_buffers.

zstd_comp_level

Syntax
zstd_comp_level <level>
Default
1
Context
http, server, location

Sets the compression level, from 1 to 22, or a negative level from -1 to -131072 for faster compression at a lower ratio. Level 0 is not accepted.

Levels 20–22 would need a window larger than 8 MB, which browsers reject; they are limited by zstd_window.

zstd_window

Syntax
zstd_window <size>
Default
8m
Context
http, server, location

Sets the largest window, the distance back that matches may reach. Acceptable values are 1k, 2k, 4k, 8k, 16k, 32k, 64k, 128k, 256k, 512k, 1m, 2m, 4m and 8m. A level whose own window is no larger is not affected, and when the response length is known, a smaller window is used if it is enough.

RFC 9659 limits the window of the zstd content coding to 8 MB, and Chrome rejects responses that need more. With the default, levels 1–19 compress exactly as without a limit, and levels 20–22 stay within 8 MB.

Not in tokers/zstd-nginx-module.

zstd_min_length

Syntax
zstd_min_length <length>
Default
20
Context
http, server, location

Sets the minimum length of a response that will be compressed. The length comes only from the Content-Length response header.

zstd_dict_file

Syntax
zstd_dict_file /path/to/dict | off
Default
off
Context
http, server, location

Compresses responses with the dictionary in the file. A relative path is taken relative to the directory of nginx.conf. Both kinds of dictionary that zstd reads are accepted: one in the Zstandard format, such as zstd --train produces, whose ID each frame names, and raw content, any file without the dictionary magic number, which the frames do not name.

Only clients that already have the dictionary can decode these responses. They are sent with Content-Encoding: zstd, as with tokers/zstd-nginx-module, and nothing in the response tells a client which dictionary it needs; browsers do not have it and fail to decode them. Use it only for clients you control. Compression dictionary transport (RFC 9842, Content-Encoding: dcz) is not supported.

The file is read once, when the configuration is loaded, and a malformed Zstandard dictionary is rejected there. It is prepared once for each zstd_comp_level and zstd_window that locations with zstd on use it with, and the prepared copies are shared by all such locations. With off, a location inherits no dictionary.

A dictionary helps most with short responses that resemble it, such as small JSON documents; on long ones the gain is small. Responses compressed with a dictionary are compressed in the event loop even with zstd_thread_pool: the parallel compressor does not take dictionaries.

In tokers/zstd-nginx-module it is only allowed in http and has no off.

See Dictionaries.

zstd_reuse_memory

Syntax
zstd_reuse_memory on|off
Default
on
Context
http, server, location

Enables or disables reusing encoders between responses. When on, each worker keeps up to zstd_idle_encoders idle encoders, so a response can reuse the match finder tables, window and buffers a previous one allocated. How much memory an idle encoder keeps is set by zstd_retention_policy. When off, every response gets a new encoder that is freed when the response is complete, as with tokers/zstd-nginx-module.

Not in tokers/zstd-nginx-module.

See Memory and encoder reuse.

zstd_retention_policy

Syntax
zstd_retention_policy aggressive | current_config | release_all | bounded <size>
Default
aggressive
Context
http, server, location

Sets how much working memory an encoder keeps between responses. It matters only when zstd_reuse_memory is on.

  • aggressive keeps every buffer, so a worker allocates only what its largest responses grow into. This is the fastest option, but each worker keeps memory for its largest responses at its highest zstd_comp_level.
  • current_config keeps buffers only while they suit the current level and window. They are released when the encoder is reused for a location with different settings.
  • bounded <size> keeps buffers while they fit within size, and releases them after a response that grew past it.
  • release_all keeps nothing between responses; only the encoder itself is reused.

Idle encoders are reused only by locations with the same retention policy; when a worker’s zstd_idle_encoders slots are full, the least recently used encoder is freed.

With zstd_thread_pool, an idle compressor keeps the encoders its segments used, up to zstd_parallel_tasks of them, and the policy applies to all of them together. A response holds its compressor until it has been sent, including while its segments wait for a free thread, so a worker keeps encoders for at most zstd_parallel_compressors compressors, idle or in use; other responses allocate encoders for their segments and free them once they have been sent. An idle compressor is reused only for the same level, window and segment size.

Not in tokers/zstd-nginx-module.

See Memory and encoder reuse.

zstd_idle_encoders

Syntax
zstd_idle_encoders <number>
Default
4
Context
http

Sets how many idle encoders each worker keeps for reuse with zstd_reuse_memory on, from 0 to 1024; when they are all taken, the least recently used one is freed. Compressors for zstd_thread_pool are kept separately, up to the same number. With 0 nothing is kept, so every response gets a new encoder.

Idle encoders are reused only by locations with the same zstd_retention_policy, so a worker that serves locations with several policies may need more of them.

Not in tokers/zstd-nginx-module.

zstd_thread_pool

Syntax
zstd_thread_pool <name> | off
Default
off
Context
http, server, location

Compresses responses on the named thread pool instead of in the worker’s event loop, so that a long response or a high zstd_comp_level does not hold up the worker’s other connections. The pool is declared with thread_pool in the main context; a pool named default with 32 threads exists without being declared. Requires NGINX built with --with-threads.

A response is cut into segments of zstd_parallel_segment_size, and up to zstd_parallel_tasks of them are compressed on the pool’s threads at once. They are sent in order as they complete, while the next segments are read. A response that fits in one segment is compressed in one piece by one thread. Each segment is compressed with only a limited overlap of the input before it, so a response longer than one segment compresses slightly worse, and a flush (for example with proxy_buffering off) ends the current segment early. The response is still one standard zstd frame.

Every worker process has its own threads, so worker_processes times the pool’s threads run at once. A response being compressed holds up to zstd_parallel_tasks + 1 segments of input, their compressed output, and an encoder for each segment being compressed.

Not in tokers/zstd-nginx-module.

See Compressing on a thread pool.

zstd_parallel_segment_size

Syntax
zstd_parallel_segment_size <size>
Default
1m
Context
http, server, location

Sets the size of the segments a response is cut into for zstd_thread_pool, from 64k to 16m. The larger the segments, the better a response compresses, but the fewer of its segments are compressed at once. Smaller segments spread responses over more threads and compress worse.

Not in tokers/zstd-nginx-module.

zstd_parallel_tasks

Syntax
zstd_parallel_tasks <number>
Default
4
Context
http, server, location

Sets how many segments of one response may be compressed, or wait to be sent, at once with zstd_thread_pool, from 1 to 1024. When they all are, NGINX stops reading the response until the first of them has been sent.

Not in tokers/zstd-nginx-module.

zstd_parallel_compressors

Syntax
zstd_parallel_compressors <number>
Default
4
Context
http

Sets for how many compressors used with zstd_thread_pool each worker keeps segment encoders, from 0 to 1024, counting both idle ones and ones held by responses. A response holds its compressor until it has been sent, including while its segments wait for a free thread, so without a limit every response in flight could keep the encoders a previous response grew. Responses beyond the limit allocate encoders for their segments and free them once they have been sent. Idle compressors are also limited by zstd_idle_encoders. Requires NGINX built with --with-threads.

Not in tokers/zstd-nginx-module.

See Memory and encoder reuse.