How to compress your webpages

Published on December 01, 2008

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

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:

mod_gzip_minimum_file_size 1500

To enable compression for GET and POST methods:

mod_gzip_handle_methods GET POST

Apache 2

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:

/etc/init.d/apache2 restart

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:

gzip on;
gzip_http_version 1.1;
gzip_comp_level 4;
gzip_proxied any;
gzip_types text/plain text/html text/css text/javascript application/x-javascript text/xml application/xml application/xml+rss;

The above sends compressed content for HTML, CSS, JavaScript, XML and RSS files that are requested. Restart Nginx.

/etc/init.d/nginx restart

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:

compress.cache-dir = "/tmp/lighttpdcache/"
compress.filetype = ( "text/html" "text/plain","text/css", "text/xml", "text/javascript" )

Restart Lighttpd:

/etc/init.d/lighttpd restart

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.

ob_start("ob_gzhandler");

PHP will compress the content of that page on every page load.

Related Posts

If you have any questions, please contact me at arulbOsutkNiqlzziyties@gNqmaizl.bkcom. You can also post questions in our Facebook group. Thank you.

Disclaimer: Our website is supported by our users. We sometimes earn affiliate links when you click through the affiliate links on our website.

Published on December 01, 2008