How to implement gzip compression in nginx to speed up your website.

Recently I was looking into page speed of my website and noticed that I forgot to enable gzip compression in my nginx config. I took out some time to implement and thus I am writing about it here. Lets see how to implement gzip compression.

Why is GZIP compression required. 

It compress your content and hence less data is transferred over network which in turn improves the performance of your website.

How to implement gzip compression

How to implement GZIP compression in nginx

Implementing it in nginx is really simple what you have to do is make some changes in config file and restart the server and the changes are not too complex. They are really simple changes and can be done in 2 min.

How to check if your content is server in compressed form.

Run the below command in terminal to request a resource.

curl -H "Accept-Encoding: gzip" -I http://domainname/resource_path

Now you will see some header which are like below

HTTP/1.1 200 OK
Server: nginx/1.4.6 (Ubuntu)
Date: Tue, 19 Mar 2017 20:04:12 GMT
Content-Type: text/html
Last-Modified: Tue, 04 Mar 2017 11:46:45 GMT
Connection: keep-alive
Content-Encoding: gzip

If content-encoding is gzip then your content is compressed else not.

Not lets implement compression in nginx. Just add the below snippet in your config file. Your nginx config file is present at /etc/nginx/sites-available/config_file_name

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_min_length 256;
gzip_types text/plain image/png image/jpg image/pjpg image/pjpeg text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript application/vnd.ms-fontobject application/x-font-ttf font/opentype image/svg+xml image/x-icon;
gzip_min_length 256; will tell nginx to not compress file below 256 bytes.
gzip types are all the file type that has to be compressed.

Now that you have changed the config file. Don’t restart the server directly first check if the config are right because if you are working in production it will break your server. To test the changes run

sudo nginx -t

If this command returns successful then restart the server with below command.

sudo service nginx restart

Now that your server is up with the latest config with compression enabled run the first command to check if the content is compressed.

curl -H "Accept-Encoding: gzip" -I http://domainname/resource_path

Now you have set up compression in your web server.

 

End of Article How to implement gzip compression in nginx to speed up your website.


Gaurav Yadav

Gaurav is cloud infrastructure engineer and a full stack web developer and blogger. Sportsperson by heart and loves football. Scale is something he loves to work for and always keen to learn new tech. Experienced with CI/CD, distributed cloud infrastructure, build systems and lot of SRE Stuff.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.