CentOSLinuxTutorialsUbuntu

How To Enable Gzip Compression on Nginx

Enable Gzip Compression on Nginx

In this tutorial, we will show you how to enable Gzip Compression on Nginx.  For those of you who didn’t know, Gzip compression is a lossless data compression method that reduces the size of files transferred between your server and visitors’ browsers. It works by identifying and eliminating repetitive patterns in text-based files like HTML, CSS, and JavaScript, resulting in smaller file sizes. This reduction in file size leads to faster transfer times and quicker website loading for users.

By default, Nginx has a built-in Gzip module that allows for easy configuration of compression settings. However, it’s important to ensure that the module is properly enabled and optimized to achieve the best possible performance gains.

This article assumes you have at least basic knowledge of Linux, know how to use the shell, and most importantly, you host your site on your own VPS. The installation is quite simple and assumes you are running in the root account, if not you may need to add ‘sudo‘ to the commands to get root privileges. I will show you the step-by-step enable Gzip Compression on Nginx.

Prerequisites

  • A server running one of the following operating systems: Ubuntu or CentOS based.
  • It’s recommended that you use a fresh OS install to prevent any potential issues.
  • SSH access to the server (or just open Terminal if you’re on a desktop).
  • A non-root sudo user or access to the root user. We recommend acting as a non-root sudo user, however, as you can harm your system if you’re not careful when acting as the root.

Enable Gzip Compression on Nginx

Step 1. Installing Nginx.

By default, Nginx is not available on CentOS or RHEL-based base repositories. Now run the following command below to add a Nginx stable repository to your system:

sudo tee /etc/yum.repos.d/nginx-stable.repo<<EOF
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/9/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
EOF

Now, run the following command to install the latest stable version of Nginx to your server:

sudo dnf update
sudo dnf install nginx

Once the installation is done, start the Nginx service and enable it to automatically start on reboot all in one go with:

sudo systemctl enable --now nginx

To verify that the latest version of Nginx has been installed, run:

nginx -v

Step 2. Configure Nginx Configuration.

To enable Gzip compression on Nginx, you will need to make some changes to your Nginx configuration file. The first step is to open the file using a text editor, such as nano:

Now we edit nginx.conf (/etc/nginx/nginx.conf) :

nano /etc/nginx/nginx.conf

Add the following lines:

## 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;

Save and close the file, then restart your Nginx service:

sudo systemctl restart nginx

If you wish to test if GZIP is enabled, use this command:

curl -H "Accept-Encoding: gzip" -I http://idroot.us

With that file now in place, restart your server and you will now be serving site assets with gzip compression. Google takes site speed into account when ranking and placing your sites in their search engine so do your users a favor and strive for the fastest site possible, especially for mobile users.

Congratulations! You have successfully enabled Gzip. Thanks for using this tutorial for enabling gzip compression Nginx on the Linux system. For additional help or useful information, we recommend you check the official Nginx website.

Squid Install Service Offer
If you don’t have time to do all of this stuff, or if this is not your area of expertise, we offer a service to do “Nginx Webserver” installation, starting from $10 (Paypal payment). Please contact us to get the best deal!

r00t

r00t is an experienced Linux enthusiast and technical writer with a passion for open-source software. With years of hands-on experience in various Linux distributions, r00t has developed a deep understanding of the Linux ecosystem and its powerful tools. He holds certifications in SCE and has contributed to several open-source projects. r00t is dedicated to sharing her knowledge and expertise through well-researched and informative articles, helping others navigate the world of Linux with confidence.
Back to top button