The gzip data com­pres­sion software is cross-platform and widely used in web de­vel­op­ment and web projects as a go-to com­pres­sion solution. It can be easily activated and operated via the command-line tool, helping you save storage space and improve system per­for­mance.

How does gzip work?

Gzip — short for “GNU zip” — is based on the freely available Deflate algorithm, which combines the LZ77 (Lempel-Ziv 77) com­pres­sion method with Huffman coding. Using these tech­niques, gzip files scan for duplicate data strings. If the program en­coun­ters 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 com­pressed) in the gzip file that receives the .gz ending.

The procedure is limited to in­di­vid­ual 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 com­pres­sion (when applied directly to a file). However, you can disable this automatic behavior by using the optional -k parameter. To de­com­press com­pressed files, you can either use the gunzip utility or the ap­pro­pri­ate gzip command.

Overview of gzip syntax and commands

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

gzip option file(s)
bash

Spec­i­fy­ing 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 com­pressed version of the text file example.txt:

gzip example.txt
bash

To de­com­press files later or to specify in­struc­tions for the com­pres­sion level, storage location, or how to handle the original file, the commands will require the ap­pro­pri­ate spec­i­fi­ca­tions. The following table provides an overview of the most important gzip commands:

Option De­scrip­tion
-1-9 Sets the com­pres­sion level (1–9), where 1 provides the fastest and least com­pressed result, and 9 offers the best but slowest com­pres­sion; the default level is 5
-r Re­cur­sive­ly scans the directory (including all sub­di­rec­to­ries) and com­press­es or de­com­press­es all contained files
-f Forces gzip com­pres­sion and over­writes existing files with the same name if necessary
-d De­com­press­es the selected file into the current directory
-k Prevents the original file from being deleted
-l Displays in­for­ma­tion such as the com­pres­sion ratio of a com­pressed file
-c Outputs the com­pressed file to standard output (typically the screen connected to the command line)
-q Sup­press­es all gzip messages
-t Tests the integrity of the com­pressed file
-h Lists all available options

Use cases for gzip com­pres­sion

Today, gzip is nearly platform-in­de­pen­dent and can be extended as needed, as long as the chosen project complies with the GPL license. On Linux systems, the com­pres­sion 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 ap­pli­ca­tion is the web. Web servers like Apache have supported gzip com­pres­sion for years, and modern browsers are able to interpret com­pressed files and de­com­press them during website rendering. This is where gzip can fully shine because when enabled, the web server au­to­mat­i­cal­ly com­press­es both uploaded content and dy­nam­i­cal­ly generated website elements on the server. This can sig­nif­i­cant­ly reduce website load times as part of basic website op­ti­miza­tion. Meanwhile, the browser handles de­com­pres­sion in the back­ground without consuming ad­di­tion­al bandwidth. This per­for­mance boost benefits mobile users es­pe­cial­ly, and it can in­di­rect­ly improve your website’s search engine ranking.

How to use gzip com­pres­sion for your Apache web project

Web servers typically offer gzip com­pres­sion 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 com­pres­sion 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 com­pres­sion method is selected — or whether any com­pres­sion is enabled at all. If gzip is available, you have three different options to use this com­pres­sion tool for your needs.

Option 1: Enable gzip com­pres­sion 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 con­fig­u­ra­tion file—typically located in the root directory—is au­to­mat­i­cal­ly 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 re­strict­ed 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 com­pres­sion 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 per­mis­sion to use the mod_gzip and mod_deflate modules.

Option 2: Enable gzip com­pres­sion via PHP

It is possible to activate gzip com­pres­sion using a simple PHP statement. The downside is that the code must be added in­di­vid­u­al­ly to each PHP file. Therefore, you should only use this option if you don’t have the necessary per­mis­sions 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: ac­ti­vat­ing the gzip com­pres­sion using a plugin for the content man­age­ment system you are using. Such useful ex­ten­sions, 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 no­tice­ably improved website per­for­mance. In addition to various caching mech­a­nisms and dedicated mobile support, this SEO and usability suite also includes options to enable gzip com­pres­sion.
  • WP Rocket: WP Rocket is another classic WordPress caching plugin. It is easy to use and activates all key per­for­mance op­ti­miza­tion features— including gzip com­pres­sion—right from the start.
  • WP Per­for­mance Score Booster: The WP Per­for­mance Score Booster plugin has already surpassed 10,000 active in­stal­la­tions. With this plugin, you can easily enable gzip and ef­fi­cient­ly 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 de­liv­er­ing your website content using an NGINX web server, you can also take advantage of gzip com­pres­sion 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 com­pres­sion service—is turned off.

To change this setting, open the main con­fig­u­ra­tion file nginx.conf and locate the gzip directive. Replace gzip off with gzip on. The following table explains the purpose and options of some ad­di­tion­al di­rec­tives used to configure gzip com­pres­sion in NGINX:

Directive Syntax Default Setting De­scrip­tion
gzip_buffers gzip_buffers number size; gzip_buffers 32 4k | 16 8k; Defines the number and size of buffers used during the com­pres­sion process
gzip_comp_level gzip_comp_level level; gzip_comp_level 1; Sets the com­pres­sion 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 com­pressed
gzip_http_version gzip_http_version version; gzip_http_version 1.1; Specifies the minimum HTTP version required for com­pressed responses
gzip_types gzip_types content-type; gzip_types text/html; De­ter­mines which content types should be com­pressed (e.g., CSS, JSON, XML)

How to test gzip com­pres­sion

Once you’ve con­fig­ured gzip com­pres­sion for your website, you can use various tools to verify that it’s working correctly and that your web server is de­liv­er­ing com­pressed content in response to client requests. One of the most rec­om­mend­ed tools for this is Google PageSpeed Insights. Simply enter any page from your site, and the tool will au­to­mat­i­cal­ly analyze its content and provide a report on the page’s per­for­mance strengths and weak­ness­es. Among the results, you’ll find in­for­ma­tion on whether gzip com­pres­sion is enabled.

Al­ter­na­tive­ly, you can perform a quick gzip test using the HTTP Com­pres­sion Test at WhatsMyIP.org.

Go to Main Menu