What I wanted to do

My site served through nginx was slow to load. I heard that enabling gzip compression reduces file sizes and speeds things up, so I gave it a try.

What is gzip compression?

It’s a mechanism that compresses text-based files (HTML, CSS, JS, JSON, etc.) before transferring them. The browser automatically decompresses them, so there’s no impact on the user experience. Depending on the file type, JS and CSS can often be reduced by 70–80%.

Edit the configuration file

sudo nano /etc/nginx/nginx.conf

Add the following inside the http block.

http {
    # gzip settings
    gzip on;
    gzip_vary on;
    gzip_proxied any;
    gzip_comp_level 6;
    gzip_buffers 16 8k;
    gzip_http_version 1.1;
    gzip_types
        text/plain
        text/css
        text/xml
        application/json
        application/javascript
        application/xml
        application/xml+rss
        text/javascript
        image/svg+xml;
}

What each setting means

SettingDescription
gzip onEnables gzip compression
gzip_vary onAdds Vary: Accept-Encoding header (for CDN compatibility)
gzip_proxied anyCompresses responses for proxied requests
gzip_comp_level 6Compression level (1–9; 6 is a good balance)
gzip_typesMIME types to compress (images are generally excluded)

Apply and verify the settings

sudo nginx -t
sudo systemctl reload nginx

Check if compression is working

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

If Content-Encoding: gzip appears in the response headers, it’s working.

HTTP/2 200
content-type: text/html
content-encoding: gzip

You can also verify in Chrome DevTools: Network tab → select a file → check Response Headers for content-encoding: gzip.

Per-site configuration

You can also add the settings inside the server block in individual files under /etc/nginx/sites-available/.

server {
    listen 80;
    server_name example.com;

    gzip on;
    gzip_types text/css application/javascript;

    location / {
        root /var/www/html;
    }
}

Gotchas

  • You don’t need to add text/html to gzip_types — it’s compressed automatically when gzip on is set
  • Images (jpg/png/webp) are already compressed, so adding them to gzip_types has no effect
  • Setting gzip_comp_level to 9 increases compression but also CPU load — 6 is enough
  • If your site is behind Cloudflare, Cloudflare also compresses responses, which can override nginx’s settings
  • Forgetting gzip_vary on can cause CDNs to confuse gzip and non-gzip cached versions

Looking for reliable cloud infrastructure? Check out these developer-friendly services.