Skip to main content

How to enable GZIP compression to increase PageSpeed Score for Nginx?

Gzip is a popular compression algorithm and format on the web. Gzip requires browser support, but you don’t have to worry because most popular browsers have support for it. Although Instructions to enable Gzip varies with different web servers, they are still very similar. 
Here’s how Gzip works with Nginx:
Now add the following Code to your Nginx Configuration file or you can add it to your vhost domain
# Compression gzip
gzip    on;
gzip_vary   on;
gzip_comp_level  6;
gzip_min_length  512;
gzip_buffers   8 64k;
gzip_types  application/atom+xml application/javascript application/x-javascript application/json application/ld+json application/manifest+json application/xml+rss application/vnd.geo+json application/ application/x-font-ttf application/x-web-app-manifest+json application/xhtml+xml application/xml font/opentype image/bmp image/png image/svg+xml image/x-icon image/gif image/jpeg text/cache-manifest text/css text/plain text/vcard text/vnd.rim.location.xloc text/vtt text/x-component text/javascript text/js text/xml text/x-cross-domain-policy;
gzip_proxied   any;
gzip_disable   "MSIE [1-6]\.";

Here's an explanation for the configuration, line by line:

gzip on: - enables gzip compression
gzip_vary on: - tells proxies to cache both gzipped and regular versions of a resource
gzip_min_length 512: - informs NGINX to not compress anything smaller than the defined size
gzip_proxied: tell Nginx to compress data even for clients that are connecting to us via proxies like CloudFront:
gzip_types:  enables the types of files that can be compressed
gzip_disable "MSIE [1-6]\.": - disable compression for Internet Explorer versions 1-6
gzip_comp_level: You can choose a number between 1 and 9 for this value. 5 is a perfect compromise between size and CPU usage, offering about a 75% reduction for most ASCII files (almost identical to level 9).