Google introduced the revolutionary lossless compression algorithm Brotli in September 2015. By leveraging advanced techniques such as a modified LZ77 algorithm, Huffman coding, and second-order text modeling, Brotli achieves data compression that is an impressive 17-25% more efficient than traditional Gzip. This translates to faster page loading speeds, reduced bandwidth usage, and improved user experiences.
As a webmaster or developer, how can you implement this powerful compression technology on your website? Many tutorials available online are outdated. Here’s a comprehensive, tested guide on enabling Brotli compression in Nginx with the Baota panel, providing you with a clear and reliable operational blueprint (tested as of September 29, 2024). Let’s elevate your website’s speed!
Step 1: Install libbrotli
First, ensure you have the latest kernel and BBR acceleration installed on CentOS7. Then, run the following commands to install libbrotli:
cd /www/server
git clone https://github.com/bagder/libbrotli
cd libbrotli
./autogen.sh
./configure
make && make install
It is advisable not to run the above commands on a fresh system. First, install the nginx suite environment using the Baota panel, then uninstall nginx before proceeding to these commands to avoid unnecessary complications.
Step 2: Download the ngx_brotli Module and Dependencies
Execute the following commands to download the ngx_brotli module:
cd /www/server
# Download Brotli
git clone https://github.com/google/ngx_brotli.git
cd ngx_brotli
# Update Brotli
git submodule update --init
During installation, I had to upgrade the nginx version to 1.25 (which was later updated to 1.26). Unfortunately, I encountered errors that wasted a day. (This issue doesn’t occur on all systems using nginx 1.25 or higher; for instance, opencloudos9 installs as expected while CentOS7 encounters errors.) If you face such issues, simply revert brotli to version 1.0.9.x by executing the following commands within the ngx_brotli directory:
wget https://ghproxy.com/https://github.com/google/brotli/archive/refs/tags/v1.0.9.tar.gz
tar -xzf v1.0.9.tar.gz
mv brotli-1.0.9/* deps/brotli
(Note: The command in the BT forum is mv brotli-1.0.9 deps/brotli/
, which will move the entire 1.0.9 folder to the Brotli directory. This is incorrect and difficult to notice, leading to further complications.)
Step 3: Modify nginx.sh
To check the current Nginx version information, use the command:
nginx -V
Then, find the ./configure
line in /www/server/panel/install/nginx.sh
and append:
--add-module=/www/server/ngx_brotli
Step 4: Compile and Install Nginx
Return to your terminal (xshell or Baota SSH terminal) and begin the compilation using the following command based on your version (adjust the number accordingly for versions like 1.26, 1.24, or OpenResty):
Method 1: Command Line Installation
sh /www/server/panel/install/nginx.sh install 1.26
Method 2: Baota Panel Installation
Go to the software list, select nginx, and click Add Module.
Select the added module and choose Compile and Install. Wait for the process to complete.
Step 5: Verify Installation
Once installed, use the command:
nginx -V
to check if the module is included.
Step 6: Enable Brotli Compression
Access the panel and navigate to Nginx settings. Modify the configuration in the http
segment by adding the following content to activate Brotli compression:
brotli on; # Enable Brotli
brotli_comp_level 6; # Compression level; default is 6 (maximum of 11, though higher compression may require more CPU)
brotli_buffers 16 8k; # Number and size of request buffers
brotli_min_length 20; # Minimum length of data to be compressed (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; # Allowed types for compression
brotli_static always; # Allow searching for pre-compressed files ending with .br
brotli_window 512k; # Window value, default is 512k
Conclusion
The compression results are commendable and particularly beneficial for those with an obsession for optimization. Brotli and Gzip can coexist, ensuring that older browsers that do not support Brotli automatically revert to Gzip compression. Implementing this data compression technology will significantly enhance your website performance.