Introduction
Let’s Learn 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.
Also, here we have a few hand-picked guides that you must read next:
- How can I setup WordPress rewrite on Nginx?
- How To Install Linux, Nginx, MySQL, and PHP (LEMP) stack on Ubuntu 14.04
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.
gzip on; gzip_comp_level 2; gzip_http_version 1.0; gzip_proxied any; gzip_min_length 1100; gzip_buffers 16 8k; gzip_types text/plain text/html text/css application/x-javascript text/xml application/xml application/xml+rss text/javascript; # Disable for IE < 6 because there are some known problems gzip_disable "MSIE [1-6].(?!.*SV1)"; # Add a vary header for downstream proxies to avoid sending cached gzipped files to IE6 gzip_vary on;
Try out the configuration and correct any errors. Some are shown here.
nginx -t
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 you have successfully served the page and it uses the Nginx gzip module.
Conclusion
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.