KNOWNHOST WIKI

User Tools

Site Tools


control-panels:misc:how-to-check-gzip-compression-with-curl

How to Check Whether Gzip Compression is Working

We often have questions about testing for Gzip compression after someone tests their landing page. Gzip is most likely working. The issue is that the landing page is composed of several types of data, and .htaccess rules may have only defined certain data to be compressed. If not everything on that landing page is compressed, then it may return an erroneous result stating that compression is not enabled. We'll explain how to enable Gzip compress and how to test and confirm that it is enabled in this article.


How To Enable Gzip Compression (mod_deflate)

To enable 'Gzip compression', you must enable the mod_deflate Apache module and then add .htaccess rules like these listed below:

<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

  # Remove browser bugs (only needed for really old browsers)
  BrowserMatch ^Mozilla/4 gzip-only-text/html
  BrowserMatch ^Mozilla/4\.0[678] no-gzip
  BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
  Header append Vary User-Agent
</IfModule>

To test whether your Apache installation had mod_deflate enabled, run the following command as root via SSH:

  httpd -M | grep deflate

If this is not already enabled on your cPanel CentOS server for some reason and you are using EasyApache 4, you can enable it using the rpm command like below:

  yum install ea-apache24-mod_deflate -y

Knownhost wants your sites to perform optimally, so this module is enabled on our servers by default.

You can add the .htaccess rules in cPanel via cPanel's Optimize Website interface or with the following deprecated 1)API command from the command line when logged into the server via SSH (replace 'username' with the actual cPanel user's name):

  cpapi1 --user=username OptimizeWS optimizews all text/html

If you can find any updated/supported equivalent UAPI, then use that instead.

Notice that the .htaccess rules will Compress HTML, CSS, JavaScript, Text, XML and fonts. Any other content won't be compressed. If your site contains a lot of different types of contents, not all contents may be compressed. This will cause the landing page to show as if it were not compressed either. which explains why testing just the landing page of the site via online testing tools like Webpagespeedtest and Pingdom and the like may erroneously state that the site lacks gzip compression.


Checking Gzip Compression With Curl

The best way to check if Gzip compression is actually working is by testing those specific resources that the .htaccess rules were written for using curl like so:

  $ curl -H "Accept-Encoding: gzip" -LI http://domain.com/wp-includes/js/wplink.js
  HTTP/1.1 301 Moved Permanently
  Date: Thu, 13 Jun 2019 04:14:51 GMT
  Server: Apache
  Location: https://domain.com/wp-includes/js/wplink.js
  Content-Type: text/html; charset=iso-8859-1

  HTTP/1.1 200 OK
  Date: Thu, 13 Jun 2019 04:14:51 GMT
  Server: Apache
  Last-Modified: Wed, 20 Mar 2019 06:45:49 GMT
  Accept-Ranges: bytes
  Vary: Accept-Encoding,User-Agent
  Content-Encoding: gzip
  Content-Length: 5938
  Content-Type: application/javascript
  $ 

Notice the "Content-Encoding: gzip" returned, which shows that the page was gzipped.

The flags used in the curl command above are as follows:

  • -H, –header <header/@file> Pass custom header(s) to server
  • -L, –location Follow redirects
  • -I, –head Show document info only


Checking Gzip Compression With Wget

You can also use wget to compare a compressed file download to a non-compressed file download.

First, download the compressed file:

  wget --header='Accept-Encoding: gzip' http://domain.com/index.html

Next, download the non-compressed file:

  wget http://domain.com/index.html

Next, compare the amount of time it took overall for the compressed file and the non-compressed files to download so that you can see how much time is saved when using compression to serve files.

Additionally, you can also ask wget to save the headers:

  wget --save-headers http://domain.com

You can then check the downloaded file for the headers and confirm that compression is enabled for that mime-type.


Beware of Size Limits Required To Deflate Content

Beware, though, of the size of the file being too small for gzip! You may not see that it is gzipped if, for example, the file is smaller than 200 bytes for Litespeed servers as the default minimum file size to be gzipped by Litespeed is 200 bytes.

That's all there is to testing for compression for your site! :) Feel free to open a support ticket with your KnownHost support team if you have any questions! :)

control-panels/misc/how-to-check-gzip-compression-with-curl.txt · Last modified: 2020/06/02 13:21 by Karson N.