How to enable Gzip compression on nginx server

How to enable Gzip compression on nginx server

If like me, you are obsessed with the perfect score on all possible charts, including your site metrics, then you must have come across many website load optimization options, one of which happens to involve compression using the popular reverse proxy server called Nginx.

Even if you aren’t obsessed with the perfect score, the thought of saving some heavily needed bandwidth is enticing enough to consider compressing your enormously beautiful & gorgeous website using Nginx’s gzip module, which we will be looking at today.

For this short tutorial, I expect you have at least the most basic knowledge on using Nginx and deploying a basic “hello world!” site at the very least. But for fun. If you don’t, I’ll go through the installation process on Linux, as well as the basic command on how to start and stop the server after its installed.

INSTALLING NGINX SERVER

While Nginx is available on all major modern distributaries. For this article, we will be working with Linux, but the process should be nothing ambiguous for other distributaries. To install Nginx and its required dependencies, simply run the following commands

sudo apt update
sudo apt install nginx

or

sudo apt update && apt install nginx

If Nginx was successfully installed, then we should be able to run some basic commands to check its status, start, stop, or restart the service. To do any of the previously stated actions, run either of the corresponding commands.

sudo service nginx status 
#or 
systemctl status nginx
sudo service nginx start 
#or 
systemctl start nginx
sudo service nginx stop 
#or 
systemctl stop nginx
sudo service nginx restart 
#or 
systemctl restart nginx

That will be all for the installation process since the article should be focused on compression, rather than an installation guide.

COMPRESSION ON NGINX

First, it is important to keep in mind that all files do not compress as efficiently as others. Plain files such as HTML, CSS, and javascript do pretty well with compression, whereas media files, iso files, zip, tarballs and a couple of others aren't particularly compression friendly.

Gzip compression can be done in the nginx.conf file, or a configuration file of a server block, depending on your choice.

To locate your nginx.conf file, simply navigate to /etc/nginx/nginx.conf or simply open the file on your terminal using nano editor by running the following command

sudo nano /etc/nginx/nginx.conf

Gzip configuration can be placed in one of three places, namely:

  • http block – on the nginx.conf file or

  • server or location block – on a server block configuration file.

In either of the above-advised block, simply place the following commands. Feel free to tailor them to your need, this is simply a basic configuration, you are in no way limited to this.

server {
    gzip on;
    gzip_vary on;
    gzip_disable "MSIE [1-6]\.";
    gzip_static on;
    gzip_min_length 256;
    gzip_buffers 32 8k;
    gzip_http_version 1.1;
    gzip_comp_level 5;
    gzip_proxied any;
    gzip_types text/plain text/css text/xml application/javascript application/x-     
        javascript application/xml application/xml+rss application/emacscript 
        application/json image/svg+xml;
}

or

http {
    gzip on;
    ...
}

or

location {
    gzip on;
    ...
}

Now let’s go through some of the commands above.

  • gzip on: This simply enables the gzip compression module for use on Nginx

  • gzip_types: according to the official Nginx documentation, Nginx by default compresses only MIME type text/html. In order to compress responses other than MIME types, include the gzip_type directive and specify additional types to compress. In the example above, we have specified a few to give an instance.

  • gzip_static: this allows you to send a compressed version of a file to the client, rather than the normal one.

  • gzip_min_length: This specifies the minimum length of response to compress. By default, it is set at 20 bytes, in our case its set at 256. So, Nginx will not compress anything smaller than our specified size.

  • gzip_proxied: here, you can direct Nginx to compress data for clients, depending on the header response. By default, responses to proxied requests are not compressed. However, this could be changed, like in our case, where it’s set to any. Other possible parameters, to restrict compression, include no-cache, no-store, expired, auth.

  • gzip_vary: This tells proxies to cache both gzipped and regular versions of a resource.

  • gzip_http_version: Specifies the minimum version of a client’s HTTP protocol request needed for Nginx to compress its response.

BONUS AND SUMMARY

NOTE: Whenever your Nginx settings/configurations are updated, run “sudo service nginx restart or systemctl restart nginx” to restart the server, to ensure that the latest configurations are read.

When you are done, here are some websites and tools, to test your amazing work for gzip compression and more SEO details.

The configurations above are almost 100 percent customizable, except for some required commands. This is simply a basic, most frequently used configuration. Again, you are not limited to these commands and are allowed the freedom to explore other options. To add and learn more on Nginx, please visit the Nginx docs for the latest and most accurate information.

Hope you enjoyed it!