How to compress your webpages
Most, if not all, major web browsers are gzip enabled these days. This was introduced in HTTP/1.1 with the "Content-Encoding" header that allows clients to send and receive compressed data. There are 2 types of compressions - gzip and deflate.
If you're a web developer or you own a website, you can serve compressed pages to save on bandwidth and decrease page load times. This is done in different ways depending on your web server.
Apache 1.3 supports only mod_gzip. To compress your webpages using this module, first of all the "mod_gzip" module has to be installed and enabled.
In Apache's httpd.conf, load the module with a directive similar to this:
LoadModule gzip_module modules/mod_gzip.so
You can set other parameters like minimum file size for compression, etc. To enable compression only for files that are at least 1500 bytes in size:
To enable compression for GET and POST methods:
mod_gzip_handle_methods GET POST
Apache 2 supports mod_deflate. To compress your webpages using this module, install and enable the mod_deflate module.
In Ubuntu, to enable this module, the command is:
sudo a2enmod deflate
Then restart Apache with:
To enable compression for HTML, text, CSS and XML files:
AddOutputFilterByType DEFLATE text/html text/plain text/css text/xml
Read more at Apache's official documentation.
Nginx web server
If you're using the great Nginx web server, edit nginx.conf to include these entries:
Lighttpd web server
If you're using the other great lightweight web server lighttpd, here's how you compress files.
Edit lighttpd.conf to include this line:
server.modules += ( "mod_compress" )
Setup a cache directory and add file types:
Gzip within PHP scripts
If you're hosting your sites on a shared server, you don't have access to the actual .conf file and your web hosting provider doesn't enable any form of compression, you can still serve compressed web pages through PHP.
For that, add this line to the very top of your PHP script.
PHP will compress the content of that page on every page load.
Posted on 01 Dec 2008
If you liked this article, subscribe to our Feed, follow us on Twitter (@aruljohn) and/or join our Facebook Page.