How to Install & Enabling Brotli Compression for Apache/NGINX

If you are unsure of how to Install & enable Brotli on your Apache/Nginx web server, then hopefully this helps!

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.

Google has released a Nginx Brotli module.

Prerequisites
I’m not sure if there is a technical reason behind this, but all the browsers I tested with require HTTPS in order to enable Brotli support.
• Apache/NGINX installed.
• HTTPS enabled.
• Access to your Apache/NGINX installation (no shared hosting)
• Brotli binary installed

Installing Brotli

apt-get install brotli

Setting up Brotli on Apache

Apache has supported brotli since version 2.4.26 by way of the mod_brotli module.
However, I can’t find any information on this so we are installing this module by kjdev.

Install the Module

git clone --depth=1 --recursive https://github.com/kjdev/apache-mod-brotli.git
cd apache-mod-brotli
./autogen.sh
./configure
make
install -D .libs/mod_brotli.so /usr/lib/apache2/modules/mod_brotli.so -m 644
cd /etc/apache2/mods-available
echo "LoadModule brotli_module /usr/lib/apache2/modules/mod_brotli.so" > brotli.load

This has added the .load file to the mods available. We need to create an accompanying config file called brotli.conf, adding:

<IfModule brotli_module>
  BrotliCompressionLevel 10
  BrotliWindowSize 22
  AddOutputFilterByType BROTLI text/html text/plain text/css application/x-javascript
<IfModule brotli_module>

Enable the module

a2enmod brotli
service apache2 restart

You should now see in the response header that the page is compressed with brotli (br).

Setting up on Nginx

Download the module

cd /usr/local/src
git clone https://github.com/google/ngx_brotli.git
cd ngx_brotli
git submodule update --init --recursive

Rebuild Nginx with our new module
You should run nginx -V to get your config string and add:

cd /opt/nginx/  (or your own path)
./configure YOUR CONFIG STRING --add-module=/usr/local/src/ngx_brotli
make
make install

Finally, add to your nginx.conf file

http {
    brotli on;
    brotli_static on;
}

In conclusion, the setup for both Apache and Nginx is pretty painless. If the browser does not support brotli it can always fallback to the ever faithful gzip.

Does your browser support Brotli?
Browsers which support Brotli send ‘br’ along with ‘gzip’ in accept-encoding request header. If Brotli is enabled succesful on your web server, you will get response in Brotli compressed format.

HTTP/2 200
server: nginx
date: Thu, 11 Apr 2019 21:01:27 GMT
content-type: text/html
content-length: 32395
last-modified: Thu, 04 Apr 2019 13:49:47 GMT
etag: "3c420-585b4a4af8fe6-br"
accept-ranges: bytes
cache-control: max-age=0
expires: Thu, 11 Apr 2019 21:01:27 GMT
vary: Accept-Encoding,User-Agent
content-encoding: br
strict-transport-security: max-age=31536000

Leave a Comment