How To Add the gzip Module to Nginx on Ubuntu 16.04

You’ll be surprised at how simple this is. Open up the file /etc/nginx/nginx.conf. The first thing you need to do is look for the directive:

gzip on;

Comment that out like so:

#gzip on;

Now add the following contents above the line you just commented out:

gzip on;
gzip_vary on;
gzip_min_length 10240;
gzip_proxied expired no-cache no-store private auth;
gzip_types text/plain text/css text/xml text/javascript application/x-javascript application/xml;
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 1024; – informs NGINX to not compress anything smaller than the defined size
  • gzip_proxied – compress data even for clients that are connecting via proxies (here we’re enabling compression if: a response header includes the “expired”, “no-cache”, “no-store”, “private”, and “Authorization” parameters)
  • gzip_types – enables the types of files that can be compressed
  • gzip_disable “MSIE [1-6]\.”; – disable compression for Internet Explorer versions 1-6

Once you’ve added the options, save and close the nginx.conf file and restart NGINX with the command:

sudo service nginx restart

NGINX should now be serving up compressed files that meet your minimum length and type configurations. Head on back to Google’s PageSpeed Insights to make sure you’re seeing some improvement.

Making fast faster

NGINX is already a fast web server; that you can eek out even more performance, speaks highly to what the developers have accomplished. Give this configuration a go and watch your web server reach new levels of page serving speeds.