Add Brotli compression to Nginx on Ubuntu
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.
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.
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.
To correctly do this step we need to make nginx from sources with all arguments from current installing.
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.
Verify that Brotli is working.
Check value of a response header with the name “content-encoding”. It should be “br”.