The gzip data compression software is cross-platform and widely used in web development and web projects as a go-to compression solution. It can be easily activated and operated via the command-line tool, helping you save storage space and improve system performance.

How does gzip work?

Gzip — short for ‘GNU zip’ — is based on the freely available Deflate algorithm, which combines the LZ77 (Lempel-Ziv 77) compression method with Huffman coding. Using these techniques, gzip files scan for duplicate data strings. If the program encounters these recurring sequences, it replaces them with a link to the string that first appears. The length of these sequences is usually limited to 32,000 bytes. If a character string does not appear in the previous 32,000 bytes, it is stored (without being compressed) in the gzip file that receives the .gz ending.

The procedure is limited to individual files, which is why the pack program, tar, is needed to create so-called tarball archives with the endings: .tar.gz or .tgz. By default, the original file is deleted after gzip compression (when applied directly to a file). However, you can disable this automatic behaviour by using the optional -k parameter. To decompress compressed files, you can either use the gunzip utility or the appropriate gzip command.

Overview of gzip syntax and commands

Even though there are graphical user interfaces available for different platforms, using gzip via commands in the terminal or command prompt is, of course, entirely possible. Even beginners will find it manageable, as gzip is a classic command-line tool optimised for this kind of usage. The general syntax follows this form:

gzip option file(s)
bash

Specifying options is not required. If the field is left blank, gzip simply uses its default settings. For example, the following simple command is enough to create a compressed version of the text file example.txt:

gzip example.txt
bash

To decompress files later or to specify instructions for the compression level, storage location, or how to handle the original file, the commands will require the appropriate specifications. The following table provides an overview of the most important gzip commands:

Option Description
-1-9 Sets the compression level (1–9), where 1 provides the fastest and least compressed result, and 9 offers the best but slowest compression; the default level is 5
-r Recursively scans the directory (including all subdirectories) and compresses or decompresses all contained files
-f Forces gzip compression and overwrites existing files with the same name if necessary
-d Decompresses the selected file into the current directory
-k Prevents the original file from being deleted
-l Displays information such as the compression ratio of a compressed file
-c Outputs the compressed file to standard output (typically the screen connected to the command line)
-q Suppresses all gzip messages
-t Tests the integrity of the compressed file
-h Lists all available options

Use cases for gzip compression

Today, gzip is nearly platform-independent and can be extended as needed, as long as the chosen project complies with the GPL license. On Linux systems, the compression tool is usually pre-installed or readily available through the package manager. The official gzip website also offers versions for macOS and Windows. You can use the software on these systems at any time to save storage space.

Another key area of application is the web. Web servers like Apache have supported gzip compression for years, and modern browsers are able to interpret compressed files and decompress them during website rendering. This is where gzip can fully shine because when enabled, the web server automatically compresses both uploaded content and dynamically generated website elements on the server. This can significantly reduce website load times as part of basic website optimisation. Meanwhile, the browser handles decompression in the background without consuming additional bandwidth. This performance boost benefits mobile users especially, and it can indirectly improve your website’s search engine ranking.

How to use gzip compression for your Apache web project

Web servers typically offer gzip compression as a module that simply needs to be enabled. Today, many web hosting providers activate this feature by default, whereas in the past that wasn’t always the case. If you’re unsure whether gzip compression is supported by your hosting provider, you can either contact them directly or initiate a manual check yourself.

On an Apache web server, you can check the module settings using a simple phpinfo() output. The HTTP_ACCEPT_ENCODING entry will show you which compression method is selected — or whether any compression is enabled at all. If gzip is available, you have three different options to use this compression tool for your needs.

Option 1: Enable gzip compression in the .htaccess File

Using the .htaccess file, you can define directory-specific settings and configure your web server in real time. This works because the configuration file—typically located in the root directory—is automatically read with every request that reaches the server. However, with some web hosting providers, the .htaccess file may be located in a different folder, hidden, or even restricted from access. In that case, your only option is to contact your hosting provider and request access. If you’re able to make changes, you can enable gzip compression using the module for the Deflate algorithm (mod_deflate) by adding the following code to the .htaccess file:

<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>
txt
Tip

You will also need your web host’s permission to use the mod_gzip and mod_deflate modules.

Option 2: Enable gzip compression via PHP

It is possible to activate gzip compression using a simple PHP statement. The downside is that the code must be added individually to each PHP file. Therefore, you should only use this option if you don’t have the necessary permissions to edit the .htaccess file. The line of code to place at the beginning of each file is as follows:

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

Option 3: Implement gzip via Plugin

In addition to these two manual solutions, there is also a variant that requires only minimal effort to set-up: activating the gzip compression using a plugin for the content management system you are using. Such useful extensions, which you can embed within a few minutes and adapt to your needs, are primarily for CMSs like WordPress, which are based on PHP. The following list contains three of the most popular plugins for the weblog software:

  • W3 Total Cache: The W3 Total Cache WordPress plugin promises a noticeably improved website performance. In addition to various caching mechanisms and dedicated mobile support, this SEO and usability suite also includes options to enable gzip compression.
  • WP Rocket: WP Rocket is another classic WordPress caching plugin. It is easy to use and activates all key performance optimisation features— including gzip compression—right from the start.
  • WP Performance Score Booster: The WP Performance Score Booster plugin has already surpassed 10,000 active installations. With this plugin, you can easily enable gzip and efficiently compress the content of your web project, including text, HTML, JavaScript, CSS, XML, and more.

How to configure gzip on an NGINX Web Server step by step

If you’re delivering your website content using an NGINX web server, you can also take advantage of gzip compression to improve your project’s load time. To do this, you simply need to configure the ngx_http_gzip_module. By default, the gzip directive—which enables or disables the compression service—is turned off.

To change this setting, open the main configuration file nginx.conf and locate the gzip directive. Replace gzip off with gzip on. The following table explains the purpose and options of some additional directives used to configure gzip compression in NGINX:

Directive Syntax Default Setting Description
gzip_buffers gzip_buffers number size; gzip_buffers 32 4k | 16 8k; Defines the number and size of buffers used during the compression process
gzip_comp_level gzip_comp_level level; gzip_comp_level 1; Sets the compression level; valid values: 1–9
gzip_min_length gzip_min_length length; gzip_min_length 20; Specifies the minimum length (in bytes) for a file to be compressed
gzip_http_version gzip_http_version version; gzip_http_version 1.1; Specifies the minimum HTTP version required for compressed responses
gzip_types gzip_types content-type; gzip_types text/html; Determines which content types should be compressed (e.g., CSS, JSON, XML)

How to test gzip compression

Once you’ve configured gzip compression for your website, you can use various tools to verify that it’s working correctly and that your web server is delivering compressed content in response to client requests. One of the most recommended tools for this is Google PageSpeed Insights. Simply enter any page from your site, and the tool will automatically analyse its content and provide a report on the page’s performance strengths and weaknesses. Among the results, you’ll find information on whether gzip compression is enabled.

Alternatively, you can perform a quick gzip test using the HTTP Compression Test at WhatsMyIP.org.

Was this article helpful?
Go to Main Menu