How to Enable HTTP Server gzip Compression

By Xah Lee. Date: . Last updated: .

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

What is 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.

How to Get HTTP Header?

There are many ways to see the header of server response.

How to Enable Compression in Apache?

There are 2 Apache modules 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

See also: Apache HTTP Server Tutorial .

Shared Hosting and

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

[1and1 web hosting][]

If you use 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: [ ]

see also: PHP Tutorial


JavaScript in Depth