Enabling Gzip compression lets your page load faster and saves bandwidth usage. This easy to follow, step-by-step tutorial is show you how to enable Gzip compression on a Nginx server running on Ubuntu 14.04 and test if it working. Earlier this week I have written a post “Optimize Compress JPEG, PNG Images on Ubuntu Server“. And, this post also falls in that same category as optimizing and performance improvement of a server. You also need this for your site’s SEO and it is very easy to enable this option on your Nginx server.
Enable Gzip Compression
To enable Gzip compression on Nginx server we need to open & edit /etc/nginx/nginx.conf file. Run the following command on your terminal to edit the configuration file. I use nano
to edit files, you can choose your own text editor, just change the name.
sudo nano /etc/nginx/nginx.conf
Now, scroll down and you may find the Gzip section commented out with # in the beginning of the line. Just uncomment the sections and Gzip should be working. Or just add the following in the conf file.
##
# Gzip Settings
##
gzip on;
gzip_disable "msie6";
gzip_vary on;
gzip_proxied any;
gzip_comp_level 6;
gzip_buffers 16 8k;
gzip_http_version 1.1;
gzip_types
application/atom+xml
application/javascript
application/json
application/rss+xml
application/vnd.ms-fontobject
application/x-font-ttf
application/x-web-app-manifest+json
application/xhtml+xml
application/xml
font/opentype
image/svg+xml
image/x-icon
text/css
text/plain
text/x-component
text/xml
text/javascript;
Save and exit the file. Next step is to check for errors and restart Nginx. Use the following command to check of errors.
sudo nginx -t
If the output is like below, your configurations are correct.
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
Now restart Nginx for your changes to take effect using the following command.
sudo service nginx restart
How to test if Gzip is working on your site?
Use the following command to check if Gzip is working on your site or on Nginx server. Change the domain name or url that you want to test.
curl –header “Accept-encoding: gzip,deflate” -I https://techloverhd.com
If it is enabled and working you should see the result like below.
HTTP/1.1 200 OK
Server: nginx
Content-Type: text/html; charset=UTF-8
Connection: keep-alive
Vary: Accept-Encoding
Date: Sun, 27 Mar 2016 20:36:41 GMT
Content-Encoding: gzip
The last line shows that Gzip is working for this site. Also you can test online on GTmetrix. Hope you enjoyed this tutorial and successfully enabled gzip compression on your site / server. There are more tutorials to explore on this site.