Add Brotli compression to Nginx on Ubuntu

Peter Bazov
2 min readDec 17, 2019

--

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

Step 1

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.

Step 2

Download and extract Brotli sources.

Step 3

Install required dependencies.

Step 4

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.

Step 5

Copy modules to the nginx directory.

Step 6

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

Restart nginx, but check configuration first.

Step 7

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.

Step 8

Verify that Brotli is working.

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

--

--