Brotli is the latest open-source compression algorithm developed by Google. It offers superior compression efficiency compared to Gzip, resulting in faster page load times and reduced bandwidth usage.
In this guide, you will learn how to enable and configure Brotli compression in Nginx to optimise website performance and enhance user experience.
Table of Contents:
Check if Brotli Module is Installed
- To check if Brotli support is included, run the command below:
nginx -V 2>&1 | grep brotli
- If the command returns ngx_brotli, Brotli is already installed. Otherwise, proceed to the next step.
Install Brotli Module
For Ubuntu/Debian Servers
- Install required dependencies:
sudo apt update sudo apt install -y git build-essential zlib1g-dev libpcre3 libpcre3-dev unzip uuid-dev
- Clone the Brotli module and initialise submodules:
cd /usr/local/src git clone https://github.com/google/ngx_brotli.git cd ngx_brotli git submodule update --init
- Recompile Nginx with Brotli support:
cd /usr/local/src/nginx ./configure --add-module=/usr/local/src/ngx_brotli make sudo make install
Tip: On Ubuntu, you can alternatively install nginx-extras, which includes Brotli support by default.
Enable Brotli in Nginx Configuration
- Open your Nginx configuration file:
sudo nano /etc/nginx/nginx.conf
- Inside the http { … } block, add:
brotli on; brotli_comp_level 6; brotli_types text/plain text/css application/javascript application/json image/svg+xml application/xml+rss;
- (Optional) Keep Gzip enabled for compatibility:
gzip on; gzip_types text/plain text/css application/javascript application/json image/svg+xml application/xml+rss;
Test and Reload Nginx
- Check for syntax errors:
sudo nginx -t
- If no errors appear, reload Nginx:
sudo systemctl reload nginx
Verify Brotli Compression
- To confirm Brotli is working, run:
curl -H ‘Accept-Encoding: br’ -I https://yourdomain.com
- If Brotli is enabled, you will see this in the output:
content-encoding: br
Conclusion
This way, you can successfully enable Brotli compression in Nginx. Regularly monitor compression performance and keep Nginx updated to maintain optimal results.
If you require additional assistance, feel free to contact our support staff.
Want to set up NGINX on your server? Check out our guide on How to install NGINX on cPanel