What is Gzip Compression
Gzip is a file format and software application used on Unix and Unix-like systems to compress HTTP content before it’s served to a client. The process has been known to shrink a file by up to 80 percent, resulting in improved page load time, decreased bandwidth consumption and reduced SSL overhead (due to a decrease in the number of roundtrips during a SSL handshake).
File types associated with gzip include:
- .gz – Indicates a file extension compressed by the gzip algorithm.
- .tar file, tarball – A format used to store multiple files for archiving, but not for compression. Gzip can be used to compress .tar files.
- .tgz, .tar.gz, .gz file – Indicates a .tar file that’s been compressed by gzip.
Enabling the Gzip Command
Gzip can be applied to a number of different platforms, including those detailed below:
WordPress
You can enable gzip on WordPress by using a caching plugin that supports gzip, or by enabling gzip on your web server via the .htaccess file.
Apache
To enable gzip on an Apache web server, add the gzip compression commands inside the mod_deflate
module at the end of the .htaccess file.
For example:
<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 </IfModule>
NGINX
To enable gzip on NGINX, the following code must be added to the nginx.conf file:
gzip on; gzip_disable "MSIE [1-6]\.(?!.*SV1)"; gzip_vary on; gzip_types text/plain text/css text/javascript image/svg+xml image/x-icon application/javascript application/x-javascript;
Verifying Gzip Activation
After enabling gzip, the next step is to verify that it’s compressing your outbound files. This is done either by using any number of online tools, or by checking HTTP response headers in a browser, e.g., by making sure that in the Network > Headers tab in Google Chrome, Gzip is displayed under the Content-Encoding header.
Additionally, it’s possible to check HTTP response headers using cURL, by entering in the following command:
curl -H "Accept-Encoding: gzip" -I https://sitename.com
In the response, gzip should be listed in the Content-Encoding header:
HTTP/1.1 200 OK Server: nginx Date: Mon, 21 Jul 2014 01:12:36 GMT Content-Type: text/html; charset=UTF-8… Vary: Accept-Encoding Content-Encoding: gzip
Using Gzip in Conjunction with a CDN
Almost all content delivery networks (CDNs) provide automated file compression with gzip. This relieves you of the need to verify that compression is indeed taking place.
Additionally, CDNs typically support a number of front-end optimization techniques that can be used in conjunction with gzip to further shrink outgoing file size.
These include minification, an optimization process in which elements such as white spaces and repeated variable names are trimmed from a file’s code, potentially reducing its size by half before compression. Most CDNs automatically minify much of a site’s content on-the-fly, including all JavaScript, HTML and CSS.
While minification before gzipping a file might seem redundant, it’s been shown to shrink tarfile size by an additional 5-10%. Because CDNs perform both minification and gzip compression, they minimize the size of the code files delivered to your users, significantly reducing page load time.