Brotli is a generic-purpose lossless compression algorithm that compresses data using a combination of a modern variant of the LZ77 algorithm, Huffman coding and 2nd order context modeling, with a compression ratio comparable to the best currently available general-purpose compression methods. It is similar in speed with deflate but offers more dense compression.
ngx_brotli is a set of two nginx modules:
- ngx_brotli filter module - used to compress responses on-the-fly,
- ngx_brotli static module - used to serve pre-compressed files.
Both Brotli library and nginx module are under active development.
Checkout the latest ngx_brotli
and build the dependencies:
git clone --recurse-submodules -j8 https://github.com/google/ngx_brotli
cd ngx_brotli/deps/brotli
mkdir out && cd out
cmake -DCMAKE_BUILD_TYPE=Release -DBUILD_SHARED_LIBS=OFF -DCMAKE_C_FLAGS="-Ofast -m64 -march=native -mtune=native -flto -funroll-loops -ffunction-sections -fdata-sections -Wl,--gc-sections" -DCMAKE_CXX_FLAGS="-Ofast -m64 -march=native -mtune=native -flto -funroll-loops -ffunction-sections -fdata-sections -Wl,--gc-sections" -DCMAKE_INSTALL_PREFIX=./installed ..
cmake --build . --config Release --target brotlienc
cd ../../../..
$ cd nginx-1.x.x
$ export CFLAGS="-m64 -march=native -mtune=native -Ofast -flto -funroll-loops -ffunction-sections -fdata-sections -Wl,--gc-sections"
$ export LDFLAGS="-m64 -Wl,-s -Wl,-Bsymbolic -Wl,--gc-sections"
$ ./configure --add-module=/path/to/ngx_brotli
$ make && make install
This will compile the module directly into Nginx.
$ cd nginx-1.x.x
$ ./configure --with-compat --add-dynamic-module=/path/to/ngx_brotli
$ make modules
You will need to use exactly the same ./configure
arguments as your Nginx configuration and append --with-compat --add-dynamic-module=/path/to/ngx_brotli
to the end, otherwise you will get a "module is not binary compatible" error on startup. You can run nginx -V
to get the configuration arguments for your Nginx installation.
make modules
will result in ngx_http_brotli_filter_module.so
and ngx_http_brotli_static_module.so
in the objs
directory. Copy these to /usr/lib/nginx/modules/
then add the load_module
directives to nginx.conf
(above the http block):
load_module modules/ngx_http_brotli_filter_module.so;
load_module modules/ngx_http_brotli_static_module.so;
- syntax:
brotli_static on|off|always
- default:
off
- context:
http
,server
,location
Enables or disables checking of the existence of pre-compressed files with.br
extension. With the always
value, pre-compressed file is used in all cases,
without checking if the client supports it.