What is the brotli compression algorithm
brotli was originally released in 2015 for offline compression of web fonts. Google software engineers released an enhanced version of brotli in September 2015 that included general lossless data compression, with a special focus on http compression. The encoder has been partially rewritten to improve compression ratio, both the encoder and decoder have been increased in speed, and the streaming API has been improved to add more compression quality levels. The new version also showcases cross-platform performance improvements and reduced memory required for decoding.
Unlike common general-purpose compression algorithms, brotli uses a predefined 120 kilobyte dictionary. The dictionary contains over 13,000 common words, phrases, and other substrings drawn from a large corpus of text and HTML documents. Predefined algorithms increase compression density for smaller files.
Using brotli instead of deflate to compress text files can usually increase the compression density by 20%, while the compression and decompression speeds remain roughly unchanged. The content encoding type for stream compression using brotli has been proposed to use "br".
Installation
##1.Download brotli
1 2 | git clone https:
cd ngx_brotli && git submodule update --init
|
Copy after login
2.Compile
Add – after the original compilation configuration add-module=/opt/nginx/ngx_brotli
For example
Copy code The code is as follows:
./configure --prefix=/usr/local/nginx --user =www --group=www --with-pcre=/opt/nginx/pcre-8.41 --with-http_ssl_module --with-zlib=/opt/nginx/zlib-1.2.11 --with-openssl=/opt /nginx/openssl-1.0.2n --add-module=/opt/nginx/ngx_brotli --with-http_v2_module
configuration, add
1 2 3 4 5 6 7 8 9 10 11 12 | http {
include mime.types;
default_type application/octet-stream;
sendfile on;
tcp_nopush on;
keepalive_timeout 65;
#brotli compression
brotli on;
brotli_comp_level 6;
brotli_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript application/javascript image/svg+xml;
……
|
Copy after login
to the http segment, restart, refresh the page to view the header , found
1 | accept-encoding:gzip, deflate, br
|
Copy after login
as shown in the picture

, which means brotli compression is turned on
Configuration instructions
Instruction introduction
The following instructions are defined:
brotli, whether to allow dynamic compression of response data , the optional values are on and off, and the default value is off. An example is as follows:
brotli_types, when dynamic compression is enabled, compressed mime types are allowed, and the default value is text/html. An example is as follows:
1 | brotli_types text/plain text/css text/xml application/xml application/json text/javascript application/javascript application/x-javascript;
|
Copy after login
brotli_static, whether to allow searching for preprocessed compressed files ending with .br, the optional values are on, off and always, and the default value is off. The sample is as follows:
brotli_comp_level, compression level, the optional value range is 0~11, and the default value is 6. The sample is as follows:
brotli_buffers, the number and size of buffers used when compressing response data. The sample is as follows:
brotli_window, the window value used by brotli, the default value is 512k. An example is as follows:
brotli_min_length, the minimum length of response data. Below this value, the brotli algorithm will not be used to perform compression operations. The brotli algorithm uses content-length to determine the length of the response data. The sample is as follows:
The above is the detailed content of How to enable Brotli compression algorithm for Nginx. For more information, please follow other related articles on the PHP Chinese website!