banner
moeyy

moeyy

一条有远大理想的咸鱼。
github
mastodon
email

BT panel LNMP enables Brotli compression, which can improve website loading speed.

Note: Brotli is an open-source compression algorithm introduced by Google. It uses a variant of the LZ77 algorithm, Huffman coding, and second-order text modeling to compress data. Compared to other compression algorithms, it has higher compression efficiency and better performance. It can efficiently compress various types of files and scripts in web pages, thereby improving loading speed and enhancing the browsing experience. The author has currently enabled Brotli compression, and the experience is good. Here, I will explain how to enable it in 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 for missing modules. It is recommended to use the Baota script compilation method described later.

First, check the current version information of Nginx using the command:

nginx -V

It will output something like:

[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: section contains the compilation parameters for your nginx, which will be used later.

Next, download nginx again and start the compilation process using the following commands:

# Download nginx, here we download version 1.15.10. If you have a different version, change "1.15.10" in the download link to your version number.
wget http://nginx.org/download/nginx-1.15.10.tar.gz
# Extract and remove 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 the previous output, 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 should be completed. Then, use the following command to check the information:

nginx -V

image

If there is a --add-module=/www/server/ngx_brotli in the returned parameters, the compilation is successful.

In addition to manual compilation, there is a more convenient and low-error-rate method: using the Baota Panel's built-in Nginx installation script for compilation and installation. The general steps are as follows:

1. Edit the nginx installation script at /www/server/panel/install/nginx.sh. Find the Install_Configure(){...} or Install_Nginx(){...} section, and locate the version number of nginx you want to install. Add --add-module=/www/server/ngx_brotli after the ./configure --user=www ... line, with a single space in between.

2. Use the following command in the SSH client to start the compilation. Change the number at the end according to your version: 1.10, 1.12, 1.14, 1.15, 1.17, 1.8, openresty, etc.
sh /www/server/panel/install/nginx.sh install 1.16

3. After the 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, go to "Nginx Settings", and modify the configuration. Add the following content within 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 "Reload Configuration" in the "Nginx Settings" to apply the changes.

Explanation of all Brotli parameters:

brotli on;              # Enable
brotli_comp_level 6;    # Compression level, default is 6, maximum is 11. Higher compression levels may require more CPU resources.
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 the minimum 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 of content allowed to be compressed
brotli_static always;   # Whether to search for preprocessed compressed files ending in .br. Possible values are on, off, always.
brotli_window 512k;     # Window value, default is 512k

image

Once everything is configured, you can use Google Chrome to check if it is enabled. If you see the br field, it means it is successful.

In conclusion, the author feels that the compression effect is good. If you are interested, you can give it a try. The installation methods for other environments are similar. Here, it is assumed that Brotli and Gzip coexist and are both enabled. The advantage is that when some older browsers do not support Brotli, it will automatically switch to Gzip compression.

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.