How to enable gzip compression on Nginx server?

Updated on November 29, 2017

The page load time is one of the most important part of your website’s visitor experience. Of course, the design of the website is important, but do you know what the visitor is more concerned about? It’s how fast the pages load. And it depends on the size of all of the files that have to be downloaded by the client browser. Consider a delay of just one second can reduce customer satisfaction and result in loss of conversions. Which is why, web developers work tirelessly to improve the website load time. One such way to increase the page speed is to compress the files that have to be downloaded by the browser. It does not only improve the website’s load time, but also cheaper to those who have to pay for the bandwidth usage! In this guide, I shall tell you how to enable gzip compression on Nginx web server and how it helped my site to load faster.

You can configure Nginx with gzip a popular data compression program to compress files on the fly. Those files are then decompressed by the browser that support gzip with no loss whatsoever.

Test Nginx default behavior for gzip compression

On my fresh installation of Nginx, lets check whether the gzip has been enabled?

$curl -H "Accept-Encoding: gzip" -I https://techglimpse.com
 HTTP/1.1 200 OK
 Server: nginx
 Date: Wed, 29 Nov 2017 11:14:25 GMT
 Content-Type: text/html; charset=UTF-8
 Connection: keep-alive
 ETag: W/"52f49928-fe89"
 Accept-Ranges: bytes

From the above result, there is no mention of compression!

Enable gzip compression on Nginx server

Let’s configure Nginx to compress not only HTML files, but also other file formats like, Images, CSS, JS etc., that can benefit from compression.

Open the Nginx configuration file in your favorite text editor and insert the below code.

# vim /etc/nginx/nginx.conf

 # reduce the data that needs to be sent over network
 gzip on;
 gzip_vary on;
 gzip_buffers 16 8k;
 gzip_comp_level 6;
 gzip_min_length 1000;
 gzip_proxied expired no-cache no-store private auth;
 gzip_types text/plain 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_disable msie6;
 gzip_http_version 1.1;

For the new configuration to take effect, test the configuration for errors and then restart Nginx.

# nginx -t

# systemctl restart nginx

Verify Gzip compression

$curl -H "Accept-Encoding: gzip" -I https://techglimpse.com
HTTP/1.1 200 OK
Server: thing
Date: Wed, 29 Nov 2017 12:00:46 GMT
Content-Type: text/html; charset=UTF-8
Connection: keep-alive
Vary: Accept-Encoding
Link: <https://techglimpse.com/wp-json/>; rel="https://api.w.org/"
X-Cache: MISS
Content-Encoding: gzip

Google gives importance to website’s load time when ranking and placing your website in search results upon your competitors. Enabling gzip will increase organic traffic and visitors like websites that load faster – especially mobile users!

Was this article helpful?

Related Articles

Leave a Comment