CentOS 8 uses dynamic module mode to install and load Nginx module ngx_brotli then enables brotli compression

Project address: Github warehouse address

ngx_brotli

Brotli is a general lossless compression algorithm. It combines the modern variant of LZ77 algorithm, Huffman coding and second-order context modeling to compress data. Its compression ratio is comparable to the best general compression method available at present. It is similar in speed to deflate, but provides a higher compression rate.

Note: this compression code can only be used for https communication.

ngx_brotli consists of two modules:

  • ngx_brotli filter module - used to dynamically compress the response body.
  • ngx_brotli static module - used for services (static sites) using pre compressed. br files.

Installation method

Get ngx_brotli

  1. Make sure you have git installed. If not, use the command:
$ yum -y install git
  1. Get NGX from warehouse_ brotli
$ git clone https://github.com/google/ngx_brotli.git

If the server is located in China, it is recommended to use mirroring:

$ git clone https://gitee.com/xlsw/ngx_brotli.git
  1. Initialization sub module
$ cd ngx_brotli
$ git submodule update --init 
$ cd ~

Get Nginx source code

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

The installed nginx version of this example is:

nginx version: nginx/1.20.1

  1. Get and unzip the Nginx source code (replace the character x with your Nginx version number. If it is not installed, it can be customized)
$ wget https://nginx.org/download/nginx-1.x.x.tar.gz && tar zxvf nginx-1.x.x.tar.gz

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.gz
  1. Clear junk files
$ rm nginx-1.20.1.tar.gz

install

Select one of the following two methods. Nginx has been installed through yum source. It is recommended to select the first dynamic module loading method

Dynamic module loading

  1. Environment required for installation and compilation
$ yum -y install pcre pcre-devel zlib zlib-devel openssl openssl-devel
  1. Enter the Nginx source directory and compile the sub modules

The official document description is:

$ cd nginx-1.x.x
$ ./configure --with-compat --add-dynamic-module=/route/ngx_brotli
$ make modules

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
  1. Move the generated. so dynamic library to the modules directory of Nginx (the default is / etc/nginx/modules)
$ cp objs/*.so /etc/nginx/modules
  1. Add permissions to. so dynamic library files
$ chmod 644 /etc/nginx/modules/*.so
  1. Add configuration to nginx.conf (above http block)
load_module modules/ngx_http_brotli_filter_module.so;
load_module modules/ngx_http_brotli_static_module.so;

Module static compilation and installation

Replace the character x in the first line with the version number of the Nginx source code you obtained above

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

This operation will directly transfer ngx_brotli compiles and installs directly into Nginx

Configuration item

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

Enable or disable automatic check for. br pre compressed files before use; If the value is always, the pre compressed file is used instead of checking.

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

Enables or disables dynamic compression of the response body.

brotli_types
  • Syntax: brotli_ Types < MIME type > [..]
  • Default: text/html
  • Context: http, server, location

In addition to text/html, it also supports dynamic compression of specific MIME types. The special value * will match all MIME types, and the response body with content type text/html will always be compressed.

brotli_buffers
  • Syntax: brotli_ buffers <number> <size>
  • Default value: 32 4K × 16 8K
  • Context: http, server, location

Deprecated, ignoring this configuration item.

brotli_comp_level
  • Syntax: brotli_ comp_ Level < level >
  • Default: 6
  • Context: http, server, location

Set Brotli dynamic compression quality level from 0 to 11. The larger the value, the higher the performance loss and the higher the compression ratio. It is recommended to default.

brotli_window
  • Syntax: brotli_ Window < size >
  • Default: 512k
  • Context: http, server, location

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

brotli_min_length
  • Syntax: brotli_ min_ Length < length >
  • Default: 20
  • Context: http, server, location

Set the minimum length of the response body with Brotli compression enabled. This value will only be judged according to the content length field in the response header.

variable

$brotli_ratio: achieve the compression ratio, the ratio between the original response size and the compressed response size, and calculate the result.

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;

Tags: Linux CentOS Nginx

Posted on Thu, 11 Nov 2021 18:24:08 -0500 by wemustdesign