Optimize Content Delivery: Enabling Gzip with Nginx or Ingress

Lakshan Fernando
2 min readMar 4, 2024

--

What is the Gzip and its history?

Gzip is a file format and a software application used for file compression and decompression. The program was created by Jean-loup Gailly and Mark Adler as a free software replacement for the compression program used in early Unix systems. It was first released in 1992.

Gzip is often used to compress web content on the server side before sending it over the internet to speed up the transfer of data and reduce bandwidth usage. When a web server sends gzip-compressed content, it is decompressed by the browser on the client side before rendering. This makes web pages load faster than if they were sent uncompressed.

The gzip the program applies the Lempel-Ziv coding (LZ77) to the input files. Files compressed by gzip are typically named with the extension .gz. Gzip can only compress single files and does not hold directories. To compress multiple files and directories, one often uses gzip in conjunction with tar, resulting in tarball files with an .tar.gz or .tgz extension.

Note: If Nginx is your server’s choice, consult the first option; alternatively, if you’re employing Ingress, refer to the second.

[1] Setup Gzip on Nginx

To set Gzip on Nginx, there are a few small steps to follow:

Step 1: Go to the Nginx config file

sudo nano /etc/nginx/nginx.conf

Step 2: Enable gzip compression using adding the following line on the Nginx Config file,

http {
gzip on;
gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
# Other HTTP configurations…
}

Step 3: Finally, restart the Nginx service to apply the changes:

sudo systemctl restart nginx

By adding the gzip directive within the http block of your Nginx configuration file, you enable gzip compression for HTTP responses served by your Nginx server.

[2] Setup Gzip on Ingress

To set up gzip compression, you need to update the ingress config map (not the services config maps). Refer to the following steps to do it.

Step 1: Find the namespace where your Ingress controller is deployed to see its status,

kubectl get namespaces

Step 2: Edit the Ingress config map by giving the ingress name and namespace,

kubectl edit ingress <ingress-name> -n <namespace>

Step 3: Add the following data to the ingress controller config map.

data:
use-gzip: "true"
gzip-level: "5"
gzip-types: "text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript"

Now apply the config and check whether your services are compressed using Gzip by running the following command on your PC.

curl -I -H "Accept-Encoding: gzip" https://<your.domain.name>

Once the command above is executed, successful implementation will be indicated by the appearance of “Content-Encoding: gzip” in the resulting output.

Cheers 🥂 Now you have successfully encoded your content using gzip! 🎉

Thank you!

Tharindu Lakshan.

--

--