Home > Article > Operation and Maintenance > How to enable Brotli compression algorithm for Nginx
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
git clone https://github.com/google/ngx_brotli cd ngx_brotli && git submodule update --init2.CompileAdd – after the original compilation configuration add-module=/opt/nginx/ngx_brotliFor exampleCopy 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
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; ……to the http segment, restart, refresh the page to view the header , found
accept-encoding:gzip, deflate, bras shown in the picture , which means brotli compression is turned on
Configuration instructions
Instruction introduction
brotli on;brotli_types, when dynamic compression is enabled, compressed mime types are allowed, and the default value is text/html. An example is as follows:
brotli_types text/plain text/css text/xml application/xml application/json text/javascript application/javascript application/x-javascript;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_static off;brotli_comp_level, compression level, the optional value range is 0~11, and the default value is 6. The sample is as follows:
brotli_comp_level 11;brotli_buffers, the number and size of buffers used when compressing response data. The sample is as follows:
brotli_buffers 16 8k;brotli_window, the window value used by brotli, the default value is 512k. An example is as follows:
brotli_window 512k;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:
brotli_min_length 20;
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!