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.
Optimized GZIP – Basic Setup
First, navigate to your nginx directory and open the nginx.conf file.
sudo nano /etc/nginx/nginx.conf
Next, copy the below-optimized settings for an introduction for compression.
This is good for servers that do not have many resources to spare. It’s basic and does the job.
You may be wondering before we look into a more advanced setup what some of the terms mean.
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 would be 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 very 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 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 versions of the resource. 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 have a negative effect.
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
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 significant factoring for SEO rankings, and this simple solution could be the difference in gaining more traffic in the long term.