Install Brotli on Nginx to Improve Website Loading Speed

One way to speed up website access is to cache data. Another effective way is to enable data compression. Gzip is a common compression function. Today, we will introduce another compression method: the brotli module developed by Google. Brotli was originally released in 2015 for offline compression of web fonts. Google software engineers released an enhanced version of Brotli that includes general lossless data compression in September 2015.

The encoder has been partially rewritten to increase the compression ratio, both the encoder and decoder have been improved in speed, and the streaming API has been improved to add more compression quality levels. The new version also showcases cross-platform performance improvements, as well as reduced memory required for decoding.

For the difference between brotli and Gzip, please read: Gzip vs Brotli compression algorithm, which is better?

Install Brotli on Nginx and enable

nginx currently does not support the Brotli algorithm, and requires the use of third-party modules, such ngx_brotlias implementation.

Download ngx_brotlithe module and its dependencies:

$ git clone https://github.com/google/ngx_brotli
$ cd ngx_brotli
$ git submodule update --init

ngx_brotliAdd the module when compiling Nginx :

$ cd /path/to/nginx_source/
$ ./configure --add-module=/path/to/ngx_brotli
$ make && make install

httpAdd the following directives under the block of the Nginx configuration file :

brotli               on;  
brotli_comp_level    5;  
brotli_buffers       16 8k;  
brotli_min_length    20;
brotli_static on;
brotli_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript application/javascript application/vnd.ms-fontobject application/x-font-ttf font/opentype image/svg+xml image/x-icon;

brotli_static

When enabled will check brfor pre-compressed files with extensions. If it is always, the compressed file is always used, regardless of browser support.

brotli

Whether to enable compressing files in on-the-fly mode, after enabling, the files will be compressed and returned in response.

brotli_types

Specifies which content-encoding types are compressed. text/htmlContent is always compressed.

brotli_buffers

Set the number and size of buffers. The size defaults to the size of a memory page, ie, 4kor 8k.

brotli_comp_level

Sets the compression quality level. The value range is 0 to 11.

brotli_window

Set the window size.

brotli_min_length

Sets the minimum response size that needs to be compressed.

Leave a Comment