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_brotli_filter_module and ngx_http_brotli_static_module. Directives marked Not in google/ngx_brotli are extensions; the rest behave as in google/ngx_brotli, except where Migrating says otherwise.

DirectiveDefaultContext
brotli_staticoffhttp, server, location
brotlioffhttp, server, location, if
brotli_typestext/htmlhttp, server, location
brotli_buffers32 4k|16 8khttp, server, location
brotli_comp_level6http, server, location
brotli_window512khttp, server, location
brotli_min_length20http, server, location
brotli_reuse_memoryonhttp, server, location
brotli_retention_policyaggressivehttp, server, location
brotli_idle_encoders4http
brotli_thread_pooloffhttp, server, location
brotli_parallel_segment_size1mhttp, server, location
brotli_parallel_tasks4http, server, location
brotli_parallel_compressors4http

brotli_static

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

Enables or disables checking for pre-compressed files with the .br extension. With always, the pre-compressed file is served whether or not the client supports it.

To serve file.js.br in place of file.js, create it next to the original with brotli -q 11 file.js (brotli keeps the input file).

brotli

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

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

brotli_types

Syntax
brotli_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.

brotli_buffers

Syntax
brotli_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. google/ngx_brotli accepts this directive but ignores it; here it works the same way as gzip_buffers.

brotli_comp_level

Syntax
brotli_comp_level <level>
Default
6
Context
http, server, location

Sets the Brotli quality (compression level), from 0 to 11.

brotli_window

Syntax
brotli_window <size>
Default
512k
Context
http, server, location

Sets the Brotli window size. Acceptable values are 1k, 2k, 4k, 8k, 16k, 32k, 64k, 128k, 256k, 512k, 1m, 2m, 4m, 8m and 16m. When the response length is known, a smaller window is used if it is enough.

brotli_min_length

Syntax
brotli_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.

brotli_reuse_memory

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

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

Not in google/ngx_brotli.

See Memory and encoder reuse.

brotli_retention_policy

Syntax
brotli_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 brotli_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 brotli_comp_level 11 with a 16m brotli_window.
  • 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 brotli_idle_encoders slots are full, the least recently used encoder is freed.

With brotli_thread_pool, an idle compressor keeps the encoders of up to brotli_parallel_tasks segments, 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 brotli_parallel_compressors compressors, idle or in use; other responses allocate encoders for their segments and free them once they have been sent.

Not in google/ngx_brotli.

See Memory and encoder reuse.

brotli_idle_encoders

Syntax
brotli_idle_encoders <number>
Default
4
Context
http

Sets how many idle encoders each worker keeps for reuse with brotli_reuse_memory on, from 0 to 1024; when they are all taken, the least recently used one is freed. Compressors for brotli_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 brotli_retention_policy, so a worker that serves locations with several policies may need more of them.

Not in google/ngx_brotli.

brotli_thread_pool

Syntax
brotli_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 brotli_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 brotli_parallel_segment_size, and up to brotli_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. At brotli_comp_level 2–9, every segment but the first also runs through the input before it, a quarter of brotli_parallel_segment_size at most and never more than the window, so that its back-references can reach into it: 256 KiB for 1m segments with the default 512k window. At other levels a segment starts from nothing, and back-references do not cross segment boundaries. Either way, a response longer than one segment compresses worse than in one piece: on the 1 MB and 2.2 MB files of the benchmarks, by 0.1–0.6% at levels 4–9 and by 3.8–5.2% at level 11. A flush (for example with proxy_buffering off) ends the current segment early.

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 brotli_parallel_tasks + 1 segments of input, their compressed output, and an encoder for each segment.

Not in google/ngx_brotli.

See Compressing on a thread pool.

brotli_parallel_segment_size

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

Sets the size of the segments a response is cut into for brotli_thread_pool, from 64k to 16m. At levels 2–9 a segment can refer back to at most a quarter of its size of the input before it, and at other levels to none of it (see brotli_thread_pool), so the larger the segments, the better a response compresses and, at levels 2–9, the less input is run through twice, but the fewer of its segments are compressed at once, and the more memory each of their encoders needs. Smaller segments spread responses over more threads and compress worse.

Not in google/ngx_brotli.

brotli_parallel_tasks

Syntax
brotli_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 brotli_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 google/ngx_brotli.

brotli_parallel_compressors

Syntax
brotli_parallel_compressors <number>
Default
4
Context
http

Sets for how many compressors used with brotli_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 brotli_idle_encoders. Requires NGINX built with --with-threads.

A higher number lets more responses reuse encoders, and costs the memory of their encoders: in single runs with a 4-thread pool and 32 connections on jquery.min.js at level 9, 32 served 8% more requests per second than the default, and the worker peaked at 168 MiB instead of 91 MiB. A pool whose threads often all compress at once, such as default with 32 threads, may benefit from a number closer to its threads, together with a higher brotli_idle_encoders.

Not in google/ngx_brotli.

See Memory and encoder reuse.