In this article, we will learn How to enable GZIP compression for NGINX on Ubuntu 18.04. Before enabling the GZIP compression lets first know more about the GZIP and compression.
GZIP is a way to compress data. It is commonly used for web apps and websites. HTTP protocols have an ability to use GZIP compression. When we request a page by entering any URL in the browser, the web server compresses the data based on the request and send back it to the browser. At the browser, data will decompress and displays it to the end user. We are assuming GZIP compression is enabled at the server. Now you may ask why we need to use GZIP compression.
To understand the importance of the GZIP compression we created a sample page. It is available on our server web root directory. In this sample page, we are using the following files.
If we load this page in a browser, the total expected size is around (4 MB + 4 MB + 4 MB + 5.5 MB = 17.5) 18 MB.
But in actual it is 13.4 MB Why? Let's check the following image.
GZIP compression is used to compress the files on the Web Server. The compressed file helps to load the web page faster and reduce the bandwidth usage.
curl -H "Accept-Encoding: gzip" -I http://126.96.36.199/index.php curl -H "Accept-Encoding: gzip" -I http://188.8.131.52/index.html curl -H "Accept-Encoding: gzip" -I http://184.108.40.206/Test1.js curl -H "Accept-Encoding: gzip" -I http://220.127.116.11/Test1.css curl -H "Accept-Encoding: gzip" -I http://18.104.22.168/lemon.jpg
If for any of the above commands if you get an entry for "Content-Encoding: gzip" then GZIP compression is enabled for that file type.
As per the image, GZIP compression is enabled in PHP. Let’s enable it for JS, CSS, and JPG.
To Configure GZIP compression for NGINX, we need to edit the nginx.conf.
To open the nginx.conf execute the following command.
sudo nano /etc/nginx/nginx.conf
In the nginx.conf file search for the gzip settings.
As shown in the image GZIP is by default is ON. But rest of the settings are disabled. To enable the rest of the settings, we need to uncomment the required settings. In this, we need to discuss the following important lines.
It tells NGINX not to compress files smaller than 256 bytes. 256 bytes is a very small file. We hardly get any benefit from the compression. If the line is not available, then you can add it.
It tells NGINX to compress typed of files. You can add additional files. For example, we can add following types
To save the config file, press Ctrl+O and to close the nano editor press Ctrl+K.
The following image shows the after changes information.
To apply the above changes, we need to restart the NGINX services. The following command is to test and restart the NGINX services.
sudo nginx -t sudo service nginx restart
Next, revisit our test page. We are using the chrome developer tools to check the network status. Following image shows the final download size.
The download size is reduced from 18 MB to 6 MB (4.4 KB (0.0044 MB) + 4.2 KB (0.0042 MB) + 4.3 KB (0.0043 MB) + 5.5 MB = 5.5129 MB).
Definitely, there is a very huge difference between before and after compression.
Now, you may ask, What about JPG file?
All the image format already uses compression. There is no need to re-compress images. If we re-compress any image, the result size may be the same or more.
We are done with GZIP compression. You may also like the following articles to improve the website and server performance.
Please login to post your valuable comments.
Get the latest vLemonn news first