What is Brotli?
Brotli is a compression algorithm developed by Google that achieves 15-25% better compression ratios than Gzip. All modern browsers support it.
Install Brotli Module for Nginx
# On Ubuntu with nginx-extras
sudo apt install -y libnginx-mod-brotliConfigure Nginx
# In http block of nginx.conf
brotli on;
brotli_comp_level 6;
brotli_types
text/plain
text/css
text/javascript
application/json
application/javascript
application/xml
image/svg+xml
application/wasm;Keep Gzip as Fallback
# Both can be enabled simultaneously
gzip on;
gzip_types text/plain text/css application/json application/javascript;
brotli on;
brotli_types text/plain text/css application/json application/javascript;Browsers that support Brotli send Accept-Encoding: br in their request. Nginx serves Brotli to those clients and Gzip to older clients.
Verify
# Check for Brotli response
curl -H "Accept-Encoding: br" -I https://example.com
# Look for: Content-Encoding: brPre-Compressed Files
# Generate .br files for static assets
brotli -Z style.css # Creates style.css.br
# Nginx serves pre-compressed files automatically
brotli_static on;