How to Enable Gzip in Nginx on Ubuntu?

Share your love by sharing

Website performance is the most critical factor for ranking, user engagement, and much more. Many things make a website faster and one of them is compression. Compressing files not only makes a website faster, but it also reduces your web hosting bills as small files need less bandwidth.

Configure-Gzip-in-Nginx-on-Ubuntu
Configure-Gzip-in-Nginx-on-Ubuntu

If you are using Nginx as your website, you can take the benefit of the gzip module. Nginx compresses files on the fly and as the files reach the browser, they get decompressed without any loss.

In this tutorial, we will learn how to enable gzip in Nginx on Ubuntu

Prerequisites

1. Open nginx.conf file

$ sudo nano /etc/nginx/nginx.conf

2. Scroll down to gzip settings and you will find

        ##
	# `gzip` Settings
	#
	#
	gzip on;
	gzip_disable "msie6";
	
	# gzip_vary on;
	# gzip_proxied any;
	# gzip_comp_level 6;
	# gzip_buffers 16 8k;
	# gzip_http_version 1.1;
	# gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;

3. Change it to

        ##
	# `gzip` Settings
	#
	#
	gzip on;
	gzip_disable "msie6";
	
	gzip_vary on;
	gzip_proxied any;
	gzip_comp_level 6;
	gzip_buffers 16 8k;
	gzip_http_version 1.1;
	gzip_min_length 256;
	gzip_types
  	application/atom+xml
  	application/geo+json
  	application/javascript
  	application/x-javascript
  	application/json
  	application/ld+json
  	application/manifest+json
  	application/rdf+xml
  	application/rss+xml
  	application/xhtml+xml
  	application/xml
  	font/eot
  	font/otf
  	font/ttf
  	image/svg+xml
  	text/css
  	text/javascript
  	text/plain
  	text/xml;

Note: No need to add JPEG, JPG, PNG, or any other image type because you can upload the compressed one using various tools available. Adding these will only consume server resources without having any considerable effect.

Press ctrl+x then “y” and hit the “Enter” key to save the file.

4. Compile Nginx to check errors

$ sudo nginx -t

If it is correct, you will get the following message

	"nginx: the configuration file /etc/nginx/nginx.conf syntax is ok"
	"nginx: configuration file /etc/nginx/nginx.conf test is successful"

5. Restart Nginx

$ sudo systemctl restart nginx

It’s done. You have enabled the gzip compression in Nginx on Ubuntu.

Share your love by sharing
Sourabh Verma
Sourabh Verma

I.T Engineer by profession, tech lover. Passionate for reading and writing technical stuffs. Loves to share knowledge.

Leave a Reply

Your email address will not be published. Required fields are marked *