How to install NGINX Brotli module in CentOS/RedHat, Amazon Linux or Fedora Linux

Danila Vershinin
3 min readJan 10, 2019

--

Brotli is the new compression algorithm, and it is now widely supported across many browsers. With Brotli being a clear winner over standard GZIP compression, not implementing it for your website is not just an oversight, but also negligence of its huge optimization potential.

You’ve been neglecting it, haven’t you? Time to fix things up :-)

You can use our commercial repository for production systems, to easily install the RPM package for the dynamic Brotli module for NGINX.
The dynamic module is compatible with the latest stable NGINX for CentOS/RHEL, Amazon Linux, Fedora Linux, etc..

Step 1. Add GetPageSpeed extras RPM repository

Run the following command to add our repository:

yum -y install https://extras.getpagespeed.com/release-latest.rpm

Step 2. Install NGINX and Brotli module

Then, all you have to do to install NGINX with the Brotli module:

yum -y install nginx nginx-module-brotli

Follow the installation prompt to import GPG public key that is used for verifying packages.

Step 3. Enable Brotli support in NGINX

Once you have installed the Brotli NGINX module package, you’ll notice it actually consists of the two separate modules. You may want to simply follow the installer’s suggestion:

----------------------------------------------------------------------

The Brotil dynamic modules for nginx have been installed.
To enable these modules, add the following to /etc/nginx/nginx.conf
and reload nginx:
load_module modules/ngx_http_brotli_filter_module.so;
load_module modules/ngx_http_brotli_static_module.so;

Please refer to documentation here:
https://github.com/google/ngx_brotli

----------------------------------------------------------------------

Doing so will enable both regular and static Brotli filters. But what is their purpose and why two?

The regular Brotli module, ngx_http_brotli_filter_module.so, will dynamically compress everything that NGINX serves to end visitors.

The static Brotli module, ngx_http_brotli_static_module.so, allows you to serve Brotli-encoded versions of your static files which you have pre-compressed in advance. More on that in the documentation.

So in most cases, you’re fine with just the regular Brotli module, but if you’re a hardcore optimizer you will pre-compress your files with Brotli’s highest compression level, and enable the second module as well.

Enable Brotli compression for all websites

Now that our NGINX has Brotli compression capability, you can start tinkering with configuration and actually enable the new compression algorithm.

The best way to do this is by using conf.d auto-include facility of your NGINX distribution. Create the new file /etc/nginx/conf.d/brotli.conf and define your desired compression parameters there. The following minimalistic configuration will ensure Brotli compression for the majority of compressible file formats:

brotli on;
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;

Now we’ve listed all the MIME types of files which will be Brotli-compressed. To apply the changes, reload your NGINX (if it was running), or start it.

Verify Brotli compression works

Via Command-Line

It’s easiest to launch Terminal app (if you have OS X) and check if your NGINX emits Brotli encoded responses.

Simply use curl like this:

curl -IL https://example.com -H "Accept-Encoding: br"

As long as you see the response includes Content-Encoding: br, it means that NGINX properly handles Brotli compression.

Using CLI to check Brotli support, implies at least some knowledge of working with the Terminal app. So you can use your browser method instead.

Via browser

  • Launch Chrome (what developer doesn’t have it?) and navigate to your website
  • Right-click anywhere on the opened page and choose “Inspect”.
  • The developer tools sidebar will open.
  • Click on “Network” in the developer tools sidebar, then “Doc”. This tells Chrome to log only requests to the main document.
  • Now reload the page, and expand the log entry which was created by clicking it.
  • Scroll down to “Response Headers” and find “Content-Encoding” header. The value of “br” means that the response was encoded via Brotli.
Brotli-encoded HTML in Chrome
Brotli-encoded HTML in Chrome

Congrats!

Feels like something is still missing? Then continue reading on how to install the PageSpeed module in NGINX :)

Originally published at GetPageSpeed.

--

--