Brotli is developed by Google as an alternative to Gzip, Zopfli and Deflate, but we can say it’s enhancement.
Our case study on Brotli has shown compression ratios of more than 25% smaller than current methods, with less CPU usage.
Currently, Nginx does not have official support to Brotli.
But Google develop third-party on github called ngx_brotli that you can use on Nginx.
According to Brotli Github project descriptions, Brotli is a generic-purpose lossless compression algorithm that compresses data using a combination of a modern variant of the LZ77 algorithm, Huffman coding and 2nd order context modeling, with a compression ratio comparable to the best currently available general-purpose compression methods.
It is similar in speed with deflate but offers more dense compression.
How to install Nginx Brotli module
# yum groupinstall 'Development Tools' -y
# yum install gcc-c++ flex bison yajl yajl-devel curl-devel curl GeoIP-devel doxygen zlib-devel pcre-devel
# yum install lmdb lmdb-devel libxml2 libxml2-devel ssdeep ssdeep-devel lua lua-devel
Clone ngx_brotli from Github https://github.com/google/ngx_brotli
# cd /usr/src
# git clone https://github.com/google/ngx_brotli.git
# cd ngx_brotli
# git submodule update --init
Check your Nginx version with command:
# nginx -V
nginx version: nginx/1.16.1
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-36) (GCC)
built with OpenSSL 1.0.2k-fips 26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie'
Now you need to download Nginx source, depend on your Nginx version.
In this example is Nginx 1.16.1, compile module and compiled Nginx Brotli located in objs.
# cd /usr/src
# wget http://nginx.org/download/nginx-1.16.1.tar.gz
# tar zxvf nginx-1.16.1.tar.gz
# cd nginx-1.16.1
# ./configure --with-compat --add-dynamic-module=../ngx_brotli
# make modules
# cp objs/ngx_http_brotli_static_module.so /etc/nginx/modules
# cp objs/ngx_http_brotli_filter_module.so /etc/nginx/modules
# chmod 644 /etc/nginx/modules/ngx_http_brotli_static_module.so
# chmod 644 /etc/nginx/modules/ngx_http_brotli_filter_module.so
Configure Nginx Brotli compression module
To load Brotli modules on Nginx, edit /etc/nginx/nginx.conf and add this code in top of configuration.
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
load_module modules/ngx_http_brotli_filter_module.so;
load_module modules/ngx_http_brotli_static_module.so;
And on your server block add this code:
server {
.......
.......
brotli on;
brotli_static on; # for static compression, explained later
brotli_comp_level 6; # this setting can vary from 1-11
brotli_types text/xml image/svg+xml application/x-font-ttf image/vnd.microsoft.icon application/x-font-opentype application/json font/eot application/vnd.ms-fontobject application/javascript font/otf application/xml application/xhtml+xml text/javascript application/x-javascript text/plain application/x-font-truetype application/xml+rss image/x-icon font/opentype text/css image/x-win-bitmap;
gzip on;
.......
.......
}
Check your Nginx configuration with
# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
Now start your nginx and check Brotli sith CURL
# curl -s -I -H 'Accept-Encoding: br,gzip,deflate' https://serverdiary.com
HTTP/2 200
date: Sun, 08 Nov 2020 12:47:20 GMT
content-type: text/html; charset=UTF-8
vary: Accept-Encoding
x-cache-page-memory: HIT
x-frame-options: SAMEORIGIN
x-content-type-options: nosniff
x-xss-protection: 1; mode=block
content-encoding: br
You also can check online and compare Brotli compression vs Gzip compression on https://tools.paulcalvano.com/compression.php

You can see the result that HTML uncompressed size is 397.4 Kb, gzip compression size is 47.9 Kb and Brotli compression size is 38.8 Kb
Thanks for your tutorial!!
You’re welcome. Thanks for visiting Serverdiary.com
great job thanks a lot
Hey! Good article and great content on the site!
Installed openssl-3.0.1. When checked, openssl version gives a new version 3. When checking nginx -V gives – nginx version: nginx/1.20.2
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-36) (GCC)
built with OpenSSL 1.0.2k-fips 26 Jan 2017
Can you write an article on how to install the new openssl-3.0.1 on nginx.
Thanks in advance.