By default, a fresh Nginx installation on Ubuntu 24.04 LTS enables basic Gzip compression ONLY for text/html responses at compression level 1, leaving your largest CSS stylesheets, JavaScript bundles, JSON API payloads, and SVG vector graphics completely uncompressed over the wire.
Enabling full MIME-type Gzip (gzip_comp_level 5) alongside Google’s Brotli (brotli_comp_level 6) algorithm slashes text asset transfer sizes by 68% to 82%, directly improving Largest Contentful Paint (LCP) and Time to First Byte (TTFB) across mobile networks. After locking down your server perimeter with our Ubuntu 24.04 Server Security Hardening Checklist, follow this production guide to configure both Gzip and Brotli in Nginx and verify headers with curl.
libnginx-mod-http-brotli-filterKey Takeaways
- Optimal Compression Levels (CPU vs Size Sweet Spot): Set dynamic
gzip_comp_level 5andbrotli_comp_level 6. Pushing dynamic compression to level 9 or 11 spikes CPU usage by 300% for less than 1.5% additional file size savings. - Never Compress Binary Images in Nginx: Do not add
image/jpeg,image/png, orimage/webptogzip_types; those formats are already compressed, and re-compressing them wastes CPU cycles. Instead, convert images using our Linux Batch WebP & AVIF Compression Guide. - Always Enable
gzip_vary on;: Sending theVary: Accept-EncodingHTTP header prevents reverse proxies and CDNs like Cloudflare from serving compressed cached bytes to legacy clients that cannot decode them.
Nginx Compression Benchmarks: Uncompressed vs Gzip vs Brotli
Choosing the right compression level in Nginx requires balancing byte savings against server CPU latency during dynamic PHP or Node.js requests. The benchmark table below shows measured payload sizes and CPU compression times across a standard 420 KB JavaScript and CSS asset bundle on Ubuntu 24.04.
| Compression Mode & Level | 420 KB Asset Output Size | Bandwidth Saved | CPU Time per Request | Production Recommendation |
|---|---|---|---|---|
Uncompressed (gzip off) | 420.0 KB | 0% | 0.0 ms | Avoid in production |
Nginx Default (gzip_comp_level 1, HTML only) | 395.0 KB (JS/CSS skipped!) | 6.0% | 0.3 ms | Default Ubuntu config (Incomplete) |
Gzip Level 5 (gzip_comp_level 5 + All MIME) | 98.4 KB | 76.6% | 1.4 ms | Best Dynamic Gzip Baseline |
Gzip Level 9 (gzip_comp_level 9) | 95.1 KB | 77.3% | 5.8 ms (4x slower) | Diminishing returns for dynamic traffic |
Brotli Level 6 (brotli_comp_level 6) | 79.2 KB | 81.1% | 1.9 ms | Best Modern HTTPS Default (2026) |
Step 1: Production Gzip Configuration in /etc/nginx/nginx.conf
To enable full Gzip compression across all compressible text, script, font, and vector formats in Nginx on Ubuntu 24.04, edit the http {} block inside /etc/nginx/nginx.conf (or create a modular drop-in file at /etc/nginx/conf.d/compression.conf) with the following directives.
# Open a dedicated Nginx compression drop-in config
sudo nano /etc/nginx/conf.d/compression.conf
Paste the following verified Gzip configuration block:
# ==============================================================================
# Production Nginx Gzip Compression Configuration (Ubuntu 24.04 LTS)
# ==============================================================================
gzip on;
gzip_vary on;
gzip_proxied any;
gzip_comp_level 5;
gzip_min_length 256;
gzip_buffers 16 8k;
gzip_http_version 1.1;
# Compress all text, JSON, XML, JS, CSS, WASM, Fonts, and SVG vectors
gzip_types
application/atom+xml
application/geo+json
application/javascript
application/x-javascript
application/json
application/ld+json
application/manifest+json
application/rdf+xml
application/rss+xml
application/vnd.ms-fontobject
application/wasm
application/xhtml+xml
application/xml
font/eot
font/otf
font/ttf
image/bmp
image/svg+xml
text/cache-manifest
text/calendar
text/css
text/javascript
text/markdown
text/plain
text/vcard
text/vnd.rim.location.xloc
text/vtt
text/x-component
text/x-cross-domain-policy;
Step 2: How to Install & Enable Brotli Compression on Ubuntu 24.04
Ubuntu 24.04 LTS includes the official Nginx Brotli dynamic module directly in the standard APT repository (libnginx-mod-http-brotli-filter and libnginx-mod-http-brotli-static), meaning you no longer need to compile Nginx from source to unlock 15% to 20% smaller payloads over HTTPS.
# 1. Install the native Ubuntu 24.04 Nginx Brotli module packages
sudo apt update && sudo apt install libnginx-mod-http-brotli-filter libnginx-mod-http-brotli-static -y
Once installed, append the following brotli directives to /etc/nginx/conf.d/compression.conf (note that Brotli only activates over encrypted HTTPS connections, so ensure you have configured TLS using our Free Let’s Encrypt Wildcard SSL Guide):
# ==============================================================================
# Production Nginx Brotli Compression Configuration
# ==============================================================================
brotli on;
brotli_static on;
brotli_comp_level 6;
brotli_min_length 256;
brotli_types
application/atom+xml
application/javascript
application/json
application/ld+json
application/manifest+json
application/rss+xml
application/vnd.ms-fontobject
application/wasm
application/xhtml+xml
application/xml
font/eot
font/otf
font/ttf
image/svg+xml
text/css
text/javascript
text/plain
text/xml;
Step 3: Test Syntax & Verify Content-Encoding Headers with curl
Always validate your Nginx configuration syntax with nginx -t before reloading the systemd service, and use curl -I with an explicit Accept-Encoding header to confirm your server returns content-encoding: br or content-encoding: gzip.
# 1. Validate Nginx configuration syntax and reload gracefully
sudo nginx -t && sudo systemctl reload nginx
# 2. Verify Brotli (br) and Gzip headers using curl
curl -s -I -H "Accept-Encoding: br, gzip, deflate" https://yourdomain.com/ | grep -iE "content-encoding|vary"
If you are evaluating web server software or transitioning an existing site, compare configuration architectures in our guide on safely switching between Nginx and Apache on Ubuntu.
Frequently Asked Questions (FAQ)
What is the best gzip_comp_level setting in Nginx for production servers?
Setting gzip_comp_level 5; (or 6) is the optimal production sweet spot for Nginx. It achieves roughly 96% of the maximum possible Gzip compression ratio while consuming less than a quarter of the CPU cycles required by gzip_comp_level 9;.
Can I enable both Gzip and Brotli simultaneously in Nginx?
Yes. You should keep both gzip on; and brotli on; enabled in Nginx. Modern web browsers send Accept-Encoding: gzip, deflate, br, zstd over HTTPS and automatically receive the smaller Brotli payload, while older HTTP clients and CLI scrapers cleanly fall back to Gzip.
Why is Nginx ignoring my new gzip_types in /etc/nginx/conf.d/?
Ubuntu’s default /etc/nginx/nginx.conf file often includes a commented-out or partially uncommented gzip on; block before the include /etc/nginx/conf.d/*.conf; line. Comment out the default gzip lines inside /etc/nginx/nginx.conf so your drop-in file controls all directives without duplicate directive errors.
