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 some cases). In this post, we’ll cover the steps how to enable gzip compression on Nginx server.
Enable Gzip compression on Nginx
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:
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
A heads up note regarding the gzip compression configuration - the configuration presented here is a “default one” you can say or a base config which can be used as starting tuning point that matches your requirements. Meaning, with this configuration, the results may vary for you. It all depend on your web site’s content and the amount files you’re serving.
In another words, the configuration can be a double edged sword and may not work best for you. If for an example you have too many small files(less than 0.5 mb in size) and you set gzip compression on the all files, it can reduce the performance of the web server and even slow down nginx since it will take it a lot of time to compress all the files. That’s why it’s usually suggested to start with the largest files you have on your server(files over 1 mb in size) to compress those.
So, you’ll then probably change the values of the gzip_min_length option until you reach the configuration that matches your needs and performance.
Restart Nginx server
After you add the lines in the config file, save the file and exit. After that, restart the nginx server. The configuration should be in effect after the restart and it’ll enable gzip compression on Nginx. It take time to compress all files(depends on amount of file and the server resources) but you should start seeing improvements in the first couple of minutes.
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 compression on Nginx server. A rather simple configuration but it provides huge web server performance improvement. 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.