How to configure gzip compression with NGINX

Development

The Complete Guide

NGINX is an outstanding, open source web server. It’s easy to get up and running, lightweight, and geared for speed. However, as with any server software, there are always ways to eek out as much performance as possible. NGINX is not short on options. One such performance route admins will take is that of the gzip compression available with NGINX. With the help of compression, the server will be sending smaller objects to clients, thereby the pages will be faster to load. One of the big mistakes with compression is betting the whole farm and compressing every object available for each page. Unfortunately, with this option comes a bit of a trade off. Compressing every object can take significant CPU resources from your server hardware. That, in turn, will cause a slowdown in NGINX, thus rendering the configuration null. How do you avoid that defeating tradeoff?

Simple.

Instead of compressing every object, configure NGINX to only compress large files and avoid the temptation to compress smaller files (such as images, executables, etc.). I’m going to show you how to achieve this with a couple of quick steps. Before you do, you might want to run Google’s PageSpeed Insights on your page before and after this configuration (so you can see, for yourself, the gains made).

The configuration

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.

Fastest Load Times

NGINX is already a fast web server; that you can seek 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.

NGINX

Conclusion

We recommend using NGINX with HOST SSH.