How To Enable Gzip in NGINX

Gzip compression improves website speed and performance by enabling servers to compress files before sending them to the client browsers. By default, Gzip compression is disabled in NGINX web server. In this article, we will look at how to enable Gzip compression in NGINX.


How To Enable Gzip in NGINX

Here are the steps to enable Gzip in NGINX. It is important to note that Gzip module is already installed in NGINX. We just have to enable it.


1. Open NGINX configuration file

Open NGINX configuration file in a text editor.

# sudo vi /etc/nginx/nginx.conf

Also read : How to Redirect Subfolder to Root in Apache


2. Enable Gzip compression

It is quite easy to enable Gzip compression in NGINX. Just add the following line in http block of server configuration to do it.

gzip on;

Here are some additional commands you can add, depending on your requirement.

gzip_vary on; 
gzip_min_length 10240; 
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 the above code, line by line,

  • gzip on – enables gzip compression
  • gzip_vary – tells proxies to cache both compressed and non-compressed versions of files
  • gzip_min_length – tells nginx not to cache files smaller than the specified size
  • gzip_proxied – compress data even for clients that have connected through proxies
  • gzip_types – the file types that you want NGINX to compress. For our example, we are compressing plain text, css, js and xml data. You can add more mime types as per your requirement.
  • gzip_disable – disable gzip compression for internet explorer versions 1-6

Also read : How to Install WordPress in NGINX in Ubuntu


3. Restart NGINX server

Restart NGINX server to apply changes.

$ sudo nginx -t 
$ sudo systemctl restart nginx

Also read : How to Change Root password in Ubuntu


That’s it. Gzip compression will now be enabled in NGINX web server.

Leave a Reply

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