What Is Gzip? How to Enable Gzip Compression

What Is Gzip? How to Enable Gzip Compression

gzip, used as a data compression tool, was developed by Jean-Loup Gailly and Mark Adler, who set out to build a strong alternative to compress, the program written for Unix.

It is a technology defined in the POSIX standard that compresses files using the adaptive Lempel-Ziv algorithm for its functions and behavior. The team behind it did good work, and we should note that this is now the most widely used compression method. You can test your own site in the section below.

gzip technology saves up to 80% on your site.

Test GZIP on Your Site

Compression Test

See this value for your own site straight away: enter your address and your Compression Test result, plus insights specific to this tool, will appear here.

Free, no sign-up needed; the analysis also personalizes every one of our tools for your site.

How does gzip work?

gzip is short for ‘GNU ZIP‘, and it rests on the freely available deflate algorithm, a variant of the LZ77 data compression method (Lempel-Ziv 77), together with Huffman coding. Using those techniques, gzip scans files for repeated strings of data. When the program meets those repeated sequences it replaces them with a reference to the first occurrence. The length of these sequences is usually limited to 32,000 bytes. If a string does not appear in the previous 32,000 bytes, it is stored uncompressed in the gzip file, which takes the .gz ending. The procedure works on single files, so the packing program tar has to create so-called tarball archives with the .tar.gz and .tgz endings. That is the technical side and the story of it. No need to stretch it further. Now we will go through how to enable it on your server and the settings you need on your site.

How to use gzip compression on your Apache web project

Web servers usually offer the practical compression process as a module you have to enable . These days many web hosting providers share this feature, though that was not the case in the past. The reason was probably that compression takes extra processor power. If you are not sure whether gzip is authorised by your server, you can contact your server administrator directly, or run a manual query instead. On an Apache web server, for example, check the server module settings with a simple phpinfo() output. The HTTP_ACCEPT_ENCODING information tells you which compression method was chosen.

If gzip is installed on your server, you have a few options for putting the compression helper to use.

Enabling gzip compression in the .htaccess file

To do it across your whole site (it applies to the current directory and all subdirectories) and to configure your web server in real time, you can add a piece of code to a .htaccess file. The .htaccess file usually sits in the root directory and lets the configuration file be read automatically on every request that reaches the site. With some web hosting providers, though, the .htaccess file is kept in a different folder, hidden, or closed to access. In that case your only option is to contact the host and ask for access. If you can make the configuration changes, add the code below to the .htaccess file to switch on gzip compression ( mod_gzip ) or the deflate algorithm ( mod_deflate ):

<IfModule mod_gzip.c>
mod_gzip_on Yes
mod_gzip_dechunk Yes
mod_gzip_item_include file .(html?|txt|css|js|php|pl)$
mod_gzip_item_include handler ^cgi-script$
mod_gzip_item_include mime ^text/.*
mod_gzip_item_include mime ^application/x-javascript.*
mod_gzip_item_exclude mime ^image/.*
mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.*
</ifModule>
<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/xml
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE application/rss+xml
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE application/x-javascript
</IfModule>

Enabling gzip compression through PHP

You can switch compression on for your site with a simple PHP command. There is one awkward problem, though: the code has to be entered separately in every PHP document. So you should only use this option on servers where you do not have the rights to edit the .htaccess file. The line of code to place at the top of each file is this:

<?php
ob_start("ob_gzhandler");
?>

Apply gzip through a WordPress plugin

On top of those two manual solutions there is an option that takes only minimal effort to set up: you can enable gzip compression using a plugin for the content management system you use. Plugins of this kind, which you can put in place within a few minutes and adapt to your needs, mainly help speed up sites on PHP-based systems such as WordPress. The list below holds the most popular plugins for your WordPress site:

  • W3 Total Cache : the WordPress plugin W3 Total Cache promises to raise website performance tenfold. Alongside various caching mechanisms and dedicated mobile support, this SEO and usability package also lets you make a great many settings, the option to enable gzip compression among them.
  • Check and Enable GZIP compression : also built for WordPress, the most common CMS, this plugin lets you check whether gzip compression is enabled for your project. If GZIP compression is off, the plugin helps you set it up.

If you use Joomla!, the compression function comes with your website out of the box.

gzip

How to enable gzip on your NGINX web server

When you deliver the content of your site to visitors through an NGINX web server, you can use gzip to improve the loading time of your project as well. To do that you only need to configure the ngx_http_gzip_module module. By default the ‘gzip’ directives used to enable or disable the compression service are off. To change that setting, open the nginx.conf file and search for the ‘gzip’ directive with CTRL + F. Then change ‘gzip off’ to ‘gzip on’. The table below shows the meaning and the possibilities of the other directives for configuring gzip compression in NGINX:

DirectiveSyntaxDefault settingDescription
gzip_buffersgzip_buffers – number and size;gzip_buffers 32 4k, 16 8k;Sets the number and the size of the buffers used for compression
gzip_comp_levelgzip_comp_level – compression ratio;gzip_comp_level 1;Sets the compression ratio; possible values: 1–9
gzip_min_lengthgzip_min_length – minimum length;gzip_min_length 20;Sets the minimum length of the response in bytes
gzip_http_versiongzip_http_version – version number;gzip_http_version 1.1;Sets the HTTP version a request must use to be answered with a compressed response
gzip_typesgzip_types – content type;gzip_types text/html;Sets which content types compression applies to (CSS, JSON, XML and others are also possible)

Differences between GZIP and its alternatives

In the table below we go through the advantages and disadvantages of the main alternatives to gzip.

MethodAdvantagesDisadvantages
gzip– Widely used and broadly compatible
– Fast to compress and decompress
– Reasonable compression ratio
– Low memory use
– A lower compression ratio than some modern algorithms
bzip2– A better compression ratio than gzip
– Effective on large files
– Slower than gzip
– More CPU and memory use
xz– Very high compression ratio
– Ideal for large files
– Slow to compress and decompress
– High memory use
lzma– A high compression ratio close to xz
– Slightly faster than xz
– Still slower than gzip
– High CPU use
zstd– Fast to compress and decompress
– Good compression ratio
– Low memory use
– Not as effective as gzip on very large files
lz4– Very fast to compress and decompress
– Low CPU and memory use
– A lower compression ratio than the others

Test GZIP

To test gzip, enter your site address in the GZIP test tool section.

A note

Speeding your site up with gzip compression is quite effective, up to a point. A good server matters a great deal too. For a good TTFB figure we listed hosting providers in Turkey by their speed.

Published: Updated: Türk SEM Academy
Tahir Dinç

Author

Tahir Dinç

SEO Specialist · Founder of Türk SEM

I began this work in 1999 and became an ODP editor in 2004. Today I provide SEO and Google Ads services to a great many large companies. I also chair Türk SEM Group and run the companies within it.

View profile →

Related SEO Insights Articles