Apache configuration for ETags, Gzip and Expires Header

Hi,

After digging deep into the sites and configuring my apache server ( after so many failures )
Here i came up with list of things to be done for

  1. Gziping the CSS/JS, image and flash files
  2. Removing Etags from CSS/JS and Image/flash files.
  3. Adding expires header to CSS/JS and image/flash files.

First, Go to your apache’s httpd.conf file
I my case its “C:\xampp\apache\conf\httpd.conf”

The lines beginning with # indicate that these are comments
Remove the comments for these below mentioned lines

LoadModule deflate_module modules/mod_deflate.so
LoadModule expires_module modules/mod_expires.so
LoadModule headers_module modules/mod_headers.so

Add the below thing to remove the ETags. This can be added at the end

FileETag None

To Add expires header to all the files that are requested from the server

ExpiresActive On
ExpiresDefault "access plus 300 seconds"
<Directory "/myProject/webResources">
    Options FollowSymLinks MultiViews
    AllowOverride All
    Order allow,deny
    Allow from all
    ExpiresByType text/html "access plus 1 day"
    ExpiresByType text/css "access plus 1 day"
    ExpiresByType text/javascript "access plus 1 day"
    ExpiresByType image/gif "access plus 1 day"
    ExpiresByType image/jpg "access plus 1 day"
    ExpiresByType image/png "access plus 1 day"
    ExpiresByType application/x-shockwave-flash "access plus 1 day"
</Directory>

Note: Here “myProject” is the folder where my web pages are present. i.e “C:\xampp\htdocs\myProject”. Where “webResources” is the directory where my css, js, image, flash files are present. For all these files I am adding expires header of one day. Means these files can be can be cached for the entire day in the local browser. In other words these files are not changed at least for one day.

To Gzip the CSS and JS files that are present in the “webResources” folder

<Location "/sprintCommunity/webResources">
# Insert filter
SetOutputFilter DEFLATE

# Netscape 4.x has some problems...
BrowserMatch ^Mozilla/4 gzip-only-text/html

# Netscape 4.06-4.08 have some more problems
BrowserMatch ^Mozilla/4\.0[678] no-gzip

# MSIE masquerades as Netscape, but it is fine
# BrowserMatch \bMSIE !no-gzip !gzip-only-text/html

# NOTE: Due to a bug in mod_setenvif up to Apache 2.0.48
# the above regex won't work. You can use the following
# workaround to get the desired effect:
BrowserMatch \bMSI[E] !no-gzip !gzip-only-text/html

# Don't compress images
SetEnvIfNoCase Request_URI \
\.(?:gif|jpe?g|png)$ no-gzip dont-vary

# Make sure proxies don't deliver the wrong content
Header append Vary User-Agent env=!dont-vary
</Location>

Please post a comment if this was useful to you. Thanks

Advertisements

18 comments

  1. The gzip part worked on my server but the expires headers did not. Hopefully another website can help me figure out the reason why. Thanks.

  2. Nice post. Really help full

    But google page speed suggests that a 30day expiry time is good practice.

    The gzip compression didnt work on my server, not sure why.

    1. Entity tags (ETags) are a mechanism that web servers and browsers use to determine whether the component in the browser’s cache matches the one on the origin server. The problem with ETags is that they typically are constructed using attributes that make them unique to a specific server hosting a site. ETags won’t match when a browser gets the original component from one server and later tries to validate that component on a different server—a situation that is all too common on web sites that use a cluster of servers to handle requests.

      As we use expires header (more info above in the original blog) we can avoid this over head. Thus loading resources from the server faster without doing the ETag check.

  3. Very good post.It is very useful for me.But In last part of code what is “/sprintCommunity/”. Please let me know.

  4. 1.We currently have static file caching for Apache headers.However its not perfect.
    2.We need to work on a solution involving Etags header (like a checksum) to ensure the webserver realizes a new version is available and then forces a file refresh.
    3.So that even if browser initiates a request, server knows whether to really send the file back or not based on the ETag.

    Please suggest me any solution on this at the earliest.

    Thanks in advance.

    1. you can do this by setting the expire header
      ExpiresDefault "access plus 30 minutes"

      Also don’t forget to enable this ExpiresActive
      # enable expirations
      ExpiresActive On

      1. # enable expirations
        ExpiresActive On

        Options FollowSymLinks MultiViews
        AllowOverride All
        Order allow,deny
        Allow from all
        ExpiresDefault A300

        ExpiresDefault “access plus 30 minutes”

        ExpiresDefault “access plus 30 minutes”

        i have added the above mentioned values and on top of this they have asked me to add etags also. so please let me know how to proceed further and where to add Etags??

  5. i dont have that FileETag None in httpd.conf file so should i add it?? If need to add how to add it please let me know…..

  6. Hi this is really helpful but in my site ETag has off I want to just on that for which i am writting FileETag MTime Size but this is going to enable.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s