A speedy website has 90% more visitor retention than those taking minutes to load. Furthermore, according to a report, if your website doesn’t load within 3-30 seconds, visitors are more likely to skip it and look for other similar ones.
How fast a website load is completely dependent on the size of all the files downloaded by the browser. Reducing the size of such websites not only makes them load faster, but they save a lot of bucks for the lesser bandwidth used.
What is Gzip compression?
Gzip is a popular compression program. It makes your files smaller in size, therefore, reducing the time required to transfer a resource from the server to the browser. Using Gzip compression, you can reduce file sizes by up to 70%. Implementing Gzip compression is considered a high-priority task by the majority of site speed test tools.
How does Gzip compression work?
Gzip compression program looks for duplicate strings within a file and replaces the second strong with a pointer to the previous string. This significantly reduces the amount of text within a file, thereby, reducing the whitespace and duplicate content.
In regards to the server to browser conversation, Gzip compression works in the following manner.
- A browser that comprehends Gzip makes a request from the webserver.
- The server after receiving the request notices that the browser understands Gzip. Therefore, it returns the Gzip compressed version of the requested file.
- The browser receives the file, decompresses it, and the visitor is then able to read the data.
For browsers that do not understand the Gzip compression file, the server will simply send an uncompressed version of the file. Mostly, old browsers are not able to support Gzip compression, which is why the vary header
plays a vital role in delivering the right type of content.
How does Gzip compress files?
For some files, Gzip compression works fantastic and better than others. For example, text files compress very well, whereas, JPEG or PNG files are already compressed one, rendering Gzip compression less effective or ineffective. Since compressing filling takes up server resources, it is better to enable Gzip compression in those files that will considerably be reduced in size.
How to enable Gzip compression in Nginx
Nginx is a very popular web server. Enabling Gzip compression in Nginx will significantly increase the delivery speed of your site.
The following steps will teach you how to enable Gzip compression in Nginx.
Before you commence
Before you begin, you must have the SSH access set up for root
or sudo
user.
STEP-1: Verify whether Gzip compression is enabled or not
Start by verifying whether Gzip compression is enabled on Nginx or not. There are several online tools that can help you to do. All you have to do is to enter your domain and they’ll give you all the details.
In case, you are unsure of the type of web server you are using, open the terminal window and use the curl command shown below with your domain name.
curl -I www.YourDomainHere.com
The output you’ll receive is:
CT-152541-bash-4.2# curl -I www.example.com HTTP/1.1 200 OK Server: nginx Date: Fri, 09 Feb 2016 12:55:33 GMT Content-Type: text/html Content-Length: 5937 Connection: keep-alive X-Accel-Version: 0.01 Last-Modified: Wed, 25 Jan 2016 13:43:03 GMT ETag: "1730-52a50fb2994d1" Accept-Ranges: bytes X-Powered-By: PleskLin MS-Author-Via: DAV
Your server is Nginx.
STEP-2 Enable Gzip compression
Start by connecting to your server via the SSH and open the nginx.config
file with a text editor.
Now, within the http
section of the nginx.conf
, add or edit the following lines.
http { gzip on; gzip_disable "MSIE [1-6]\.(?!.*SV1)"; gzip_vary on; gzip_types text/plain text/css text/javascript application/javascript application/x-javascript; }
STEP-3 Verify Gzip compression
Once you have added the code, restart Nginx and verify whether Gzip is working properly. Use command,
service nginx restart
There are several online tools that can check and navigate Gzip compression. You can use Google’s PageSpeed Insights to make sure you’re seeing some improvements.
Conclusion
You have successfully enabled Gzip compression in Nginx. This configuration is going to reach new levels of page serving speeds.