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.
Installation
Statically compiled
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.
Dynamically loaded
$ 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):
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.
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
Enables on-the-fly compression of responses for the specified MIME types
in addition to text/html. The special value * matches any MIME type.
Responses with the text/html MIME type are always compressed.
brotli_buffers
syntax: brotli_buffers <number> <size>
default: 32 4k|16 8k
context: http, server, location
Deprecated, ignored.
brotli_comp_level
syntax: brotli_comp_level <level>
default: 6
context: http, server, location
Sets on-the-fly compression Brotli quality (compression) level.
Acceptable values are in the range from 0 to 11.
Copyright (C) 2002-2015 Igor Sysoev
Copyright (C) 2011-2015 Nginx, Inc.
Copyright (C) 2015 Google Inc.
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
SUCH DAMAGE.
ngx_brotli
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:
Table of Contents
brotli_staticbrotlibrotli_typesbrotli_buffersbrotli_comp_levelbrotli_windowbrotli_min_length$brotli_ratioStatus
Both Brotli library and nginx module are under active development.
Installation
Statically compiled
Checkout the latest
ngx_brotliand build the dependencies:This will compile the module directly into Nginx.
Dynamically loaded
You will need to use exactly the same
./configurearguments as your Nginx configuration and append--with-compat --add-dynamic-module=/path/to/ngx_brotlito the end, otherwise you will get a “module is not binary compatible” error on startup. You can runnginx -Vto get the configuration arguments for your Nginx installation.make moduleswill result inngx_http_brotli_filter_module.soandngx_http_brotli_static_module.soin theobjsdirectory. Copy these to/usr/lib/nginx/modules/then add theload_moduledirectives tonginx.conf(above the http block):Configuration directives
brotli_staticbrotli_static on|off|alwaysoffhttp,server,locationEnables or disables checking of the existence of pre-compressed files with
.brextension. With thealwaysvalue, pre-compressed file is used in all cases, without checking if the client supports it.brotlibrotli on|offoffhttp,server,location,ifEnables or disables on-the-fly compression of responses.
brotli_typesbrotli_types <mime_type> [..]text/htmlhttp,server,locationEnables on-the-fly compression of responses for the specified MIME types in addition to
text/html. The special value*matches any MIME type. Responses with thetext/htmlMIME type are always compressed.brotli_buffersbrotli_buffers <number> <size>32 4k|16 8khttp,server,locationDeprecated, ignored.
brotli_comp_levelbrotli_comp_level <level>6http,server,locationSets on-the-fly compression Brotli quality (compression)
level. Acceptable values are in the range from0to11.brotli_windowbrotli_window <size>512khttp,server,locationSets Brotli window
size. Acceptable values are1k,2k,4k,8k,16k,32k,64k,128k,256k,512k,1m,2m,4m,8mand16m.brotli_min_lengthbrotli_min_length <length>20http,server,locationSets the minimum
lengthof a response that will be compressed. The length is determined only from theContent-Lengthresponse header field.Variables
$brotli_ratioAchieved compression ratio, computed as the ratio between the original and compressed response sizes.
Sample configuration
Contributing
See Contributing.
License