How to enable GZIP compression in Nginx

Everything Linux, A.I, IT News, DataOps, Open Source and more delivered right to you.
Subscribe
"The best Linux newsletter on the web"

Hello, friends. Speeding up websites is a task we have to do if we manage a server or if we own one of them. One possible solution is to enable GZIP compression in Nginx.

What does GZIP compression allow us to do?

Enabling GZIP compression allows Nginx to compress the response data before sending it to the client. This saves a lot of response time, saves bandwidth, and therefore speeds up the execution of the website.

The reality is that this is a good way to optimize your website and make it more navigable.

Prerequisites

Before we continue, we have to make sure that we meet the requirements to complete the post without problems

  • You need to have Nginx installed and configured. So make sure you have it using the official repositories of your Linux distribution.
  • The version of Nginx must be higher than 1.9.5 You should have no issue to have it because most distributions include recent versions.
  • Have basic knowledge on the use of the terminal.
  • A user with sudo access or root access.

Let’s go for it.

Enable GZIP compression on Nginx

The first thing we need to do is to modify the configuration file of your website. In short, we are talking about the Server Block or Virtual host file. You can choose any text editor.

sudo nano /etc/nginx/sites-enabled/domain.conf

And add the following lines

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]\.";
1.- Enable GZIP compression in Nginx
1.- Enable GZIP compression in Nginx

Let’s explain them a bit.

gzip on enables GZIP compression. gzip_vary ontells proxies to cache both gzipped and regular versions of a resource.gzip_min_lengthsets the minimum size of files to compress; in this case 1024 bytes or 1kb.gzip_proxiedenables GZIP even on proxy connections.gzip_types` sets the types of files to compress.

Finally, the gzip_disable directive allows you to set an exception to compression. In this case, it is disabled for older versions of Internet Explorer because it does not support this feature.

Save your changes and close the editor.

To apply the changes, restart Nginx.

sudo systemctl restart nginx

And you are done.

Conclusion

Nginx is a very lightweight web server, but we can always play with it to get as much performance out of it as possible. Today, you learned a quick and easy way to speed up your websites by simply enabling GZIP compression.

Everything Linux, A.I, IT News, DataOps, Open Source and more delivered right to you.
Subscribe
"The best Linux newsletter on the web"
Angelo
Angelo
I am Angelo. A systems engineer passionate about Linux and all open-source software. Although here I'm just another member of the family.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest articles

Join us on Facebook