Best Practices for GZIP Compression

Best Practices for GZIP Compression

Gzip compression is the process of compressing files that make up your website. When compressed, the communication between the networks and the server is much faster.

Rich Howard
Rich Howard

Founder & CEO

Because the communication between the networks and the server is much faster, you will see a significant improvement in website speed.

Gzip compression is used by most websites today in order to stay competitive. After all, your website visitors are not going to spend much time on your webpages if they take forever to load. Furthermore, Google will penalize you for having a slow loading website, and when that happens, your content marketing and SEO strategy won’t really matter much. So let’s take a look at some of the best practices for gzip compression on different web servers.


Testing is an important phase of gzip compression, so make sure you test your site before and after implementation.

GZIP Compression Best Practices for Apache

Enabling gzip compression in Apache is fairly straightforward, and there are two methods to achieve this:

  • Using mod_deflate
  • Using mod_gzip

If you are running one of Apache 2.x versions, then you should use mod_deflate encoding. As for browser compatibility, you don’t need to worry since most users today do not use Netscape 4 or other older browsers.

If you do not have the ability to control your httpd.conf file (i.e. your site is running in a shared hosting space) then you can add the following lines of script in your .htaccess file

# compress text, html, javascript, css, xml based on MIME type: 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

# Alternatively you can compress certain file types by extension:

<files *.html> SetOutputFilter DEFLATE </files>

More ideally, gzip is configured using the main httpd.conf file (you may need to uncomment a line at the top that says ‘LoadModule mode_deflate.c’:

<IfModule mod_deflate.c>
  SetOutputFilter DEFLATE # example of how to compress ONLY html, plain text and xml #
  AddOutputFilterByType DEFLATE text/plain text/html text/xml # Don't compress binaries
  SetEnvIfNoCase Request_URI .(?:exe|t?gz|zip|iso|tar|bz2|sit|rar) no-gzip dont-vary # Don't
  compress images SetEnvIfNoCase Request_URI .(?:gif|jpe?g|jpg|ico|png) no-gzip dont-vary # Don't
  compress PDFs SetEnvIfNoCase Request_URI .pdf no-gzip dont-vary # Don't compress flash files (only
  relevant if you host your own videos) SetEnvIfNoCase Request_URI .flv no-gzip dont-vary # Netscape
  4.X has some problems BrowserMatch ^Mozilla/4 gzip-only-text/html # Netscape 4.06-4.08 have some
  more problems BrowserMatch ^Mozilla/4.0[678] no-gzip # MSIE masquerades as Netscape, but it is
  fine BrowserMatch \bMSIE !no-gzip !gzip-only-text/html # Make sure proxies don't deliver the wrong
  content Header append Vary User-Agent env=!dont-vary # Setup custom deflate log DeflateFilterNote
  Input instr DeflateFilterNote Output outstr DeflateFilterNote Ratio ratio LogFormat '"%r"
  %{outstr}n/%{instr}n %{ratio}n%%' DEFLATE CustomLog logs/deflate_log DEFLATE

GZIP Compression Best Practices for Nginx Servers

Nginx server, along with Apache, account for more than 50% of the total internet traffic. The gzip directive in Nginx is equivalent of the mod_deflate encoding in Apache.

To start gzip compression, you must first adjust the worker_processes which tells the virtual server the number of cores assigned to processors which allows it to manage requests optimally.

By default /etc/nginx/nginx.conf is the Nginx configuration path. First, we must find the number of processes allocated to the web server using this command:

grep processor /proc/cpuinfo | wc –1

So here, 1 core CPU is assigned per work process. Set the work process to 1 to allow best performance:

worker_processes 1

Now to enable gzip compression, you must first create /etc/nginx/conf.d/gzip.conf by adding the following lines:

gzip on; gzip_proxied any; gzip_types text/plain text/xml text/css application/x-javascript;
gzip_vary on; gzip_disable “MSIE [1-6]\.(?!.*SV1)”;

Now restart your server. That’s it; gzip compression is now enabled on your website.

A good way to save resources (CPU resources, hard drive space) is by disabling the access_logs. Simply add this right after access_log:

access_log off;

Gzip Compression Best Practices for WordPress via Apache & plugins

Enabling gzip compression on your WordPress website is fairly simple. Download the W3 Total Cache plugin and open it from the dashboard. Scroll to Performance and open Browser Cache.

Once you have downloaded the plugin, extract it wp-content/plugins folder and activate it from the dashboard under the Plugins tab. Once done, simply go to settings and you can now enable gzip compression from the dashboard!

Here you should check on ‘Enable HTTP (gzip) compression’.

Wrapping It Up

Gzip compression is a quick win performance gain that will improve the performance of your website for very little cost. Most modern websites apply gzip to their assets and it makes sense for you to do so too. Before you apply the gzip setting, test the page load speed of your website before and after you enable gzip compression and you should notice a solid performance improvement.

View all articles

Stay up-to-date on the latest performance news and tips

We believe it is important to keep abreast of industry developments. Join our newsletter and never miss out on breaking industry news again.

* We hate spam as much as you do and promise not to bombard you with emails.