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 repository to easily install RPM package for dynamic Brotli module for NGINX.
The dynamic module is compatible with the latest stable NGINX for CentOS 7.
Step 1. Add GetPageSpeed extras YUM repository
Run the following command to add our repository:
yum install https://extras.getpagespeed.com/release-el7-latest.rpm
Step 2. Install NGINX and Brotli module
Then, all you have to do to install NGINX with Brotli module:
yum install nginx nginx-module-nbr
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 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/eustas/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:
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
It’s easiest to launch Terminal app (if you have OS X) and check if your NGINX emits Brotli encoded responses.
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.
- Launch Chrome (what developer doesn’t have it?) and navigate to your website
- Right-click anywhere on the opened page and choose “Inspect”.
- 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.
Feels like something is still missing? Then continue reading on how to install PageSpeed module in NGINX 🙂
Also published on Medium.