Enable Brotli Compression in Nginx to Boost Website Speed
Google believes that internet users' time is precious and should not be wasted on long page load times. Therefore, in September 2015, Google launched the lossless compression algorithm Brotli. Brotli achieves higher compression efficiency compared to other algorithms by using a variant of the LZ77 algorithm, Huffman coding, and second-order text modeling.

Key Features of Brotli
- For common web resource content, Brotli's performance is 17-25% better than Gzip;
- When Brotli compression level is set to 1, the compression ratio is higher than Gzip at level 9 (the highest);
- Brotli still provides very high compression rates when processing different HTML documents.
Compiling and Installing the ngx_brotli Module for Nginx
Nginx does not support the ngx_brotli module by default, so it must be compiled manually. The compilation method is as follows:
# Download brotli first
git clone https://github.com/google/ngx_brotli.git
# Enter the directory
cd ngx_brotli
# Update brotli
git submodule update --init
# Enter the Nginx source directory
cd xxx/nginx
# Generate makefile, note to add modules according to your usage
./configure ... --add-module=../ngx_brotli
# Compile Nginx
make && make install
If the compilation is successful, you can see the ngx_brotli module by entering nginx -V, as shown in the screenshot below.

Enabling ngx_brotli Support
Modify nginx.conf and add the following content within the http section to enable Brotli compression:
# Enable Brotli compression
brotli on;
# Compression level, 0 to 11, default value is 6. Too high a value will consume extra server CPU
brotli_comp_level 6;
# Set the minimum response size to be compressed, in bytes
brotli_min_length 512;
# Specify which MIME types to compress
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;
# Whether to allow looking for pre-processed compressed files ending with .br. Optional values are on, off, always
brotli_static always;
Finally, don't forget to reload Nginx for the changes to take effect: nginx -s restart.
Additional Notes
Browsers that support the Brotli compression algorithm use the content encoding type br. For example, the value of Accept-Encoding in the Chrome browser request header is as follows (note that the browser only sends br in the Accept-Encoding header when using HTTPS):
Accept-Encoding: gzip, deflate, sdch, br
If the server supports the Brotli algorithm, it will return the following response header:
Content-Encoding: br
Brotli and Gzip can coexist. Therefore, it is recommended to enable both compression methods. When some older browsers do not support Brotli, they will automatically degrade to Gzip for processing.