WEB SERVER
June 23, 2026

How to Enable Gzip and Brotli Compression in Nginx

7 min read
Author
CloudStick Team
Backend Developer
Share this article
Gzip and Brotli Compression in Nginx
CloudStick
Gzip + Brotli in Nginx

Why Compression Cuts Load Times Dramatically

Text-based assets — HTML, CSS, JavaScript, JSON, XML — compress extremely well. A typical uncompressed 100 KB JavaScript file compresses to 25-35 KB with Gzip (65-75% reduction) and 20-28 KB with Brotli (72-80% reduction). For a WordPress page loading 400-500 KB of JS and CSS, compression routinely saves 250-350 KB per page view — reducing Time to First Byte and reducing bandwidth costs simultaneously.

Gzip is supported by every browser and every CDN. Brotli is supported by all modern browsers and achieves better compression ratios — particularly for HTML and JavaScript. The practical approach is to enable both: Nginx serves Brotli to browsers that support it (sent via the Accept-Encoding: br header) and falls back to Gzip for older clients.

Enable Gzip in Nginx

Gzip is built into Nginx — no module installation needed. Add the following to the http block in /etc/nginx/nginx.conf. Setting gzip_comp_level 5 is the sweet spot — higher levels use significantly more CPU for minimal additional compression.

# Add to http {} block in /etc/nginx/nginx.conf
gzip on;
gzip_vary on;
gzip_proxied any;
gzip_comp_level 5;
gzip_min_length 256;
gzip_types
text/plain
text/css
text/xml
text/javascript
application/json
application/javascript
application/x-javascript
application/xml
application/xml+rss
image/svg+xml;

Install the Brotli Module

Brotli is not included in the default Nginx package. On Ubuntu 24.04, the libnginx-mod-http-brotli-filter and libnginx-mod-http-brotli-static packages are available in the repositories alongside Nginx.

# Install Brotli module packages
sudo apt install libnginx-mod-http-brotli-filter libnginx-mod-http-brotli-static -y
# Verify modules are loaded
nginx -V 2>&1 | grep brotli
PREREQUISITE

The Brotli module packages are only available if you installed Nginx from Ubuntu's default repositories (not from nginx.org's PPA). If you installed from the official Nginx PPA, you will need to compile the Brotli module from source using ngx_brotli from Google's GitHub repository.

Configure Brotli Compression Levels

Once installed, enable Brotli in the same http block alongside your Gzip settings. Brotli levels run from 0-11; level 4 gives excellent compression with low CPU overhead. Level 11 achieves maximum compression but is too slow for on-the-fly dynamic content — use it only for static file pre-compression.

# Add to http {} block alongside Gzip settings
brotli on;
brotli_comp_level 4;
brotli_static on;
brotli_types
text/plain
text/css
text/xml
text/javascript
application/json
application/javascript
application/xml
image/svg+xml;
# Reload after changes
sudo nginx -t && sudo systemctl reload nginx

Verify Compression Is Working

Test with curl by sending the Accept-Encoding header that browsers send. The response should include Content-Encoding: br for Brotli or Content-Encoding: gzip for Gzip.

# Test Brotli compression
curl -H 'Accept-Encoding: br' -I https://example.com/
# Look for: Content-Encoding: br
# Test Gzip compression
curl -H 'Accept-Encoding: gzip' -I https://example.com/
# Look for: Content-Encoding: gzip

CloudStick Enables Compression by Default

When you provision a server through CloudStick, Nginx is configured with Gzip compression enabled from the start. The default config applies compression to all appropriate MIME types and uses the level 5 setting that balances CPU overhead against compression ratio. You do not need to manually configure these settings — they are part of CloudStick's baseline server configuration.

For Brotli, you can add the packages and configuration above to any CloudStick-managed server — the agent does not prevent manual Nginx config additions. Just ensure your additions go in the correct block context and validate with nginx -t before reloading.

Leave a comment
Full Name
Email Address
Message
Contents

We use cookies to improve your experience

CloudStick uses cookies to personalise content, analyse traffic and keep you signed in. Cookie Policy · Terms of Service

Manage cookies