Improving web performance is essential in today's landscape, where page speed directly affects user retention and SEO. One modern approach to delivering smaller payloads is Brotli compression—a Google-developed algorithm that outperforms gzip in many scenarios. In this article, we’ll explore how to configure Brotli compression on an NGINX server for real-time web asset delivery.
Why Brotli?
Brotli provides better compression ratios than gzip, particularly for text-based assets like HTML, CSS, and JavaScript. It's supported by all major browsers and is ideal for reducing bandwidth usage and load times.
Prerequisites
- NGINX (preferably compiled with Brotli support or use a dynamic module)
- Root or sudo access to the server
Step 1: Install Brotli on Ubuntu/Debian
sudo apt update
sudo apt install brotli
If your NGINX isn’t compiled with Brotli, use a dynamic module:
sudo apt install nginx-module-brotli
Step 2: Enable Brotli in NGINX
Modify your NGINX config (e.g., /etc/nginx/nginx.conf
) and add the following lines inside the http
block:
load_module modules/ngx_http_brotli_filter_module.so;
load_module modules/ngx_http_brotli_static_module.so;
brotli on;
brotli_comp_level 6;
brotli_types text/plain text/css application/javascript application/json image/svg+xml application/xml+rss;
Step 3: Test the Configuration
sudo nginx -t
sudo systemctl restart nginx
You can now check your server’s response headers using curl
or your browser's dev tools:
curl -H "Accept-Encoding: br" -I https://yourdomain.com
If successful, you’ll see:
Content-Encoding: br
Step 4: Combine with Static Precompression (Optional)
You can pre-compress assets during deployment using Brotli CLI:
brotli -Z -o app.js.br app.js
Then ensure NGINX is configured to serve .br
files with:
brotli_static on;
Performance Tips
- Use Brotli for text assets only — images and videos should be compressed with other tools (like WebP, AVIF, etc.)
- Set compression level to 4-6 for a good balance between speed and ratio
- Always test Brotli compatibility with CDNs and reverse proxies
Conclusion
By enabling Brotli compression with NGINX, you can significantly enhance the performance of your site with minimal configuration. It’s an excellent upgrade for modern web servers and one of the simplest ways to optimize page delivery.
Enjoyed this post? Feel free to support my work: buymeacoffee.com/hexshift