Enable Gzip and Brotli Compression on Nginx/Apache

Enable Gzip and Brotli Compression on Nginx/Apache

June 2026 · 6 min read

Compression is one of the easiest and most effective optimization techniques. HTML, CSS, and JavaScript files compressed with Gzip reduce file size by 60-80%, while Brotli (Google's algorithm) achieves 15-20% better compression than Gzip. You can enable both on Nginx/Apache in just 5 minutes.

Enable Gzip on Nginx

sudo nano /etc/nginx/nginx.conf
http {
    # Gzip Settings
    gzip on;
    gzip_vary on;
    gzip_proxied any;
    gzip_comp_level 6;
    gzip_min_length 1024;
    gzip_buffers 16 8k;
    gzip_http_version 1.1;
    gzip_types
        text/plain
        text/css
        text/javascript
        application/javascript
        application/json
        application/xml
        application/rss+xml
        font/woff2
        image/svg+xml;
}
sudo nginx -t && sudo systemctl reload nginx

Enable Brotli on Nginx

On Ubuntu 20.04 and newer, install the Brotli module from the repository:

sudo apt install libnginx-mod-http-brotli -y

sudo nano /etc/nginx/nginx.conf
http {
    # Brotli (add after gzip config)
    brotli on;
    brotli_comp_level 6;
    brotli_static on;
    brotli_types
        text/plain
        text/css
        application/javascript
        application/json
        font/woff2
        image/svg+xml;
}

Enable Gzip on Apache

sudo a2enmod deflate
sudo nano /etc/apache2/mods-enabled/deflate.conf
<IfModule mod_deflate.c>
    AddOutputFilterByType DEFLATE text/html text/plain text/css
    AddOutputFilterByType DEFLATE text/javascript application/javascript
    AddOutputFilterByType DEFLATE application/json application/xml
    AddOutputFilterByType DEFLATE image/svg+xml font/woff2

    # Don't compress images and already-compressed files
    SetEnvIfNoCase Request_URI \
        \.(?:gif|jpe?g|png|webp|gz|zip|bz2)$ no-gzip

    DeflateCompressionLevel 6
</IfModule>
sudo systemctl reload apache2

Compression can boost web speed as much as a hardware upgrade. For VPS that need high performance from day one, AsiaGB's VPS service offers NVMe SSD options in both Thailand and Singapore, perfect for production web servers. You can verify compression is working using dnsxray.com, which reports HTTP headers including Content-Encoding to confirm compression is active.


Verify Compression is Working Correctly

# Check Gzip response header
curl -sI -H "Accept-Encoding: gzip" https://example.com | grep "Content-Encoding"
# Should return: Content-Encoding: gzip

# Check Brotli
curl -sI -H "Accept-Encoding: br" https://example.com | grep "Content-Encoding"
# Should return: Content-Encoding: br

# Check actual file size
curl -s -H "Accept-Encoding: gzip" https://example.com | wc -c  # compressed size
curl -s https://example.com | wc -c  # normal size

# Test with GTmetrix or PageSpeed Insights
Tip: The gzip_vary on directive in Nginx is critical — it sends the Vary: Accept-Encoding header to tell CDNs and proxies that responses differ based on Accept-Encoding, preventing CDNs from serving compressed content to clients that don't support it.

Common Issues & Troubleshooting

curl shows no Content-Encoding: gzip even after enabling compression

You must send the Accept-Encoding header to receive compressed responses:

# Must include -H "Accept-Encoding: gzip"
curl -sI -H "Accept-Encoding: gzip,deflate,br" https://example.com | grep -i encoding

Images (JPG/PNG) are not being compressed

JPG/PNG are already binary-compressed formats. Compressing them again yields minimal or larger file sizes. Typically you should not include images in gzip_types at all. Use WebP instead if you need to reduce image sizes.

Brotli module fails to load on Ubuntu

Check your Nginx version and module compatibility:

# Check if module exists
ls /etc/nginx/modules-enabled/ | grep brotli

# If not found, reinstall
sudo apt install nginx-extras -y  # nginx-extras includes brotli

# Or compile Nginx with brotli module manually

Website is barely faster even with compression enabled

Verify that large files (JS/CSS) are actually being compressed. If a CDN is in front of your server, it may serve cached uncompressed versions:

# Check specific file
curl -sI -H "Accept-Encoding: gzip" https://example.com/main.js | grep encoding

# Purge CDN cache and test again

Frequently Asked Questions

How is Brotli better than Gzip?

Brotli achieves about 15-25% better compression than Gzip for text files, but uses more CPU during compression. Both decompress at similar speeds. Brotli only works over HTTPS (browsers enforce this), while Gzip works over HTTP as well.

What's the difference between compression levels 1-9?

Level 1 = fastest, least compression; Level 9 = slowest, most compression. For web servers, level 6 is the sweet spot—it achieves 95% of level 9's compression ratio while using significantly less CPU. Level 9 is not recommended on production because CPU overhead outweighs the benefit.

Does compression work with WebSocket?

Gzip/Brotli work on standard HTTP response headers. WebSocket doesn't use the original HTTP response—it requires WebSocket per-message compression (permessage-deflate) instead, which must be configured at the application level.