Compressing HTML, CSS, Javascript using Apache There are two methods to available in apache to handle compression of webpage contents. They are mod_gzip and mod_deflate . Usually deflate comes pre-installed on servers. We can install both in our apache server. before we install, lets check is it availble in your server,

In order to check whether these apache directives are installed or not, we can create a test php file with calling function the phpinfo() function. From that we can see "Loaded Modules" setting in the "apache2handler" header.

From that we can see whether its installed or not. in the case of deflate, we can see "mod_deflate" under that section.

Install mod_deflate in your server

a2enmod deflate

the restart your apache with the following command

/etc/init.d/apache2 restart

We can enable this feature using .httacess file in the root directory

## Apache2 deflate support if available
## Important note: mod_headers is required for correct functioning across proxies.

AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css text/javascript application/javascript application/x-javascript
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4\.[0678] no-gzip
BrowserMatch \bMSIE !no-gzip

Header append Vary User-Agent env=!dont-vary
# The following is to disable compression for actions. The reason being is that these
# may offer direct downloads which (since the initial request comes in as text/html and headers
# get changed in the script) get double compressed and become unusable when downloaded by IE.
SetEnvIfNoCase Request_URI action\/* no-gzip dont-vary
SetEnvIfNoCase Request_URI actions\/* no-gzip dont-vary

This is a simple version for the usage of mod_deflate.c in .httacces

 AddOutputFilterByType DEFLATE application/x-javascript text/css text/html text/xml

The another option is mod_gzip, it is usually 4 -6 times faster than deflate. But its needs too much server load compared to deflate. So its advised to use mod_gzip on low-traffic sites and in large traffic sites, its better to use deflate. The settings for mod_zip is following. its can be given in a .htaccess file.

# Turn on mod_gzip if available

    mod_gzip_on yes
    mod_gzip_dechunk yes
    mod_gzip_keep_workfiles No
    mod_gzip_minimum_file_size 1000
    mod_gzip_maximum_file_size 1000000
    mod_gzip_maximum_inmem_size 1000000
    mod_gzip_item_include mime ^text/.* 
    mod_gzip_item_include mime ^application/javascript$
    mod_gzip_item_include mime ^application/x-javascript$
    # Exclude old browsers and images since IE has trouble with this
    mod_gzip_item_exclude reqheader "User-Agent: .*Mozilla/4\..*\["
    mod_gzip_item_exclude mime ^image/.*


Post a Comment

Please put your comments here. your questions, your suggestions, also what went wrong with me.