How to Enable HTTP Server gzip Compression
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:
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.
- If you are on Linux, you can use the
HEADcommand in terminal. For example,
- Use Firefox's Web Developer Toolbar, menu 〖Information ▸ View Response Headers〗. Check if it sends the header “Content-Encoding:”.
- Firefox add-on Live HTTP Headers
- Online based tools. For example, http://www.gidnetwork.com/tools/gzip-test.php and rexswain.com. (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 modules that do compression: mod_gzip and apache.org 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
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
See also: Apache HTTP Server Tutorial.
Shared Hosting and 1and1.com
If you are using 1and1 shared hosting, they don't support compression as of 2010-07.
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: 〔http://mrrena.blogspot.com/2009/01/how-to-compress-php-and-other-text.html 〕
see also: PHP Tutorial
- 〔 Best Practices for Speeding Up Your Web Site (~2010) By Yahoo.com. At http://developer.yahoo.com/performance/rules.html 〕
- List of HTTP headers
- Testing Server Performance Using Apache Benchmark Tool
- Apache HTTP Server Tutorial
- Tech Humor: Apache vs Node.js vs NGINX 📺
- Apache Rewrites History: Why is it Named “Apache”?
- Blocking Image Leechers in Apache
- nginx Tutorial
- TCP/IP Tutorial for Beginner
- Linux: Firewall Tutorial, iptables, netfilter
- Understanding Public-Key Cryptography for Beginner
- How to Diagnose Computer Networking Problems
Ask me question on patreon