Enabling Gzip Compression in Nginx Web Server on Linux Systems

undefined

In this article, we will show you the steps and benefits of enabling Gzip compression in Nginx web server on Linux systems (RPM Family “Redhat/CentOS/Scientific Linux ”and Debian Family “Debian/Ubuntu”).

Performance of your website can be improved by enabling response compression using GZIP. Compression reduces the size of a response body, reduces the bandwidth required to transfer the response data, and ultimately makes sure the resources of your website are delivered to the client side sooner “reduce website page load time”.

Compression can be enabled using the gzip directive. This directive is valid in the http, server, location, and if sections. To enable gzip compression, follow the below steps:

Step 1: Enable Gzip in Nginx

By default, only documents with MIME type text/HTML are compressed. To enable compression for other types of documents, use the gzip_types directive, add the following content in Nginx main configuration file or create a separate gzip configuration file with following content.

# vim /etc/nginx/conf.d/gzip.conf
gzip on;
gzip_vary on;
gzip_min_length 512;
gzip_http_version 1.1;
gzip_types text/plain text/css text/html text/javascript text/xml application/json application/javascript application/x-javascript application/xml application/xml+rss;

The preceding configuration enables compression for MIME types that hypertext documents, cascading style sheets, and JavaScript files appear to be in. These are the types of documents that benefit most from the compression.

Hints:

1. Archives, images, and movies are not suitable for compression, as they are usually already compressed.
2. Executable files are less suitable for compression, but can benefit from it in some cases.
3. We used gzip_min_length directive to disable compression for small documents.
4. Response compression comes at a cost: it is CPU-intensive. You need to consider that in your capacity planning and system design.

With the preceding configuration, all documents smaller than 512 bytes will not be
compressed. The length information that is used to apply this restriction is extracted
from the Content-Length response header. If no such header is present, the response
will be compressed regardless of its length.

Step 2: Restarting Nginx Server

After adding the above configuration just restart your Nginx web server for the compression to work.

Now as your site has been enabled the gzip compression. You can use any online tools to verify that Gzip is working correctly on your website or use YSlow plugin on your browser.

If You Appreciate What We Do Here On Mimastech, You Should Consider:

  1. Stay Connected to: Facebook | Twitter | Google+
  2. Support us via PayPal Donation
  3. Subscribe to our email newsletters.
  4. Tell other sysadmins / friends about Us - Share and Like our posts and services

We are thankful for your never ending support.

Leave a Reply

Your email address will not be published. Required fields are marked *