Description: Brotli
is an open-source compression algorithm launched by Google
, which compresses data using a variant of the LZ77
algorithm, Huffman
coding, and second-order text modeling. Compared to other compression algorithms, it has a higher compression efficiency and performs 17-25%
better than the commonly used Gzip
, helping us compress various file sizes and scripts on web pages more efficiently, thereby improving loading speed and enhancing the web browsing experience. The blogger has also enabled Brotli
compression, and the experience is decent. Here, I will explain how to enable it on the Baota panel.
Installation#
1. Download Brotli
cd /www/server
# Download Brotli
git clone https://github.com/google/ngx_brotli.git
cd ngx_brotli
# Update Brotli
git submodule update --init
2. Compile Nginx
Note: Manual compilation has only been tested with Nginx 1.15; some versions may prompt missing modules. It is recommended to use the later Baota script compilation method.
First, check the current Nginx
version information using the command:
nginx -V
It will output information similar to the following:
[root@rats ~]# nginx -V
nginx version: nginx/1.15.10
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-36) (GCC)
built with OpenSSL 1.1.1b 26 Feb 2019
TLS SNI support enabled
configure arguments: --user=www --group=www --prefix=/www/server/nginx --with-openssl=/www/server/nginx/src/openssl ... --with-ld-opt=-ljemalloc
The nginx
version is 1.15.10
, and the configure arguments:
following it are your nginx
compilation parameters, which will be used later.
Then, re-download nginx
and start the compilation using the command:
# Download nginx, here we are downloading version 1.15.10. If it's another version, just change 1.15.10 in the download link to your version number.
wget http://nginx.org/download/nginx-1.15.10.tar.gz
# Unzip and delete the compressed package
tar -xvzf nginx-*.tar.gz && rm -rf nginx-*.tar.gz
# Enter the nginx directory
cd nginx*
# Generate Makefile, copy the parameters after ./configure from above, and add --add-module=/www/server/ngx_brotli at the end.
./configure --user=www --group=www --prefix=/www/server/nginx ... --add-module=/www/server/ngx_brotli
# Compile nginx
make && make install
If everything goes well, the compilation will be complete. Then continue to use the command to check the information:
nginx -V
If the returned parameters include --add-module=/www/server/ngx_brotli
, the compilation was successful.
In addition to manual compilation, there is a more convenient and less error-prone method using the built-in Nginx
installation script of Baota for compilation and installation. The general steps are:
1. Edit the nginx installation script located at /www/server/panel/install/nginx.sh. Find the Install_Configure(){...} or Install_Nginx(){...} section, then locate the version number of nginx you want to install, and add --add-module=/www/server/ngx_brotli after the ./configure --user=www ... line, making sure to leave a space.
2. Use the command in the SSH client to start the compilation. Change the number at the end to 1.10, 1.12, 1.14, 1.15, 1.17, 1.8, openresty, etc., according to your version.
sh /www/server/panel/install/nginx.sh install 1.16
3. After installation, use nginx -V to check if the module is included.
3. Enable Brotli Compression
Next, click on the software store on the left side of the panel - Nginx
settings - configuration modification, and add the following content in the http
section to enable Brotli
compression.
brotli on;
brotli_comp_level 6;
brotli_min_length 512;
brotli_types text/plain text/javascript text/css text/xml text/x-component application/javascript application/x-javascript application/xml application/json application/xhtml+xml application/rss+xml application/atom+xml application/x-font-ttf application/vnd.ms-fontobject image/svg+xml image/x-icon font/opentype;
brotli_static always;
Finally, click on the reload configuration in the Nginx
settings to make it effective.
Detailed explanation of all Brotli
parameters:
brotli on; # Enable
brotli_comp_level 6; # Compression level, default is 6, maximum is 11; too high a compression level may require more CPU
brotli_buffers 16 8k; # Number and size of request buffers
brotli_min_length 20; # Specify the minimum length of data to be compressed; only data greater than or equal to this length will be compressed. Here it is set to 20 bytes
brotli_types text/plain application/javascript application/x-javascript text/javascript text/css application/xml text/html application/json image/svg application/font-woff application/vnd.ms-fontobject application/vnd.apple.mpegurl image/x-icon image/jpeg image/gif image/png image/bmp; # Specify the types allowed for compression
brotli_static always; # Whether to allow searching for pre-processed compressed files ending with .br; optional values are on, off, always
brotli_window 512k; # Window value, default is 512k
Once all configurations are set, you can use Google Chrome to check if it has been successfully enabled. Seeing the br
field indicates success.
Finally, the blogger feels that the compression effect is decent. If you're interested, you can enable it. The installation methods are similar for other environments. Here, it is assumed that Brotli
and Gzip
coexist and both are enabled, which is beneficial because if some older browsers do not support Brotli
, they will automatically switch to Gzip
compression.