How to Enable gzip Compression in Nginx

When you build a website or run a server, you want everything to load fast. One easy way to speed things up is to make files smaller before they’re sent to visitors. That’s where gzip compression comes in.

In this guide, I’ll show you how to enable gzip on Nginx, step-by-step. You don’t need to be an expert, and I’ll keep the terms simple. I’ll also explain what gzip is, how it works, and why I always turn it on for every server I manage.


What Is gzip Compression?

Let’s start with the basics.

gzip is a method used to shrink the size of files. It’s a kind of file compression, just like a .zip file on your computer. When gzip is turned on in your web server, it compresses text-based files—like HTML, CSS, and JavaScript—before sending them to a visitor’s browser.

Smaller files mean faster downloads. And faster downloads mean faster websites.

Here’s a quick example:

  • Without gzip: 100 KB file
  • With gzip: maybe 30 KB

That’s a big difference, especially when you have many visitors or lots of files.


What Is Nginx?

Nginx is a popular web server. People use it to serve websites, handle traffic, and act as a reverse proxy. It’s fast, lightweight, and works well on small or big servers.

I’ve used Nginx on both personal projects and busy online shops. It’s great because it gives you a lot of control—and enabling gzip is one of the things you can easily do to improve performance.


Why Use gzip in Nginx?

From my own experience, enabling gzip:

  • Makes websites load faster
  • Reduces the amount of data sent to visitors
  • Lowers bandwidth usage

If your site feels slow or your visitors are on mobile data, gzip can help a lot.


Check if gzip Is Already Working

Before changing anything, let’s see if gzip is already enabled.

You can do this using a browser tool or a command line tool. My favorite is curl.

Run this in your terminal:

curl -H "Accept-Encoding: gzip" -I http://yourdomain.com

Look at the headers. If you see something like:

Content-Encoding: gzip

That means gzip is working.

If not, don’t worry—we’ll fix it.


How to Enable gzip in Nginx

Let’s get right into it. I’ll walk you through the steps I use.

Step 1: Open the Nginx config file

Most of the time, you can find the main config file here:

sudo nano /etc/nginx/nginx.conf

If it’s not there, try:

sudo nano /etc/nginx/conf.d/default.conf

Or check inside the /etc/nginx/sites-enabled/ folder.

Step 2: Add gzip settings

Find the section that starts with http {. Inside that block, you can add your gzip settings.

Here’s what I usually add:

gzip on;
gzip_disable "msie6";

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
    application/json
    application/javascript
    text/xml
    application/xml
    application/xml+rss
    text/javascript;

Let me break down a few of these:

  • gzip on; – Turns gzip on.
  • gzip_disable "msie6"; – Disables gzip for old browsers that don’t handle it well.
  • gzip_comp_level 6; – Compression level (1 is fastest, 9 is best compression; 6 is a good balance).
  • gzip_types – File types that will be compressed.

Make sure this block is inside the http section, not server or location.

Step 3: Test and restart Nginx

After editing, always test your config:

sudo nginx -t

If everything is okay, restart Nginx:

sudo systemctl restart nginx

Or if your system uses a different command:

sudo service nginx restart

Now gzip should be enabled.


How to Make Sure It’s Working

Once gzip is turned on, test it again with curl:

curl -H "Accept-Encoding: gzip" -I http://yourdomain.com

You should now see Content-Encoding: gzip in the headers.

Or, you can use an online tool like:

These tools show whether your server is compressing files properly.


Which File Types Should Be Compressed?

Not every file needs gzip. Some files are already compressed, like images or videos. You won’t gain much by compressing them again.

Stick to these types:

  • HTML
  • CSS
  • JavaScript
  • JSON
  • XML

I usually skip these:

  • Images (JPG, PNG, GIF, WebP)
  • PDFs
  • Videos and audio files

Three Lists to Help You

Files You Should Compress

  • .html, .htm
  • .css
  • .js
  • .json
  • .xml

Files You Should Not Compress

  • .jpg, .jpeg, .png, .gif, .webp
  • .mp4, .mp3
  • .zip, .tar.gz, .rar
  • .pdf

gzip Settings I Use Most

  • gzip on; – Always needed
  • gzip_comp_level 6; – Good balance between speed and savings
  • gzip_types ...; – Only compress useful text-based files

Does gzip Slow Down Your Server?

Some people ask me if gzip makes the server slower. The short answer: not really.

Yes, it takes a bit of CPU to compress files. But the savings in bandwidth and speed usually make up for it. On a regular server or VPS, you’ll barely notice the difference.

I once tested a small site with and without gzip. The page load time went from 1.2 seconds down to 600 milliseconds. That’s twice as fast.

And if you’re worried, you can lower the compression level to 3 or 4. That makes it faster to compress, with only slightly bigger files.


A Few Tips from My Experience

Here are some extra tips I’ve learned over time:

  • Don’t overdo it. Stick with level 6 or lower unless you need the tiniest files.
  • Test your site after changes to make sure it’s not breaking anything.
  • Check both http and https versions of your site.
  • Use tools like GTmetrix to see how gzip affects speed.
  • Combine gzip with caching (like expires or cache-control) for even better performance.

Final Thoughts

Enabling gzip in Nginx is one of those simple changes that can give big results. It’s free, takes just a few minutes, and can make your website feel much snappier.

I always turn it on when I set up a new server. It’s just one of those good habits that helps everything run better.

So, now that you’ve seen how easy it is, are you going to enable gzip on your Nginx server? If you’re still unsure or want help testing it, I’d be happy to explain further.

Leave a Reply