gzip | LinuxHostSupport Linux Tutorials and Guides Mon, 05 Dec 2022 13:16:57 +0000 en-US hourly 1 https://wordpress.org/?v=6.6.1 Improve Website Performance Using gzip and Nginx on Ubuntu 22.04 https://linuxhostsupport.com/blog/improve-website-performance-using-gzip-and-nginx-on-ubuntu-22-04/ https://linuxhostsupport.com/blog/improve-website-performance-using-gzip-and-nginx-on-ubuntu-22-04/#respond Thu, 01 Dec 2022 18:30:00 +0000 https://linuxhostsupport.com/blog/?p=1728 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 […]

The post Improve Website Performance Using gzip and Nginx on Ubuntu 22.04 appeared first on LinuxHostSupport.

]]>
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.

The post Improve Website Performance Using gzip and Nginx on Ubuntu 22.04 appeared first on LinuxHostSupport.

]]>
https://linuxhostsupport.com/blog/improve-website-performance-using-gzip-and-nginx-on-ubuntu-22-04/feed/ 0
How to enable GZIP compression in Nginx https://linuxhostsupport.com/blog/how-to-enable-gzip-compression-in-nginx/ https://linuxhostsupport.com/blog/how-to-enable-gzip-compression-in-nginx/#respond Wed, 19 Apr 2017 12:41:45 +0000 https://linuxhostsupport.com/blog/?p=64 We’ll show you, How to enable GZIP compression in Nginx. Lately, it’s all about optimizing your website and making it as fast as possible. Especially since search engines like Google consider page load time as a ranking factor. And, of course, the load time is a huge factor in user experience. Aside from using a […]

The post How to enable GZIP compression in Nginx appeared first on LinuxHostSupport.

]]>
We’ll show you, How to enable GZIP compression in Nginx. Lately, it’s all about optimizing your website and making it as fast as possible. Especially since search engines like Google consider page load time as a ranking factor. And, of course, the load time is a huge factor in user experience.

Aside from using a fast, SSD-powered VPS, you can also do some other tweaks and settings to speed up your website, like enabling gzip compression in Nginx.

Here we’ll discuss one method that will increase the transfer speed by reducing the size of the files with compression. This method will also bring up the benefit of reducing the bandwidth that is used in this process and will make the hosting cheaper to the owner (ie, you can use a hosting plan with fewer resources. The best part about – it’s easy to do and it will only take you a few minutes.

In this article, we’ll share with you how to do the above with the Nginx gzip module. As it is stated this module will help you to compress your files with the gzip compression method. It is known that this method will reduce the size of the transmitted files by half or in some cases even more.

How to enable it?

SSH into your server, and edit your nginx configuration:

[user]$ nano /etc/nginx/nginx.conf

Add or edit the following lines:

gzip on;
gzip_comp_level 2;
gzip_http_version 1.0;
gzip_proxied any;
gzip_min_length 1100;
gzip_buffers 16 8k;
gzip_types text/plain text/html text/css application/x-javascript text/xml application/xml application/xml+rss text/javascript;

# Disable for IE < 6 because there are some known problems
gzip_disable "MSIE [1-6].(?!.*SV1)";

# Add a vary header for downstream proxies to avoid sending cached gzipped files to IE6
gzip_vary on;

Test the configuration and fix errors (if any are shown):

nginx -t

Restart Nginx and verify that gzip is working properly.

service nginx restart

How to test if gzip compression is working in Nginx? Run the following curl command:

[user]$curl --header "Accept-Encoding: gzip,deflate,sdch" -I http://your-domain-name.com/
HTTP/1.1 200 OK
Server: nginx
Date: Fri, 14 Apr 2017 09:59:38 GMT
Content-Type: text/html; charset=UTF-8
Connection: keep-alive
Keep-Alive: timeout=60
Link: <http://your-domain-name.com/wp-json/>;
Content-Encoding: gzip

If you see Content-Encoding: gzip, that means your page was served using the Nginx gzip Module.

Another way to test this is using an online gzip test. There are many “gzip checkers” out there, so use whichever one you want. You can even check and verify if gzip is enabled using multiple methods, including curl and several online tools.

That’s it. Your server should now be configured to use gzip compression and your websites should be noticeably faster.

Of course, you don’t have to do any of this if you use one of our Service Configuration & Optimization Services, in which case you can simply ask our expert Linux admins to enable gzip compression for you. They are available 24×7 and will take care of your request immediately.

PS. If you liked this post, please share it with your friends on the social networks using the buttons on the left or simply leave a reply below. Thanks.

The post How to enable GZIP compression in Nginx appeared first on LinuxHostSupport.

]]>
https://linuxhostsupport.com/blog/how-to-enable-gzip-compression-in-nginx/feed/ 0