How to Enable Gzip Compression in Nginx on Linux

Website speed and efficiency are crucial for a positive user experience and for improving website rankings on search engines. One effective way to achieve this is by enabling Gzip compression in Nginx on Linux. This article will guide you through enabling Gzip compression in Nginx on Linux.

What is Gzip Compression?

Gzip compression is a method of compressing data to reduce the amount of data transmitted over the network. This results in faster website loading times and improved website speed and efficiency. When a user requests a website, the server sends the compressed data to the user’s browser, which then decompresses the data and displays the website.

Benefits of Enabling Gzip Compression in Nginx on Linux:

  • Improved website speed and efficiency
  • Reduced server load and bandwidth usage
  • Enhanced user experience

How to Enable Gzip Compression

Step 1: Check if Gzip Compression is Enabled

Before enabling Gzip compression in Nginx, it’s important to check if it’s already enabled. To check if Gzip compression is enabled, use the following command in your terminal:

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

If Gzip compression is enabled, you’ll see the following output in your terminal:

HTTP/1.1 200 OK
Server: nginx
Date: Fri, 11 Feb 2023 07:50:05 GMT
Content-Type: text/html
Content-Encoding: gzip
Last-Modified: Mon, 05 Feb 2023 12:45:05 GMT
Connection: keep-alive
Vary: Accept-Encoding

Step 2: Edit Nginx Configuration File

To enable Gzip compression in Nginx, you’ll need to edit the Nginx configuration file. The configuration file is usually located at /etc/nginx/nginx.conf. To edit the file, use a text editor such as nano:

sudo nano /etc/nginx/nginx.conf

Add the following code to the Nginx configuration file to enable Gzip compression:

## enables GZIP compression ##
 gzip on; 

 ## do not add text/html as this is enabled by default. ##
 gzip_types
     application/json
     application/javascript
     application/xml
     text/css
     text/javascript
     text/plain
     text/xml;

Test the nginx service before restarting to ensure no syntax errors have occurred.

sudo nginx -t

If everything is ok, restart the Nginx service using the following command.

sudo systemctl restart nginx

Optimized Nginx and GZIP Configuration

For those looking for an advanced setup for their dedicated server, below is an example of a more optimized Nginx and GZIP configuration. It’s important to note that this is just one example and that you should experiment with different settings to determine what works best for your server under live load conditions.

Here’s an example of a sample Nginx configuration located in the Nginx folder (nginx.conf):

sudo nano /etc/nginx/nginx.conf
## enables GZIP compression ##
 gzip on; 

 ## compression level (1-9) ##
 ## 4 is a good compromise between CPU usage and file size. ##
 gzip_comp_level 9;

 ## minimum file size limit in bytes, to low can have negative impact. ##
 gzip_min_length 1000;

 ## compress data for clients connecting via proxies. ##
 gzip_proxied any;

## add vary header for responses for responses eligible for compression ##
gzip_vary on;

 ## compress outputs labeled with the following MIME-types. ##
 ## do not add text/html as this is enabled by default. ##

 gzip_types
     application/atom+xml
     application/geo+json
     application/javascript
     application/x-javascript
     application/json
     application/ld+json
     application/manifest+json
     application/rdf+xml
     application/rss+xml
     application/xhtml+xml
     application/xml
     font/eot
     font/otf
     font/ttf
     image/svg+xml
     text/css
     text/javascript
     text/plain
     text/xml;

In this example, we’ve enabled GZIP compression, set the compression level to 9 (the maximum), and specified the minimum length of 1000 bytes for a response to be eligible for compression. We’ve also set the gzip_proxied directive to any so that proxies will cache both regular and gzipped versions of a resource. The gzip_vary directive is set to on to indicate that a Vary header should be added for responses that are eligible for compression. The gzip_types directive specifies the mime types of the content that can be compressed using GZIP. This case includes text files, CSS files, JavaScript files, XML files, and more.

GZIP Definitions and Explanations

Compression Level – gzip_comp_level #;

The gzip_comp_level can be set between 0 and 9, with higher values representing a higher compression level. However, it’s important to note that higher compression levels require more CPU processing power. If your server is already facing CPU constraints, keeping the compression level in the mid-range is recommended, as the difference in results from increasing this setting is minimal.

Compression Minimum Length – gzip_min_length #;

By default, Nginx uses compression for responses greater than 1000 bytes. While it’s possible to set this value lower, doing so is not recommended as the time required for compressing smaller files is more significant than the time saved in transmitting them. Additionally, using compression for small files can also lead to increased CPU usage and, in some cases, can even result in larger file sizes, particularly for static files like images which should never be compressed.

Compression Vary Header – gzip_proxied #;

The gzip_proxied directive informs proxies to cache both the original and compressed resource versions. Nginx will only add this header when compression is enabled, and the presence of the header depends on the gzip_min_length setting.

Compression Mime Types

The mime types listed in your Nginx configuration’s mime.types file determines the types of content that can be compressed using GZIP. While only the most common types are listed by default, it’s possible to compress many others. It’s important to note that compressing static files such as images is not recommended as it can have a negative impact on performance. The only exception to this rule is for “image/svg+xml” binary files.

Closing Thoughts on Enabling Gzip Compression with Nginx

In conclusion, enabling Gzip compression in Nginx on Linux is a simple and effective way to improve website speed and efficiency. Following the steps outlined in this article, you can easily enable Gzip compression and enjoy the benefits of a faster and more efficient website. Whether a website owner or a web developer, maximizing website speed and efficiency is essential for a positive user experience and improved search engine rankings.

Share to...