Compression

Performance

Reducing the size of HTTP response bodies before transmission. Common algorithms: gzip, Brotli, and zstd. Saves bandwidth, speeds up transfers, and reduces CDN egress costs.

Updated Mar 9, 2026

Full Explanation

Compression is free performance. A typical HTML page compresses by 70-80%, JavaScript by 60-70%, and CSS by 80-85%. That's real bandwidth savings on every single request.

How it works: the client sends Accept-Encoding: gzip, br, zstd to tell the server what it supports. The server picks the best algorithm, compresses the response, and sets Content-Encoding: br (or whichever). The client decompresses transparently.

CDNs typically handle compression at the edge, so your origin doesn't need to spend CPU on it. Some CDNs also re-compress—they might fetch gzip from origin and serve Brotli to clients that support it. Important: always set Vary: Accept-Encoding so caches store separate copies per encoding.

Try the interactive Compression Comparison animation in the course to see the real-world difference between gzip, Brotli, and zstd on various content types.

Interactive Animation

Loading animation...

Examples

# Enable compression in Nginx
gzip on;
gzip_types text/html text/css application/javascript application/json;
gzip_min_length 256;
gzip_vary on;  # Adds Vary: Accept-Encoding

# Test compression with curl
$ curl -H 'Accept-Encoding: gzip, br' -sI https://example.com/ | grep -i 'content-encoding\|content-length'
Content-Encoding: br
Content-Length: 4231

# Compare uncompressed vs compressed
$ curl -sI https://example.com/ | grep content-length
Content-Length: 18920  # ~78% reduction with Brotli

Frequently Asked Questions

Reducing the size of HTTP response bodies before transmission. Common algorithms: gzip, Brotli, and zstd. Saves bandwidth, speeds up transfers, and reduces CDN egress costs.

# Enable compression in Nginx
gzip on;
gzip_types text/html text/css application/javascript application/json;
gzip_min_length 256;
gzip_vary on;  # Adds Vary: Accept-Encoding

# Test compression with curl
$ curl -H 'Accept-Encoding: gzip, br' -sI https://example.com/ | grep -i 'content-encoding\|content-length'
Content-Encoding: br
Content-Length: 4231

# Compare uncompressed vs compressed
$ curl -sI https://example.com/ | grep content-length
Content-Length: 18920  # ~78% reduction with Brotli

Related CDN concepts include:

  • Content-Encoding — An HTTP response header indicating the compression algorithm applied to the response body. Common values: …
  • Vary Header — A response header that tells caches which request headers should be included in the cache …
  • Brotli — A compression algorithm developed by Google that typically achieves 15-25% better compression than gzip for …