How to Enable HTTP Server gzip Compression

Perm url with updates:

How to Enable HTTP Server gzip Compression

Xah Lee, 2010-07-26

This page shows you how to add compression to Apache webserver, so that your site will be faster.

What's Compression for Web Server

When a web server respond to a page, it can first compress the page content, then send that to the browser, then the browser de-compress it then renders it in its window.

gzip compression typically will compress text so the new file is between 10% to 20% of the original. So, for html, css, php, js, etc files, it saves a lot bandwidth, which means faster for users especially those with slow connection.

The downside is that each time the server compresses a file, it have to use some CPU resource to do it. So, although the traffic output is reduced, the cpu usage increases. However, some advanced setup will pre-compress all your files, not per request.

Overall, compression is good because ultimately what matters most is for users. Less content size means faster load, and your user will be happier.

When enabling compression, typically you enable it for text files only. Because image files are already compressed, and compress again using gzip may increase the file size.

How to Check If a Web Server Has Compression Enabled?

When a browser request a page, it may send the following header to the server:

Accept-Encoding: compress, gzip

This means the browser can accept response that's compressed using the “compress” or “gzip” compression methods. When server receives this header, and if the server is configured to send compressed files, then it'll send the result compressed using one of the method. And, in the server's response header, it will contain a line like this:

Content-Encoding: gzip

to indicate the output is a compressed by gzip method.

So, to check if a server is sending compressed output, you can look for the header the server sent.

One easy way to see the response header is by using Firefox's Web Developer Toolbar, “Information ‣ View Response Headers”. Check if it sends the header “Content-Encoding:”.

Another Firefox add-ons you can use is Live HTTP Headers.

Otherwise, you can online based tools. e.g.: and (for the latter, you need to manually enter a accep-encoding in the request first)

How to Enable Compression in Apache?

There are 2 Apache modues that do compression: mod_gzip and mod_deflate.

Apache 1.3 uses mod_gzip. Apache 2.0 uses mod_deflate. (note: Apache 1.3 is no longer supported.)

With mod_deflate, add the following line in your “.htaccess” file:

AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/xml
AddOutputFilterByType DEFLATE text/css

You can add more file types to the line. For example, you might want to add the following

AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE application/rss+xml
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE application/x-javascript

Shared Hosting and

If you are using 1and1 shared hosting, they don't support compression as of 2010-07.


If you are understand PHP, you can use it to compress the file before it sends to browser, because PHP can compress your file and send HTTP headers. See: Source.


Popular posts from this blog

11 Years of Writing About Emacs

does md5 creates more randomness?

Google Code shutting down, future of ErgoEmacs