What is GZIP compression?
NicolasBrondinBernard
Enabling gzip on your web server, sure, but what exactly is it for?

Article published on 06/11/2023, last updated on 10/08/2026
If you use website performance analysis tools, such as Google Lighthouse for example, you've surely come across this piece of advice:
Enable gzip compression on your web server to reduce the size of HTTP responses
But what exactly does this mean?
Why compress?
Even though our network infrastructures are becoming increasingly powerful, the performance of our machines in terms of number of calculations performed per second is evolving much more rapidly.
This is what Moore's law predicted
This notably means that, in terms of performance, it is now more efficient to lose a few milliseconds compressing data before sending it, and decompressing it upon arrival, than to send it raw.
Is this always the case?
Like any compression method, there are certain edge cases where compression isn't efficient enough to be worthwhile, but this is very rare!
If you're not familiar with the concepts of lossless compression, I invite you to read our previous article to get acquainted with certain concepts.
Particularly the concept of compression ratio, which varies depending on the algorithm and the data being compressed
The usefulness of gzip
You're surely familiar with the concept of a compressed archive called "zip," and indeed gzip gets its name from this technology, standing for "GNU zip."
But be careful, because these are two very different technologies.
A zip file is an archive, which contains one or more individually compressed files, grouped together in a "folder." There are many compatible compression algorithms for creating a zip archive.
Gzip is different, because it's a compression/decompression software for single files, but also for data streams, which makes it a fantastic tool for processing data coming directly from the network.
Hence its use in the web world
And the compression that gzip offers is not insignificant, as we're talking about an average compression ratio of around 40%!
The algorithm behind gzip
The Gzip software uses the DEFLATE algorithm, which is itself a combination of the LZ77 algorithm and Huffman coding.
We won't go into the details of this algorithm's implementation, as it would be too lengthy, but we will discuss the main principles and their advantages.
If you want to understand the full implementation, I recommend this series of videos!
LZ77 is a (lossless) compression algorithm based on a dictionary of patterns.
This means that the algorithm will scan through the file's binary data, index each newly discovered pattern in a dictionary, and replace the pattern with its index in the dictionary within the compressed file.
The file is scanned using a sliding window that moves through each character, and associates it with the n preceding characters to detect whether an existing pattern exists, with n greater than or equal to 1.
So the smallest pattern size will be two characters
This compression method has a major advantage: there's no need to have the entire file to begin decompression.
Since the dictionary is located at the beginning of the compressed file, and pattern detection is linear (character by character), it's enough to link each index to its corresponding pattern in the dictionary to retrieve the original file.
Enabling gzip compression on your site
Client side
To enable compression of HTTP responses, the client and server need to work hand in hand to agree on a compression method to use.
To tell the server to compress the data, the client (browser) must add a specific header to all requests, like this:
"Accept-Encoding": gzip
Fortunately, all modern web browsers add this header by default, so you won't need to do anything.
Server side
The server, for its part, must compress the data contained in the response, but it must also indicate to the client that the response has been compressed, with the following response header:
"Content-Encoding": gzip
Nginx
To enable gzip compression on Nginx, you'll need to add the following directive to the nginx.conf file:
gzip on;
By default, nginx only compresses the MIME type text/html; to extend this compression, follow the official documentation
Apache
To enable gzip compression on Apache (2+), you'll first need to enable the deflate module and restart the service:
sudo a2enmod deflate
service apache2 restart
Then add the following directive to the apache2.conf file:
#/etc/apache2/apache2.conf
#...
<IfModule mod_deflate.c>
# Compress HTML, CSS, JavaScript, Text, XML and fonts
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE application/rss+xml
AddOutputFilterByType DEFLATE application/vnd.ms-fontobject
AddOutputFilterByType DEFLATE application/x-font
AddOutputFilterByType DEFLATE application/x-font-opentype
AddOutputFilterByType DEFLATE application/x-font-otf
AddOutputFilterByType DEFLATE application/x-font-truetype
AddOutputFilterByType DEFLATE application/x-font-ttf
AddOutputFilterByType DEFLATE application/x-javascript
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE font/opentype
AddOutputFilterByType DEFLATE font/otf
AddOutputFilterByType DEFLATE font/ttf
AddOutputFilterByType DEFLATE image/svg+xml
AddOutputFilterByType DEFLATE image/x-icon
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/javascript
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/xml
# Level of compression (highest compression level)
DeflateCompressionLevel 9
# Do not compress certain file types
SetEnvIfNoCase Request_URI \.(?:gif|jpe?g|png)$ no-gzip dont-vary
SetEnvIfNoCase Request_URI \.(?:exe|t?gz|zip|bz2|sit|rar)$ no-gzip dont-vary
SetEnvIfNoCase Request_URI \.pdf$ no-gzip dont-vary
# Make sure proxies don't deliver the wrong content
Header append Vary User-Agent env=!dont-vary
</IfModule>
To note
Be careful, using gzip doesn't rule out the need to minify your source files; compression doesn't replace minification, they are complementary!
And keep in mind that while gzip is only designed to compress individual files, it's possible to pair it with a TAR archive to create a compressed archive (.tar.gz)
Complete courses, exercises and certificates to really learn programming!
4.8 average rating
No comments yet