A better compression than gzip for your website aka Brotli

Written by James McDonald

February 16, 2022

Apparently brotli is a more efficient compression protocol than gzip.

Install both the PHP and the Nginx support

Install nginx Support

https://www.atlantic.net/dedicated-server-hosting/how-to-install-brotli-module-for-nginx-on-ubuntu-20-04/

Install PHP Brotli Support

https://github.com/kjdev/php-ext-brotli

My Notes

I noticed that W3 Total Cache has a check for “brotli” compression so decided to investigate and then to enable it as it appears to be much more efficient than gzip.

Left gzip on in http section of my nginx.conf and created a file named /etc/nginx.conf/brotli-compression.conf and used include brotli-pression.conf in nginx.conf next to the gzip on directive

# contents of /etc/nginx/brotli-compression.conf
brotli on;
brotli_comp_level 6;
brotli_static on;
brotli_types text/plain text/css application/javascript application/x-javascript text/xml
application/xml application/xml+rss text/javascript image/x-icon
image/vnd.microsoft.icon image/bmp image/svg+xml;

Nginx.conf edits

# in /etc/nginx/nginx.conf
http {
        # snippage
        ##
        # Gzip Settings
        ##
        include brotli-compression.conf;
        gzip on;
        # more snippage
        include /etc/nginx/conf.d/*.conf;
        include /etc/nginx/sites-enabled/*;
}

Commands to install on Ubuntu 20.04

I have Ubuntu 20.04 installed so I can use PHP8.0+

sudo add-apt-repository ppa:ondrej/php
sudo apt-get update

Commands to get both PHP and Nginx modules installed

 sudo apt-get install libnginx-mod-brotli
 sudo apt-get install brotli
 
 git clone --recursive --depth=1 https://github.com/kjdev/php-ext-brotli.git
 cd php-ext-brotli/
 sudo apt install php8.0-dev
 phpize
 sudo apt install libbrotli-dev libbrotli1
 ./configure --with-libbrotli
 make
 sudo make install
 echo "extension=brotli.so" > /etc/php/8.0/mods-available/brotli.ini
 sudo phpenmod brotli
 sudo systemctl restart php8.0-fpm.service
 sudo systemctl restart nginx.service

What it looks like in Developer Tools when you have brotli compression working?

Notice how the Requesting client (a modern browser says I will accept gzip, deflate, br and the server responds with content-encoding: br

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

The reCAPTCHA verification period has expired. Please reload the page.

You May Also Like…

Clear HSTS Settings in CHrome

Open chrome://net-internals/#hsts enter the domain in the query field and click Query to confirm it has HSTS settings...

Ubuntu on Hyper-v

It boils town to installing linux-azure # as root or sudo apt-get update apt-get install linux-azure...