Enable GZip or DEFLATE compression of text-based responses in order to reduce file size and bandwidth. Can have significant impact on the perceived speed of website page loads.
Refer also to: Virtual Host Config or .htaccess?
Compress using mod_deflate
Using DEFLATE for compression is typically preferred over GZip given its broader browser support historically. Ultimately, the amount of compression you’ll see from either module will be essentially the same, so it more comes down to what may already be enabled on your server (or which you choose to enable).
To enable compression using the mod_deflate module, use the following code (the mod_deflate module must be enabled):
Compress using mod_gzip
To enable compression using the mod_gzip module, use the following code (the mod_gzip module must be enabled):
If using a server-side language whose extension is not listed in the file extension list above, simply add its extension to the pipe-delimited list.
In More Detail
Web servers leverage gzip to compress data returned to clients. When a gzip-enabled browser sends a request, it adds ‘gzip’ to its Accept-Encoding request header.
When the web server receives the request it processes the response as normal and then checks the Accept-Encoding header to determine how to encode the response. If the requesting browser supports gzip and the server also supports gzip, it compresses each compressible resource using the gzip algorithm and returns those assets with an added Content-Encoding header, specifying that the resource has been encoded using gzip. The browser then decompresses the compressed content into its original uncompressed format before rendering it in the browser.
Note that there is some cost associated with this process. Compression is a CPU-intensive process, and the more you compress a response, the longer it will take. Given this size vs speed trade-off, gzip offers compression levels from 1 to 9, where 1 provides the fastest speed but with a lower compression ratio, and 9 offers the highest compression ratio but at a slower speed. By default, the setting is typically 6 to slightly prefer smaller file sizes over speed.