• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

Swift Guides

Howtos, Tutorials & Guides

  • Home
  • Servers
  • Applications
  • Opinions
  • Search
You are here: Home / Servers / How to Enable Gzip in Nginx on Ubuntu?

How to Enable Gzip in Nginx on Ubuntu?

May 20, 2024 by Sourabh Verma

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.

Improve Website performance with Gzip

Gzip compresses the website elements and when any visitor visits your website, the compress data is sent to visitor’s device and unzipped. As we all know compress data use less bandwidth which significantly improves website speed.

Table of Contents

  • Improve Website performance with Gzip
    • Prerequisites
  • How to Configure Gzip compression in Nginx

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

Prerequisites

  • Ubuntu server with initial configuration done
  • A sudo user
  • Ubuntu server with LEMP configured. If you don’t have one, follow our guide to install and configure LEMP in Ubuntu.

How to Configure Gzip compression in Nginx

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.

Category: Servers Tagged In: Nginx, Server, Ubuntu

Like my work? Don't Forget to Share

Share on Facebook Share on Twitter Share on LinkedIn Share on Reddit Share on WhatsApp Share via Email

Hi, welcome to SwiftGuides! With over 12 years of experience in Linux, SysAdmin, databases, cloud computing, and other IT-related areas, I make sure publish easy-to-follow, 100% working tutorials. I hope you like my work. Thanks!

Reader Interactions

Leave a Reply Cancel reply

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

Primary Sidebar

Stay With Us

Follow on Facebook Follow on Twitter Follow on LinkedIn Follow on Reddit Subscribe on YouTube Follow on Instagram

SwiftGuides: Howtos, Tutorials & Guides © 2025 ยท All Rights Reserved

  • Contact Us
  • Privacy Policy
  • Disclaimer