NGINX is a powerful web server that is easily customizable. It provides many ways to speed up your website and reduce server load. One such feature is GZIP compression, which compresses data and files before sending them to web browsers. This only improves your website speed and performance. Here’s how to enable GZIP compression in NGINX.
How to Enable GZIP Compression in NGINX
Here are the steps to enable GZIP compression in NGINX. It is very easy to enable GZIP compression in NGINX.
1. Open NGINX config file
Open NGINX config file in a text editor
$ sudo vim /etc/nginx/nginx.conf
2. Enable GZIP compression
Look for the following directive
Remove ‘#’ sign to uncomment the directive. If you can’t find the line, then add it directly.
This will enable gzip compression in NGINX.
3. Optimize GZIP compression (Optional)
The above setting will compress all files and data before sending them to client’s web browser. This can resource intensive. So we configure NGINX to compress only large objects.
To do that, add the following lines to your NGINX config.
- gzip_vary – tells NGINX to cache both gzipped and regular version of files
- gzip_min_length – the minimum size of content that needs to be gzipped. Anything smaller will not be compressed
- gzip_proxied – send compressed data even for proxy-based connections
- gzip_types – types of files that can be compressed
- gzip_disable – disable gzip for Internet explorer versions 1-6
4. Restart NGINX web server
Restart NGINX web server to apply changes
$ sudo service nginx restart
5. Test GZIP compression
Now NGINX will start serving compressed files and data. You can test it by visiting Google PageSpeed insights and entering your website address there. It will analyze your web page and report the compression percent.
That’s it! As you can see it is quite easy to enable gzip compression in NGINX. If you want to compress everything, skip step 3 mentioned above. You can also change the gzip_min_length variable to change the min file size for compression.