How to Set Up gzip and Brotli Compression for Faster Page Loads

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

Browse more articles in Web Servers.

  • gzip compression nginx, brotli compression, web performance compression, nginx gzip static
  • 0 用戶發現這個有用
這篇文章有幫助嗎?

相關文章

How to Install Nginx on Ubuntu & Debian (Complete Guide)

Nginx is one of the world's most widely used web servers, known for its speed, low resource...

How to Install Apache on Ubuntu & Debian (Complete Guide)

Apache HTTP Server is one of the most widely used web servers, valued for its stability, flexible...

Nginx Virtual Hosts (Server Blocks): Hosting Multiple Websites on One VPS

Nginx "server blocks" (the equivalent of Apache's virtual hosts) let you host multiple...

Apache Virtual Hosts: Hosting Multiple Websites on One VPS

Apache Virtual Hosts allow a single server to host multiple independent websites, each identified...

How to Configure Nginx as a Reverse Proxy for Node.js/Docker Apps

A reverse proxy sits in front of your application, handling incoming traffic on standard ports...