One of the simplest ways to speed up your website is enabling Gzip compression. Compressed files are smaller and load faster. Nginx includes built-in Gzip support — you just need to turn it on.

This guide shows you how to enable Gzip compression in Nginx on Ubuntu.

What Is Gzip Compression?

Gzip compresses text-based files (HTML, CSS, JavaScript, JSON, XML) before sending them to the browser. The browser decompresses them on arrival. This reduces file sizes by 60-80% and significantly improves load times.

For example, a 200 KB CSS file might be reduced to 40 KB with Gzip. That's a 5x reduction in transfer size.

Step 1: Check Your Nginx Configuration

Open your Nginx configuration file:

sudo nano /etc/nginx/nginx.conf

Look for the gzip directives in the http block.

Step 2: Enable Gzip

Add or uncomment these lines:

gzip on;
gzip_vary on;
gzip_proxied any;
gzip_comp_level 6;
gzip_types text/plain text/css text/xml text/javascript application/json application/javascript application/xml+rss application/rss+xml image/svg+xml;

Explanation:

  • gzip on – Enables Gzip compression
  • gzip_vary on – Adds Vary header for proxy caching
  • gzip_proxied any – Compresses responses from proxy servers
  • gzip_comp_level 6 – Balances compression ratio and CPU usage (1-9, 6 is recommended)
  • gzip_types – Specifies which file types to compress

Step 3: Test and Reload Nginx

sudo nginx -t
sudo systemctl reload nginx

Step 4: Verify Compression Is Working

Use curl to check if compression is enabled:

curl -H "Accept-Encoding: gzip" -I https://yourdomain.com/style.css

Look for Content-Encoding: gzip in the response headers.

You can also use browser developer tools (F12) → Network tab. Check the response headers for any CSS or JS file — you should see Content-Encoding: gzip.

Step 5: What Not to Compress

Don't waste CPU cycles compressing already-compressed files:

  • Images (JPEG, PNG, WebP)
  • Videos (MP4, WebM)
  • Fonts (WOFF, WOFF2)
  • PDFs

These file types are already compressed. Applying Gzip to them won't reduce size and will waste CPU resources.

Troubleshooting

Gzip not working – Check that the gzip_types list includes your file types. Some Nginx versions require explicit listing.

CPU usage increase – Lower gzip_comp_level to 4 or 5 if you notice performance impact.

Gzip compression is one of the easiest performance optimizations you can make. It takes five minutes to set up and can cut your page load time by 30-50%.

Need a VPS to try this on? Check our recommended VPS providers.