Enable Gzip Compression On Nginx

By bhagwatchouhan
Enable Gzip Compression On Nginx

In this tutorial, we will discuss the steps required to enable Gzip compression of static resources served by the Nginx Web Server. When the browser or HTTP client sends a request to a web server, the server will either generate the response(HTML) or locate the resource i.e. image, JavaScript, CSS and try to compress where possible before replying back to the browser or HTTP client. This is where Gzip compression happens where the webserver further compresses the resources before sending it to the requestor. It will add additional overhead on the server-side since it will consume additional resources to support the compression. At the same time, the compressed resources will reduce network payload which loads the sites faster.

The gzip configuration does the heavy lifting job to compress and serve the static resources including HTML, images, JavaScript, CSS, etc.

 

Enable Compression

We can enable the compression by updating the http, server, or location context as shown below. I have used the http block for demonstration purposes which will enable the compression for all the sites. We can enable it only for specific sites or locations by updating the server or location block.

    # Enable gzip    # Gzip types    # Minimum length(bytes) to compress 

I have enabled the compression by enabling gzip as shown above. Also, we can specify the types of resources to be considered for compression. We can also specify the minimum size of the resources to be compressed to avoid compression of resources smaller in size.

We can also enable the compression of proxied resources by adding another configuration as shown below.

# Enable compression of proxied resources
gzip_proxied no-cache no-store private expired auth;

 

Enable Decompression

There are clients who do not support the responses having gzip compression enabled on the server-side. We must also enable decompression to serve such clients. In this case, Nginx will use the compressed assets from the cache and decompress before sending a response to the client. We can enable decompression for server and location blocks as shown below.

# Update server block
sudo nano /etc/nginx/sites-available/example.com
# Content server { ... ...
gunzip on;
...
... }
# OR
server {
... ...
location / {
... gunzip on;
... }
...
... }

We can enable compression and decompression within the server block as shown below.

# Update server block
sudo nano /etc/nginx/sites-available/example.com
# Content server { ... ...
# Enable gzip gzip on; # Gzip types gzip_types text/plain application/xml; # Minimum length(bytes) to compress gzip_min_length 100;
# Enable compression of proxied resources gzip_proxied no-cache no-store private expired auth;
# Enable decompression gunzip on;
... ... }

 

Pre-Compressed Assets

We can also configure Nginx to serve the pre-compressed assets. In this way, Nginx won't compress the assets, but serve the assets which are already compressed by other tool or library. The most popular tools include Google Brotli or Zopfli.

We can simply serve the pre-compressed assets as shown below.

# Update server block
sudo nano /etc/nginx/sites-available/example.com
# Content server { ... ... # Enable gzip static gzip_static on;
# Enable compression of proxied resources gzip_proxied expired no-cache no-store private auth;
...
... }
# Save and exit the editor
# Check configurations sudo nginx -t
# Output nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful
# Restart Nginx sudo systemctl restart nginx

 

Summary

This is how we can enable compression and decompression on the Nginx Web Server. We can speed up the websites and reduce the load time by enabling the compression of the selected resource types.

Share this blog:

Profile picture for user bhagwatchouhan
bhagwatchouhan