What is GZIP Compression?

GZIP compression is a method for making files smaller. When it comes to page speed, smaller files mean faster load times.

GZIP is also a system command used to compress files. These files usually have a .gz file extension.

This article will look at how GZIP is used with HTTP servers to compress text-based files before sending them over a network. 

How does GZIP work?

GZIP is a lossless compression algorithm based on the deflate algorithm. Deflate, a predecessor of GZIP combines LZ77 and Huffman coding

A lossless compression algorithm means the compressed data is the same after decompressing.

GZIP compression works in two stages. First, the compression identifies repeated sequences of bytes and represents them with a shorter sequence. 

The shortened sequences are then assigned a number of bits based on how frequently they occur. The more frequent a sequence, the fewer bits assigned. The more rare a sequence, the more bits assigned.

This process results in a smaller compressed file than the original file.

Advantages of GZIP compression

Increased page speed is the most significant advantage of GZIP compression. Smaller files mean faster load times. GZIP can do this with relatively low CPU and memory overhead. 

Depending on the file and the compression level, GZIP can reduce the size of a file anywhere between 75% and 95%. 

Finally, GZIP compression is a widely adopted method of compressing files. You can count on modern browsers supporting GZIP compression.

How to use GZIP with a CDN

A Content Delivery Network (CDN) is a distributed network of servers serving content to users from a geographically close location relative to the user. A CDN speeds up delivery and adds redundancy to your website.

The good news is all CDNs should already have GZIP enabled. It is in their best interest to reduce the size of the files before sending them to you.

The tools below can be used to verify this is the case. If you find GZIP is not enabled, search your CDNs documentation on how to enable GZIP.

How to check GZIP Activation

There are many easy ways to determine if GZIP compression is enabled on your web server. 

Online GZIP Compression Test

An excellent online tool is https://www.giftofspeed.com/GZIP-test/. Enter the URL in question; the test will return if compression is enabled for GZIP or another method.

Check the HTTP Header

The cURL command test if a server is enabled correctly. In the command below, the options -L is used to follow potential redirects (301, 302), and the -H sends the header, letting the server know this request can accept GZIP encoding.

curl -LH "Accept-Encoding: GZIP" -I http://website.com

 

The response should contain something similar to the following.

content-encoding: GZIP

How to Enable GZIP Compression

Once we know how to verify GZIP is enabled, we can configure web servers and confirm it is working. Below is how to configure GZIP on multiple platforms.

Enabling GZIP in WordPress

Most WordPress hosting services already have GZIP enabled. You can use one of the online testing sites to verify.

If you find GZIP is not enabled by default, many WordPress plugins, such as WP Rocket, JetPack, and WP Super Cache, can be used.

A third option is the .htaccess file. You can add the text below under Red Hat/CentOS into a .htaccess file. Always be sure to verify it’s enabled using one of the methods in this article.

Enabling GZIP on Apache

On the Apache web server, enabling mod_deflate enables GZIP compression. Here is how to do it on both Ubuntu and Red Hat based systems.

Ubuntu/Debian

More than likely, the deflate module will already be loaded when you install Apache. To confirm, run the following command.

a2enmod deflate

 

This command enables the deflate module and uses the following configuration in 

/etc/apache2/mods-enabled/deflate.conf.
<IfModule mod_deflate.c>
          <IfModule mod_filter.c>
                  AddOutputFilterByType DEFLATE text/html text/plain text/xml
text/css
                  AddOutputFilterByType DEFLATE application/x-javascript
application/javascript application/ecmascript
                  AddOutputFilterByType DEFLATE application/rss+xml
                  AddOutputFilterByType DEFLATE application/xml
          </IfModule>
</IfModule>

RedHat/CentOS

For Apache 2.4 the mod_deflate module is already loaded. To configure it, add the following to the end of the httpd.conf file for a simple configuration.

<ifmodule mod_deflate.c>
AddOutputFilterByType DEFLATE text/text text/html text/plain text/xml
text/css application/x-javascript application/javascript
</ifmodule>

Restart the server for the changes to take effect.

Enabling GZIP on NGINX

To enable GZIP compression in NGINX, add the following lines to your nginx.conf. 

GZIP on; 
GZIP_vary on; 
GZIP_min_length 1024; 
GZIP_proxied expired no-cache no-store private auth; 
GZIP_types text/plain text/css text/xml text/javascript application/x-javascript application/xml; 
GZIP_disable "MSIE [1-6]\.";

Remember to restart the server for the changes to take effect.

Enabling GZIP on Microsoft IIS

For Microsoft IIS, ensure that HTTP Compression is installed. You can find an overview of how to do that here.

Place the following in either web.config or applicaitonHost.config.

<httpCompression directory="%SystemDrive%\inetpub\temp\IIS Temporary Compressed Files">
    <scheme name="br" dll="%ProgramFiles%\IIS\IIS Compression\iisbrotli.dll" />
    <scheme name="gzip" dll="%ProgramFiles%\IIS\IIS Compression\iiszlib.dll" />
    <dynamicTypes>
        <add mimeType="text/*" enabled="true" />
        <add mimeType="message/*" enabled="true" />
        <add mimeType="application/x-javascript" enabled="true" />
        <add mimeType="application/javascript" enabled="true" />
        <add mimeType="*/*" enabled="false" />
    </dynamicTypes>
    <staticTypes>
        <add mimeType="text/*" enabled="true" />
        <add mimeType="message/*" enabled="true" />
        <add mimeType="application/javascript" enabled="true" />
        <add mimeType="application/atom+xml" enabled="true" />
        <add mimeType="application/xaml+xml" enabled="true" />
        <add mimeType="image/svg+xml" enabled="true" />
        <add mimeType="*/*" enabled="false" />
    </staticTypes>
</httpCompression>

Restart the server for the changes to take effect.

Brotli vs GZIP Compression

GZIP continues to be one of the most popular compression schemes. GZIP is used to compress whole files and is supported by every modern browser.

Brotli can compress entire files, but it was designed to compress data streams, which GZIP cannot do. Brotli, having the benefit of seeing how the web has developed, can compress streamed data, think mp3s or video.

Brotli has been shown to compress data with better efficiency than GZIP and is becoming more widely supported in web browsers. Both are great solutions for increasing the speed of your website.