The title
Brotli is a compression algorithm that boasts faster compression times and greater compression of webpages than its predecessor GZIP. It is open-source, free to use, and already supported by modern web servers and browsers.
On 10Web hosting, brotli is enabled by default. Let’s dig into some of the benefits of using brotli compression.
Purpose, usage, and efficiency
Brotli was originally developed by Google in 2013 to compress web fonts and gained popularity in 2015 as it focused more on HTTP stream compression.
Most modern web servers already support using Brotli as a compression format.
One of the features that help Brotli increase its compression efficiency is that it uses a pre-defined dictionary of commonly used words. This reduces the amount of data because both the server and client have this dictionary.
Brotli compression vs. GZIP
Both brotli and GZIP decrease page size by making files smaller. Smaller files mean faster page load times.
Feature | Brotli | GZIP |
Compression Algorithms | Brotli uses LZ77 and Huffman encoding and a pre-defined static dictionary. | GZIP uses LZ77 and Huffman encoding. |
Compression Performance | Brotli on average, produces 1.19x smaller files than GZIP. Depending on the compression level and dataset, Brotli can either be faster or slower than GZIP |
On the maximal compression level, GZIP files, on average, are larger than those compressed with Brotli. On speed, there are cases where GZIP is faster than Brotli. |
Support | Brotli is supported on most major servers and browsers. Apache has supported pre-built modules, but Nginx requires the user to build the module. |
Wide adoption since the 1990s. Supported by all major web servers and browsers. Both Nginx and Apache have built-in modules. |
Cloudflare has an in-depth breakdown of their bench-mark testing.
Static vs. dynamic compression
When serving compressed data, static compression and dynamic compression are two ways to serve up the compressed data. Brotli can be used to do both dynamic and static compression.
Dynamic compression happens when a file is compressed on-the-fly. A web server receives a request, and the requested file is compressed before sending it over.
Static compression is when the files on the disk are pre-compressed. This avoids having to compress the file on each request, saving CPU time.
How to test brotli compression
There are many easy ways to determine if brotli compression is enabled on your web server.
Online Brotli Compression Test
An excellent online tool is https://www.giftofspeed.com/GZIP-test/. Do not let the name of the URL dissuade you. This tests for both GZIP and Brotli. Enter the URL in question; the test will return if compression is enabled for Brotli or another method.
Check the HTTP Header
The cURL command test if a server is enabled correctly. In the command below, the options -L is used to follow potential redirects (301, 302), and the -H sends the header, letting the server know this request can accept GZIP encoding.
curl -LH "Accept-Encoding: br" -I http://website.com
The response should contain something similar to the following.
content-encoding: br
How to enable brotli compression
Apache and Nginx both support brotli compression. Just starting out, Apache has better adoption of brotli because they already have a module build for brotli. Nginx requires a bit more work to get it configured.
Nginx
There are two ways to enable brotli on an Nginx server. If you are a Nginx+ subscriber, there is a supported brotli module pre-built and ready to use. You can read more about how to configure this module on Nginx’s website.
The second method is to build the brotli modules for your version of Nginx from the source code provided on GitHub.
After installing the brotli modules, the following two sections can be added to nginx.conf.
Add the following lines near the top of the file.
load_module modules/ngx_http_brotli_filter_module.so; load_module modules/ngx_http_brotli_static_module.so;
Add the following inside the HTTP section of the file.
brotli on; brotli_comp_level 6; brotli_static on; brotli_types application/atom+xml application/javascript application/json application/rss+xml application/vnd.ms-fontobject application/x-font-opentype application/x-font-truetype application/x-font-ttf application/x-javascript application/xhtml+xml application/xml font/eot font/opentype font/otf font/truetype image/svg+xml image/vnd.microsoft.icon image/x-icon image/x-win-bitmap text/css text/javascript text/plain text/xml;
Apache
Apache has a supported brotli module that is packaged with Apache on both CentOS and Debian-based systems.
CentOS Red Hat
On a CentOS or Red Hat machine, the brotli module is already loaded in the /etc/httpd/conf.modules.d/00-base.conf file.
Add the following to the httpd.conf file.
AddOutputFilterByType BROTLI_COMPRESS text/html text/plain text/xml text/css text/javascript application/javascript
Ubuntu / Debian
Enable the brotli module
# a2enmod brotli Enabling module brotli. To activate the new configuration, you need to run: systemctl restart apache2
In the configuration file /etc/apache2/sites-enabled/000-default.conf add the following inside the <VirtualHost> tags.
AddOutputFilterByType BROTLI_COMPRESS text/html text/plain text/xml text/css text/javascript application/javascript
Web Hosting
Check with your hosting provider to see if they support enabling brotli compression. Support will depend on the type of hosting you are using.
How to implement brotli compression in WordPress
Enabling brotli compression on a WordPress website depends on the ability of the server to support brotli.
The ability to support compression varies on the type of hosting you have. Check your provider’s help documentation to see how to enable brotli compression.
If your hosting provider does not support brotli, a popular way to add brotli compression to your website is through Cloudfare CDN. Once configured, you can enable brotli inside the Cloudflare control panel under the ‘Speed’ tab.