Project address: Github repository address

ngx_brotli

Brotli is a general-purpose lossless compression algorithm that combines a modern variant of the LZ77 algorithm, Huffman coding, and second-order context modeling to compress data with a compression ratio comparable to the best general-purpose compression methods currently available. It is similar to Deflate in speed, but offers a higher compression rate.

Note: This compression encoding is only available for HTTPS communication.

Ngx_brotli contains two modules:

  • ngx_brotli filter module– Used to dynamically compress the response body.
  • ngx_brotli static module– Used to serve with pre-compressed.br files (static site).

Installation method

Get ngx_brotli

  1. Make sure you have Git installed, if not, use the following command to install it:
$ yum -y install git
Copy the code
  1. Obtain from warehousengx_brotli
$ git clone https://github.com/google/ngx_brotli.git
Copy the code

If the server is located in the country, it is recommended to use the image:

$ git clone https://gitee.com/xlsw/ngx_brotli.git
Copy the code
  1. Initialize the submodule
$ cd ngx_brotli
$ git submodule update --init 
$ cd ~
Copy the code

Get Nginx source code

  1. If you already have Nginx installed, check out the version:
$ nginx -v
Copy the code

The version of Nginx installed in this example is:

Nginx version: nginx / 1.20.1

  1. Get and unzip the Nginx source codexReplace with your Nginx version number, customizable if not installed)
$ wget https://nginx.org/download/nginx-1.x.x.tar.gz && tar zxvf nginx-1.x.x.tar.gz
Copy the code

If the example version is 1.20.1, the command is:

$wget https://nginx.org/download/nginx-1.20.1.tar.gz && tar ZXVF nginx - 1.20.1. Tar. GzCopy the code
  1. Clean up junk files
$rm nginx - 1.20.1. Tar. GzCopy the code

The installation

You can use either of the following methods. If you have installed Nginx through the YUM source, you are advised to use the first dynamic module loading method

Dynamic module loading

  1. Install the environment required for compilation
$ yum -y install pcre pcre-devel zlib zlib-devel openssl openssl-devel
Copy the code
  1. Go to the Nginx source directory and compile the submodules

Official documentation is as follows:

$ cdX $./configure --with-compat --add-dynamic-module=/ path /ngx_brotli $make modulesCopy the code

This example uses (ngx_brotli has been cloned to the local ~ directory) :

$ cd ~/nginx-1.20.1
$ ./configure --with-compat --add-dynamic-module=../ngx_brotli
$ make modules
Copy the code
  1. Move the generated.so dynamic library to the modules directory of Nginx (default/etc/nginx/modules)
$ cp objs/*.so /etc/nginx/modules
Copy the code
  1. Add permissions to the.so dynamic library file
$ chmod 644 /etc/nginx/modules/*.so
Copy the code
  1. tonginx.confAdd a configuration to the HTTP block in
load_module modules/ngx_http_brotli_filter_module.so;
load_module modules/ngx_http_brotli_static_module.so;
Copy the code

The module is statically compiled and installed

Replace the character X in the first line with the Nginx source version number you obtained above

$ cd nginx-1.x.x
$ ./configure --add-module=/path/to/ngx_brotli
$ make && make install
Copy the code

This will compile ngx_brotli directly and install it directly into Nginx

Configuration items

brotli_static
  • Grammar:brotli_static on|off|always
  • Default value:off
  • Context:http.server.location

Enable or disable automatic checking for existence of.br precompressed files before using; If the value is always, the precompressed file is used without checking.

brotli
  • Grammar:brotli on|off
  • Default value:off
  • Context:http.server.location.if

Enables or disables dynamic compression of the response body.

brotli_types
  • Grammar:Brotli_types < MIME type > [..]
  • Default value:text/html
  • Context:http.server.location

In addition to text/ HTML, dynamic compression of specific MIME types is supported. The special value * will match all MIME types, and response bodies with content-type text/ HTML will always be compressed.

brotli_buffers
  • Grammar:brotli_buffers <number> <size>
  • Default value:32 4k|16 8k
  • Context:http.server.location

The configuration item is deprecated.

brotli_comp_level
  • Grammar:Brotli_comp_level < level >
  • Default value:6
  • Context:http.server.location

Set Brotli dynamic compression quality level. The value ranges from 0 to 11. A larger value leads to higher performance loss and compression ratio. The default value is recommended.

brotli_window
  • Grammar:Brotli_window < size >
  • Default value:512k
  • Context:http.server.location

Set Brotli window size to 1K, 2K, 4K, 8K, 16K, 32K, 64K, 128K, 256K, 512K, 1m, 2m, 4m, 8m, and 16m. Gzip compression uses fixed-size 32K Windows, which means that Brotli Windows can be up to 512 times larger than Deflate Windows. This difference is almost irrelevant in the context of a Web server, because text files larger than 32KB are rare.

brotli_min_length
  • Grammar:Brotli_min_length < length >
  • Default value:20
  • Context:http.server.location

Set the minimum Length of the response body with Brotli compression enabled. This value is determined only by the Content-Length field in the response header.

variable

$brotli_ratio: The calculation result of the compression ratio, the ratio between the original response size and the compressed response size.

Sample configuration

brotli on;
brotli_comp_level 6;
brotli_static on;
brotli_types application/atom+xml application/javascript application/json application/rss+xml
             application/vnd.ms-fontobject application/x-font-opentype application/x-font-truetype
             application/x-font-ttf application/x-javascript application/xhtml+xml application/xml
             font/eot font/opentype font/otf font/truetype image/svg+xml image/vnd.microsoft.icon
             image/x-icon image/x-win-bitmap text/css text/javascript text/plain text/xml;
Copy the code