Add Brotli compression to Nginx on Ubuntu

Introduction

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.

You can read more here: https://github.com/google/brotli.

Prerequisites

Before you begin you should verify that the nginx is installed in your server. If it doesn’t, you can read about installing process here: https://www.digitalocean.com/community/tutorials/how-to-install-nginx-on-ubuntu-18-04.

Instruction

Brotli for nginx is represented by two dynamic modules. One for static files and second for dynamic content. We must manually compile them for use. Unfortunalety, we need to compile nginx to solve this problem.

Download and extract nginx sources. Replace YOUR_NGINX_VERSION by version number from the sudo nginx -v output.

Download and extract Brotli sources.

Install required dependencies.

Make modules.

To correctly do this step we need to make nginx from sources with all arguments from current installing.

Run sudo nginx -V command and copy all text from “configure arguments” section after ‘:’ symbol.

Run next commands but replace $CONFIGURE_ARGUMENTS with your configure arguments from the previous output.

Copy modules to the nginx directory.

Say nginx to use modules. Add next lines at the top of the “/etc/nginx/nginx.conf” file.

Restart nginx, but check configuration first.

Enable Brotli for static files by adding few lines to the “/etc/nginx/nginx.conf” file in “http” section.

Enable Brotli for dynamic content by adding another instruction to the “/etc/nginx/nginx.conf” file in “http” section.

Restart nginx.

Verify that Brotli is working.

Check value of a response header with the name “content-encoding”. It should be “br”.

--

--

Software Engineer

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store