How To Enable Brotli Compression in Apache
Abstract: This will install the required package containing the algorithm files on your system. Step 2 – Configure Brotli with Apache The Apache server contains
Just like Gzip, Brotli is also a generic-purpose compression algorithm developed by Google. It compresses data using a combination of modern technologies and algorithms. It is similar in speed to deflate but provides higher compression. Brotli compression is supported by all the major browsers like Chrome, Firefox, Safari, Edge.
The Brotli compression is opted by the top tech fortunes like Cloudflare etc. This is the reason, we recommend switching to brotli from the old deflate data compression algorithm.
This tutorial helps you to enable brotli compression in the Apache webserver.
PrerequisitesShell access to your server with sudo privileged account.
We assume that you already have a running Apache server. Also created a virtual host for the web application.
Step 1 – Installing BrotliFirst, install the brotli package on your system. For the Ubuntu and Debian systems, it’s available in the default repositories.
Open a terminal and type:
sudo apt install brotli -y
This will install the required package containing the algorithm files on your system.
Step 2 – Configure Brotli with ApacheThe Apache server contains the Brotli module default. You can enable the brotli module in Apache with the following command.
sudo a2enmod brotli
Next, you have to configure the Apache virtual host to enable compression with brotli. You need to add the below code in the virtual host configuration file.
<IfModule mod_brotli.c> AddOutputFilterByType BROTLI_COMPRESS text/html text/plain text/xml text/css text/javascript application/javascript </IfModule>123<IfModule mod_brotli.c> AddOutputFilterByType BROTLI_COMPRESS text/html text/plain text/xml text/css text/javascript application/javascript</IfModule>After enabling the brotli compression the Virtual host configuration file looks like below:
<VirtualHost *:80> ServerAdmin webmaster@localhost ServerName example.com DocumentRoot /var/www/ <IfModule mod_brotli.c> AddOutputFilterByType BROTLI_COMPRESS text/html text/plain text/xml text/css text/javascript application/javascript </IfModule> ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined </VirtualHost> 123456789101112<VirtualHost *:80> ServerAdmin webmaster@localhost ServerName example.com DocumentRoot /var/www/ <IfModule mod_brotli.c> AddOutputFilterByType BROTLI_COMPRESS text/html text/plain text/xml text/css text/javascript application/javascript </IfModule> ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined</VirtualHost>Save the configuration file and close it. Then reload the Apache service to apply changes.
sudo systemctl restart apache2
That’s it. You have successfully enabled brotli compression in the Apache server.
Step 3 – Test CompressionAccess your web application in a browser and check headers value in the browser console. You need to search for the Content-Encoding
value. It must contain br
as value, which denotes that the web page is compressed with brotli compression.
The command line heroes can also use curl command to access the header values as below:
curl -I -H 'Accept-Encoding: br' http://example.com
You will see the result below.
HTTP/1.1 200 OK Date: Thu, 01 Jul 2021 06:26:54 GMT Server: Apache/2.4.41 (Ubuntu) Upgrade: h2,h2c Connection: Upgrade Last-Modified: Fri, 05 Feb 2021 08:55:44 GMT ETag: "33-5ba92fc4cecdf-br" Accept-Ranges: bytes Vary: Accept-Encoding Content-Encoding: br Content-Length: 46 Content-Type: text/html
Check for the value of Content-Encoding
option.
This tutorial helped you to configure Brotli compression in the Apache webserver.