A website’s performance depends on many factors, and choosing a suitable web server is one of them. You can choose from many web servers, like Apache, LiteSpeed, Nginx, etc.
Nginx is an open-source web server, it was initially developed by Igor Sysoev and released in October 2004. In Nginx, gzip compression can significantly reduce the size of transmitted data to website visitors.
Modern web browsers support GZIP compression by default. However, we need to configure our server to serve the compressed resources to our website visitors properly. Without a proper configuration, it could make your server load higher and even slower. This article will show you how to improve website performance using GZIP and Nginx on Ubuntu 22.04.
Prerequisites
- Ubuntu 22.04
- SSH access with root privileges or a regular system user with sudo privileges
What is GZIP Compression
GZIP or GNU zip is an open-source algorithm for file compression. GZIP compresses your website resources, such as Javascript and CSS files, while serving requests to the web browsers. It compresses text files effectively, while image files are not compressed because they have some built-in compression.
How to Enable GZIP Compression
To enable compression in Nginx, simply include the following directives in your nginx.conf file, or comment them out if you already have the lines.
gzip on; gzip_vary on; gzip_comp_level 6; gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
Save and close the file, then verify Nginx for any syntax errors:
$ sudo nginx -t
You should get the following output:
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful
Next, restart the Nginx service using the following command:
$ sudo systemctl restart nginx
Then check the status.
$ sudo systemctl status nginx
You should see the following output:
● nginx.service - A high performance web server and a reverse proxy server Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled) Active: active (running) since Mon 2022-09-19 17:51:13 WIB; 1s ago Docs: man:nginx(8) Process: 1249555 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS) Process: 1249574 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=0/SUCCESS) Main PID: 1249579 (nginx) Tasks: 3 (limit: 4532) Memory: 4.4M CGroup: /system.slice/nginx.service ├─1249579 nginx: master process /usr/sbin/nginx -g daemon on; master_process on; ├─1249580 nginx: worker process └─1249581 nginx: worker process Sep 19 17:51:12 home systemd[1]: Starting A high performance web server and a reverse proxy server... Sep 19 17:51:13 home systemd[1]: Started A high performance web server and a reverse proxy server.
The following are explanations of our directives to enable and configure gzip in nginx.
gzip on;
The directive above should be turned on to enable gzipping of responses.
In gzip_type directive, you can add another MIME type because, by default, Nginx compresses responses only with MIME type text/thml.
gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
To set gzip compression level, you can add gzip_comp_level directive. The value should be between 1 and 9. The higher the value, the higher the compression. Please note that the most compressed data usually requires more work to compress or decompress, so if you have it set fairly high on a busy website, you may see the difference in your CPU usage.
gzip_comp_level 6;
Another directive, gzip_vary is optional. It is used to enable or disable inserting the “Vary: Accept-Encoding” response header. This header inform the browsers if the client can handle the compressed version of the website or not, especially when your Nginx server is behind CDN or another reverse caching server. If gzip_vary is enabled, you will see vary: Accept-Encoding in the header response, like the following.
HTTP/2 200 server: nginx date: Mon, 19 Sep 2022 10:25:15 GMT content-type: text/html content-length: 14512 last-modified: Sat, 22 Jan 2022 13:53:28 GMT vary: Accept-Encoding etag: "61ec0c58-38b0" accept-ranges: bytes
Since compression happens at runtime, it can add considerable processing overhead, which can negatively affect your server performance. You can lower your gzip_comp_level if you think your CPU usage is abnormally higher after enabling gzip in Nginx.
How to Verify GZIP is working?
There are several ways to verify whether the gzip compression is working or not. You can use an online tool like Google PageSpeed Insights to check it, or use your browser’s developer tools, or even simply use the curl shell command as follow:
$ curl -I -H 'Accept-Encoding: gzip,deflate' https://www.rosehosting.com/
The command above will show you an output like this:
HTTP/2 200 server: nginx date: Mon, 19 Sep 2022 09:52:28 GMT content-type: text/html last-modified: Sat, 22 Jan 2022 13:53:28 GMT etag: W/"61ec0c58-38b0" content-encoding: gzip
As we can see in the output, the website is compressed using gzip (content-encoding: gzip).
Compare GZIP and Plain Text
After enabling gzip compression, you can compare the files transmitted by nginx to you by simply running these commands.
$ curl -s --output uncompressed https://www.rosehosting.com/ $ curl -s --output compressed -H 'Accept-Encoding: gzip,deflate' https://www.rosehosting.com/
Then, run ‘ls -lh’ or ‘ll -h’ command to see the file size.
As seen above, the compressed file has a lower file size. The lower the file size, the faster your website is and the higher number of concurrent visitors your server can handle.
Congratulations! You have successfully improved your website performance using gzip and Nginx on Ubuntu 22.04.
Of course, if you are one of our Ubuntu Hosting customers, you don’t have to improve your website performance using gzip and Nginx yourself – simply ask our admins, sit back, and relax. Our admins will improve your website performance using gzip and Nginx for you immediately without any additional fee, along with many useful optimizations that we can do for you. Improving your website performance using gzip and Nginx is not just about the installation; we can help you with optimizing your website if you have a VPS with us.
If you liked this post about how to improve your website performance using gzip and Nginx on Ubuntu 22.04, please share it with your friends on the social networks using the share buttons below, or simply leave a comment in the comments section. Thanks.