How to enable GZIP compression in Nginx
It is important to optimize your website to ensure that it is as fast as possible; website load time has a big impact on user experience and search engines such as Google consider the load time of a site as a ranking factor. SSD powered VPS is fast but not necessary when settings options such as Gzip compression in Nginx can also speed up your page load times.
In the following tutorial, we will teach you how to enable Gzip compression in Nginx.
We are going to use a method which will improve your website’s transfer speed by reducing the size of its files with compression. This can also reduce the bandwidth used and can help you get slightly cheaper if you use a hosting plan with fewer resources.
We will be using the Nginx Gzip module which will help you compress your files with the gzip compression method, with the ultimate aim of reducing the size of transmitted files by half or more.
Step 1: Enabling GZip compression
Begin by logging into your server via SSH and modifying your Nginx configuration as shown below.
[user]$ nano /etc/nginx/nginx.conf
Append or modify the lines below.
Try out the configuration and correct any errors. Some are shown here.
Restart Nginx and confirm that Gzip is working correctly.
service nginx restart
You can verify that the Gzip compression is working Nginx by simply executing the curl command shown below.
[user]$curl --header "Accept-Encoding: gzip,deflate,sdch" -I http://your-domain-name.com/ HTTP/1.1 200 OK Server: nginx Date: Fri, 14 Apr 2017 09:59:38 GMT Content-Type: text/html; charset=UTF-8 Connection: keep-alive Keep-Alive: timeout=60 Link: <http://your-domain-name.com/wp-json/>; Content-Encoding: gzip
If the message ‘Content-Encoding: gzip’ shows up, it means that your page was served and uses the Nginx gzip module.
There are many online Gzip checkers to choose from and use in order to test whether Gzip was enabled; the curl utility check and other online tools allow you to do this.
Your server should now be properly configured to use gzip compression, making your website noticeably quicker.