How to enable Gzip compression on Nginx server


How to enable Gzip compression on Nginx server

Overview

If we want to use Nginx as a web server, for better caching and website performance, it is recommended to have compression enabled, such as gzip compression. Nginx is another good open sourceĀ  web server and it’s packed with many good features. Unfortunately, gzip compression on Nginx is not enabled by default. In this post, we’ll cover the steps how to enable gzip compression on Nginx server.

Enable Gzip compression

In order to enable gzip compression, we are going to edit the Nginx config file. For this, you can use any text editor, but in this example, we’ll go with nano text editor. Run the terminal, and navigate to the nginx config file:

/etc/nginx/nginx.conf

Terminal command with text editor will look like this:

sudo nano /etc/nginx/nginx.conf

In the config file, we need to add the following files in order to enable the gzip compression. Scroll down to the part of the file where the line will start with Gzip Settings:

gzip on;

gzip_vary on;
gzip_proxied any;
gzip_comp_level 6;
gzip_buffers 16 8k;
gzip_http_version 1.1;
gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

On the image bellow you can see how the config should look like:

nginx enable gzip compression

 

Here’s an explanation for the configuration, line by line:

  • gzip on; – enables gzip compression
  • gzip_vary on: – tells proxies to cache both gzipped and regular versions of a resource
  • gzip_min_length 1024; – informs NGINX to not compress anything smaller than the defined size
  • gzip_proxied – compress data even for clients that are connecting via proxies (here we’re enabling compression if: a response header includes the “expired”, “no-cache”, “no-store”, “private”, and “Authorization” parameters)
  • gzip_http_version 1.1 – is the minimum version of the HTTP protocol of the request (coming from client) needed to compress the response from server.
  • gzip_types – enables the types of files that can be compressed

Restart Nginx server

After you add the lines in the config file, save the file and exit. After that, restart the nginx server.

The commands bellow can be used for all Debian/Ubuntu/RHEL and CentOS distros

Use the following command:

# /etc/init.d/nginx restart

OR

# /etc/init.d/nginx reload

OR

# service nginx restart

OR

# service nginx reload

OR if you are using systemd based Linux distro:

$ sudo systemctl restart nginx

OR

$ sudo systemctl reload nginx

To view server status:

# service nginx status

OR

$ sudo systemctl status nginx

Summary

We covered the steps how to enable gzip on Nginx server. The configuration we used in this post is, you can say a default configuration. It’s the most common one at least, but it works very well. You can check out more about compression and decompression on the nginx gzip link and how to add more parameters and compression attributes.

I hope you found the information useful. I thank you for your time.


You may also like...

Leave a Reply

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