2016-12-24

Last year, Google released a successor to the deflate compression algorithm, Brotli. Chrome adopted it in version 51, and Firefox in version 44 (see Can I use…). That said, from the webserver side, nginx doesn’t support it natively, so Google provides the ngx_brotli module, making it just a matter of compiling nginx.

Compiling a custom nginx build is not as daunting as it sounds. I described the process back in June, when I desired to preserve http/2 support so needed to switch to OpenSSL 1.0.2: https://ethitter.com/2016/06/nginx-openssl-1-0-2-http-2-alpn/.

Getting Brotli

First, we need the nginx module and the Brotli library checked out where we’ll build nginx. I use /usr/local/src when building from sources, but the directory can be almost anything you’d like.

Now it’s time to compile an updated nginx.

Updating the nginx build configuration

Using the configuration from my earlier post, compiling nginx with Brotli is a matter of adding one additional flag to the ./configure call:

In my case, the full command is now:

Note that starting with 1.11.5, the --with-ipv6 flag was removed because the support is now added by default.

nginx can now be compiled and installed as noted in my previous post.

Enabling Brotli

Now that nginx supports Brotli, its nginx.conf needs a small update to enable the new compression.

Run nginx -t to confirm the configuration and binary, then restart nginx to enable Brotli for all supported requests.

Two flavors of Brotli

In the above configuration, two types of Brotli were enabled: brotli and brotli_static. Given the name of the latter, one can probably figure out that brotli enables the compression when each request is processed. brotli_static allows pre-compressed versions of files to be served automatically when they’re present.

Creating the pre-compressed versions requires the Brotli command-line tool, which Scott Helme discusses in https://scotthelme.co.uk/brotli-compression/. Once .br files are present, nginx will serve them instead of dynamically compressing the sources.

Since the bulk of my server’s requests are fronted by a CDN that doesn’t support Brotli (it falls back to gzip, fortunately), or are dynamic and served by WordPress, I only make use of on-the-fly compression. GitLab doesn’t support a CDN, so I may create static versions of its public assets; my installation is a manual upgrade anyway, so recreating the Brotli versions with each update would be trivial.

Final Thoughts

In addition to Scott Helme’s post, I found Sam Saffron’s review of Brotli to be very insightful: https://samsaffron.com/archive/2016/06/15/the-current-state-of-brotli-compression.

Show more