Beginner Fundamentals

Gzip Compression

Compression reduces the size of responses before they travel to the browser. Smaller responses mean faster page loads and less bandwidth. Nginx supports gzip out of the box.

Turning Gzip On

Enable it in the http context so it applies everywhere:

http {
    gzip on;
}

The browser advertises support with the Accept-Encoding header, and Nginx compresses only when the browser can decode it.

Choosing What to Compress

By default Nginx only compresses HTML. Add other text-based types with gzip_types:

gzip on;
gzip_types text/plain text/css application/json
           application/javascript text/xml application/xml;

Do not compress images, video, or archives. They are already compressed, so gzip just wastes CPU.

Tuning Options

gzip_comp_level 5;
gzip_min_length 256;
gzip_vary on;
  • gzip_comp_level sets effort from 1 (fast) to 9 (smallest). Around 5 is a good balance.
  • gzip_min_length skips tiny files where compression is not worth it.
  • gzip_vary on adds a Vary: Accept-Encoding header so caches store both versions.

Verifying It Works

Check the response headers with curl:

curl -H "Accept-Encoding: gzip" -I https://example.com

Look for Content-Encoding: gzip in the output to confirm compression is active.