Compressing text-based responses (HTML, CSS, JavaScript) before sending them over the network significantly reduces page load time, especially on slower connections. This guide covers configuring both gzip and the more modern Brotli.
Why Compression Matters
Text content compresses very well — often 70-90% size reduction — directly translating to faster downloads, especially impactful for visitors on slower or higher-latency connections.
gzip: The Widely-Supported Standard
http {
gzip on;
gzip_types text/plain text/css application/json application/javascript text/xml application/xml text/javascript;
gzip_min_length 1024;
gzip_comp_level 6;
}
gzip_min_length avoids compressing very small responses where the overhead isn't worthwhile; gzip_comp_level (1-9) trades CPU usage for compression ratio — 6 is a reasonable balance for most use cases.
Brotli: Better Compression, Growing Support
Brotli generally achieves better compression ratios than gzip for the same content, at the cost of needing an additional Nginx module not included by default.
Installing the Brotli Module
sudo apt install nginx-module-brotli -y
sudo nano /etc/nginx/nginx.conf
load_module modules/ngx_http_brotli_filter_module.so;
load_module modules/ngx_http_brotli_static_module.so;
Configuring Brotli
http {
brotli on;
brotli_types text/plain text/css application/json application/javascript text/xml application/xml text/javascript;
brotli_comp_level 6;
}
Serving Both: Let the Browser Choose
Modern browsers automatically request their preferred compression method via the Accept-Encoding header — with both gzip and Brotli configured, Nginx serves whichever the specific browser supports and prefers, with Brotli typically preferred when both are available.
Verifying Compression Is Working
curl -H "Accept-Encoding: gzip" -I https://yourdomain.com
Look for Content-Encoding: gzip in the response headers — its absence means compression isn't being applied.
What NOT to Compress
Already-compressed formats (JPEG, PNG, MP4, ZIP) gain little to nothing from additional compression and waste CPU attempting it — ensure your gzip_types/brotli_types lists include only genuinely compressible text-based content types.
Pre-Compressing Static Assets (Avoiding Runtime CPU Cost)
gzip_static on;
brotli_static on;
Serves pre-compressed .gz/.br files (generated at build time) directly, rather than compressing on every request — eliminates the runtime CPU cost of compression for static assets, particularly valuable for high-traffic sites.
Measuring the Impact
Compare page load times and transfer sizes before/after enabling compression using your browser's developer tools network tab, or a dedicated page speed testing tool.
Common Errors
Compression configured but Content-Encoding header absent — verify the response's content type is actually included in your gzip_types/brotli_types list, and that gzip_min_length isn't excluding the response due to small size.
Continue Reading
- Nginx Performance Tuning: Worker Processes, Caching & Gzip
- How to Optimize Images for Web Performance
- How to Perform a Complete Website Speed Audit
Browse more articles in Web Servers.