How do I Enable Compression in Brotli?
The most common way to enable Brotli compression is by using a Content Delivery Network (CDN) that already offers Brotli Compression as a feature.
A few such CDN providers include:
However, if you want to enable Brotli compression in a different environment (one that does not use a CDN), the process can be a lot more tricky, especially since it’s not the most common web compression technique.
{{cool-component}}
Who Can Experience the Brotli Boost?
Before enabling Brotli, it's wise to make sure most of your visitors can reap the benefits of its speed. The good news is that Brotli enjoys widespread support across modern browsers.
Brotli browser support is usually prevalent in the following:
- Chrome
- Firefox
- Safari
- Edge
You can check a website like caniuse.com to see the latest browser compatibility for Brotli. If you're targeting a niche audience using older browsers, you might want to consider offering both Brotli compressed content and a non-compressed version as a fallback.
Enabling Brotli
There are a couple of ways to enable Brotli compression, depending on how your website is set up. Here's a breakdown of the two most common scenarios:
1. Content Delivery Network (CDN)
A CDN is your network of warehouses strategically scattered around the world, each storing copies of your website's content. Many CDNs, like Cloudflare, offer Brotli compression as a built-in feature.
You can typically enable it through your CDN's control panel with a few clicks, saving you from tinkering with server configurations.
This is a great option for beginners or those who don't want to mess with server settings.
2. Gearing Up Your Web Server
If you're not using a CDN, you might be able to enable Brotli directly on your web server. The specific steps will depend on the type of server software you're using (e.g., Apache, Nginx).
In many cases, it involves editing configuration files and restarting your server. This approach requires a bit more technical know-how, so don't hesitate to seek help from your web hosting provider if needed.
There are often plenty of tutorials and guides available online as well, but be sure they are compatible with your specific server software version.
Brotli Compression Methodology
It's important to have a fallback for browsers that do not support Brotli. Ensure your server configuration includes conditions to serve non-Brotli compressed content to such browsers.
This often involves server-side checks of the Accept-Encoding header in HTTP requests.
Once done, follow the steps below:
For Apache Servers
Installing mod_brotli: If you're running an Apache server, you'll need to ensure that mod_brotli is installed and enabled. This module provides Brotli compression capabilities.
- Check if mod_brotli is already installed by running apachectl -M | grep brotli in your terminal. If you see brotli_module (shared), then it's installed.
- If it's not installed, you can typically install it via your package manager. For example, on Ubuntu, you would use sudo apt-get install brotli.
- Once installed, you need to enable it. You can do this by adding LoadModule brotli_module modules/mod_brotli.so to your Apache configuration file.
For Nginx Servers
Compiling Nginx from Source: Nginx doesn't include Brotli support out of the box. To use Brotli with Nginx, you'll likely need to compile Nginx from source with the Brotli module.
- Clone the Nginx source code and the Brotli module from their respective repositories.
- When configuring the Nginx build, include the path to the Brotli module with --add-module=/path/to/ngx_brotli.
- Continue with the make and make install commands to compile and install Nginx.
Configuring Your Server for Brotli Compression
- Apache: After enabling mod_brotli, you can configure it in your .htaccess file or directly within your site's configuration files. Use directives like AddOutputFilterByType BROTLI_COMPRESS text/html text/css application/javascript to specify which content types to compress.
- Nginx: With Nginx, after compiling with the Brotli module, you'll need to add configuration directives to your site's server block. This involves setting brotli on; and specifying which types of files to compress using brotli_types text/plain text/css application/json application/javascript.
Testing Brotli Compression
Once Brotli is enabled, you should test to ensure it's working correctly.
You can use tools like Google Chrome's Developer Tools or online services to check the Content-Encoding header for responses from your server.
If it shows br (compress text online format), Brotli compression is active and functioning.
Note: Brotli is a bit stronger than simple compression, since in compression vs encoding, the latter is usually a complete format, while the former focuses on lessening the numbers and symbols.
Help is a Click Away
If you're unsure about enabling Brotli on your specific setup, don't fret! Most web hosting providers offer comprehensive documentation or have a supportive customer service team.
Their team can guide you through the process for your particular server environment, ensuring a smooth Brotli implementation.