How to Enable NGINX Gzip Compression

Last updated on August 1st, 2024 at 05:08 am

Every site administrator and web developer wants to make their website run faster. One of the most powerful ways to do this is to compress the files and data sent from the web server to client browser. GZIP compression allows NGINX server to compress data before sending it to client browser. This reduces data bandwidth, improves website speed and saves server costs. In this article, we will learn how to enable NGINX GZip compression.

What is GZIP Compression in NGINX

By default, when a web browser sends a request to web server, it returns all the response data as it is. Since today’s web pages are content heavy and require many files (images, css, js, etc.), this can slow down your website. When you enable GZIP compression on your web server, then it will automatically compress all the response data sent to client browser, using GZIP compression algorithm. It will also set the ‘content-encoding’ response header to ‘gzip’. When the web browser receives the response data, it will see this response header and automatically decompress the data using the same GZIP algorithm, before rendering it for the end user.

Why Enable GZIP Compression

If there are too many files requested on a web page or a large amount of data is to be transferred, this can slow down your website and also increase network bandwidth costs. Therefore, it is practical to compress the response data sent by the server as much as possible. This will load your web pages faster and reduce data transfer costs. There are several data compression algorithms for this purpose. Among them, GZIP is one of the most popular. GZIP compression is supported by most web servers and browsers. So it is easy to switch it on your website.

How to Enable NGINX Gzip Compression

Here are the steps to enable NGINX GZip compression.

1. Open NGINX Configuration file

You can enable GZIP compression via main config file or virtual host file, depending on your setup. Open terminal and run the following command to open NGINX server configuration file.

$ sudo vi /etc/nginx/nginx.conf

If you have configured separate virtual hosts for your website (e.g www.example.com), such as /etc/nginx/sites-enabled/website.conf then open its configuration with the following command

$ sudo vi /etc/nginx/sites-enabled/website.conf

Bonus Read : How to Enable Browser Caching in NGINX

2. Enable GZIP Compression in NGINX

Add/Uncomment the following lines in your NGINX configuration file, or add them to http or server block, as per your requirement.

server{
gzip on;
gzip_vary on;
gzip_min_length 1024;
gzip_proxied expired no-cache no-store private auth;
gzip_types text/plain text/css text/xml text/javascript application/x-javascript application/xml;
gzip_disable "MSIE [1-6]\.";
}

Let us look at each of the above lines

  • gzip on – turns on gzip compression
  • gzip_vary on – enable gzip for both gzipped and regular versions of a file
  • gzip_min_length – compress files that are larger than 1024 bytes (1kb). Small files cannot be compressed much.
  • gzip_proxied – compress data even for users connected via proxies
  • gzip_types – specifies the file types to be compressed
  • gzip_disable – disable gzip for IE browser versions 1-6, since they do not support GZIP compression

Bonus Read : How to Remove Trailing Slash in NGINX

Please note, using the gzip_types server directive you can easily control the file types to be compressed.

The above configuration ‘gzip on’ directive on entire website. If you want to exclude GZIP compression for certain URLs, you can add ‘gunzip on’ directive to their location blocks. Here is an example to exclude /files/ from data compression.

location /files/ {
gunzip on;
...
}

Also, in the above case, NGINX will compress all your responses on the fly. If you already have compressed data that you want to send, instead of compressing data on the fly, use gzip_static directive for the specific file location block, instead of using ‘gzip on’ directive.

location / {
gzip_static on;
}

In the above example, when a user requests /file.txt, NGINX will look for /file.txt.gz and directly send it instead of compressing it and sending it. If the file is not found, then it will send the uncompressed file.

3. Restart NGINX Server

Run the following command to check syntax of your updated config file.

$ sudo nginx -t

If there are no errors, run the following command to restart NGINX server.

$ sudo service nginx reload #debian/ubuntu
$ systemctl restart nginx #redhat/centos

Bonus Read : How to Install & Enable modsecurity in NGINX

4. Verify GZIP Compression

If you are wondering how to verify GZIP compression in NGINX, then there are many third-party tools like GZIP compression test where you can enter your website URL and it will tell you if GZIP compression is enabled on your website.

Alternatively, you can also use command line tools like curl to check if GZIP compression is enabled or not. Here is the command to check your website’s home page for gzip compression.

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

Here is the sample output. It should contain ‘Content-Encoding’ header as shown below.

HTTP/1.1 200 OK
...
Content-Encoding: gzip

Hopefully, the above article will help you enable and verify GZIP compression in NGINX.

Conclusion

In this article, we have learnt how to enable GZIP compression in NGINX server. Although it is disabled by default, it is highly recommended to enable it no matter what kind of website or application you are running. It reduces the size of response data on the fly, speeding up your website, improving performance and reducing network bandwidth costs. It is supported by almost all web browsers and NGINX servers.