Enable & Configure Gzip Compression on Nginx

Part of any website operation is to make sure visitors can view the site as quickly as possible. Still, one of the biggest causes of slowdowns is loading resources that depend on size can severely impact a website without GZIP enabled to a similar website.

NGINX is an excellent web server, built for speed, lightweight to handle multiple connections, and it does come with GZIP support, but this can be a double-edged sword as using GZIP increases CPU utilization. Depending on your server and its resources, it could have the opposite impact enabling it without optimization.

So interested in the topic so far? In our tutorial, you will learn the basic GZIP setup and configuration on Nginx, which can be used on any operating system, UNIX or Windows.

Optimized GZIP – Basic Setup

First, navigate to your Nginx directory and open the nginx.conf file.

Linux Example:

sudo nano /etc/nginx/nginx.conf

Next, copy the below-optimized settings for an introduction to compression.

This is good for servers that do not have many resources to spare, and it’s essential and does the job.

## enables GZIP compression ##
 gzip on; 

 ## compression level (1-9) ##
 ## 4 is a good compromise between CPU usage and file size. ##
 gzip_comp_level 4;

 ## minimum file size limit in bytes, to low can have negative impact. ##
 gzip_min_length 1000;

 ## compress data for clients connecting via proxies ##
 gzip_proxied any;

 ## disables GZIP compression for ancient browsers that don't support it. ##
 gzip_disable "msie6";

 ## compress outputs labeled with the following MIME-types. ##
 ## do not add text/html as this is enabled by default. ##
 gzip_types
     application/json
     application/javascript
     application/xml
     text/css
     text/javascript
     text/plain
     text/xml;

Before we look into a more advanced setup, you may be wondering what some of the terms mean.

GZIP Definitions

Compression Level – gzip_comp_level #;

The gzip_comp_level can be set between 0 to 9. The highest the compression level, the higher the compression is applied. When higher compression levels, such as the max, are 9, then more CPU is needed. If your server is struggling with CPU usage, it’s recommended to keep mid-range as the trade-offs for increasing this setting are minimal, and you will hardly see a difference in results.

Compression Minimum Length – gzip_min_length #;

Nginx uses compression when a response length is more significant than 1000 bytes which cannot be changed. You can set this lower than 1000 bytes; however, for smaller files, the time taken to compress these files is more significant than the time saved in transferring. Also, you will be clogging up needless use of the CPU, and in some cases, you can increase file sizes, such as static files like images which should never be included.

Compression Vary Header – gzip_proxied #;

This directive tells proxies to cache both regular and gzipped resource versions. Nginx will only add this header when compression is used, depending on the gzip_min_length setting.

Compression Mime Types

Mime types located in your /yourlocation/nginx/mime.types are the content types listed in your Nginx that GZIP can compress if listed. We only listed the basic ones; however, you can compress many other aspects. Remember not to gzip static files such as images, as it will negatively affect you.

The only binary files that can be compressed with images are “image/svg+xml”

Advanced Optimized Nginx and GZIP set-up

Below we will show an example of a more advanced set-up, much more can be done, and you should be testing what works well with your server under live load.

Remember, your nginx.conf is located in nginx folder.

sudo nano /etc/nginx/nginx.conf
## enables GZIP compression ##
 gzip on; 

 ## compression level (1-9) ##
 ## 4 is a good compromise between CPU usage and file size. ##
 gzip_comp_level 6;

 ## minimum file size limit in bytes, to low can have negative impact. ##
 gzip_min_length 1000;

 ## compress data for clients connecting via proxies. ##
 gzip_proxied any;

 ## disables GZIP compression for ancient browsers that don't support it. ##
 gzip_disable "msie6";

 ## compress outputs labeled with the following MIME-types. ##
 ## do not add text/html as this is enabled by default. ##

 gzip_types
     application/atom+xml
     application/geo+json
     application/javascript
     application/x-javascript
     application/json
     application/ld+json
     application/manifest+json
     application/rdf+xml
     application/rss+xml
     application/xhtml+xml
     application/xml
     font/eot
     font/otf
     font/ttf
     image/svg+xml
     text/css
     text/javascript
     text/plain
     text/xml;

For testing afterward, a few websites such as Gift for Speed and SiteCheckerPro can do online tests. This is probably the easiest way to tell how your server performs under load from an external source.

Comments and Conlusion

GZIP is one of the most effective ways to optimize your website before minification software can complicate and break websites during configuration. GZIP will not negatively affect your website unless you set the compression rate to CPU too high or the minimum length too low where it’s compressing unnecessary files.

Many statistics tell you to speed is a significant factor for SEO rankings, and this simple solution could be the difference in gaining more traffic in the long term.



Follow LinuxCapable.com!

Like to get automatic updates? Follow us on one of our social media accounts!