1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Speeding up your site (mod_expire)

Discussion in 'Apache' started by nevetS, Aug 21, 2005.

  1. #1
    So, I just fixed a major problem with a redesign I've been working on, and the solution actually has imrpoved performance implications beyond just solving my one problem.

    I thought I would share the info with you.

    The problem:

    IE6 does not cache css background images. This means that you end up with a whole bunch of hits to your web server for these background images and it causes a flickering in Internet Explorer if you are doing some sort of hovering effect. This was a painful thing for me because I have background images associated with a css based navigation menu and any time you went to a menu item the background image (an arrow) would flicker. There are also two other background images in my layout which means that for every page load from an IE6 browser, it would download several images slowing down my page load time and increasing my bandwidth usage. The problem is most apparent when the user has the setting "Check for a new page -> every time you visit the page" - which is my own personal IE setting since I do web development and I update pages frequently and check to see the results.

    The solution:
    The solution involves the use of an apache module - mod_expire. You can check to see if you have it loaded by running "./httpd -l |grep expire" from your apache bin directory. If you do not have it loaded, it comes as part of the apache distribution so you can recompile and get it in there pretty easily. You can put the solution in your httpd.conf or your virtual host conf file (requiring an apache restart) or in your .htaccess file. If you are using .htaccess, the Indexes override is required. (AllowOverride Indexes in your directory declaration).

    The code to place in your .htaccess (or configuration file) is as follows:
    
    ExpiresActive On
    ExpiresByType image/gif A2592000
    ExpiresByType image/jpeg A2592000
    ExpiresByType image/png A2592000
    
    PHP:
    This causes a header to be sent with the images that it expires three days after originally accessed. (259,200 seconds = 72 hours). When IE6 gets this header, it will cache the image.

    The implications:
    This solution is not without it's problems. Mod_expire does not have a way to specify individual files - only types of files. So by doing this, I'm saying that any image downloaded with a .gif, .jpg, or .png extension can be cached for 3 days - meaning that if I change an image on my site it can be up to 3 days before my end users see the change.

    If this is a problem for you, you can put your css background images in their own directory and place this code inside it's own directory declaration or inside a directory specific .htaccess file.

    Additionally, if you try to do this in your .htaccess file and you do not have override permissions for indexes or you do not have mod_expire installed, an error page will be produced. You'll see in your error logs the reason, but if this happens, I would very quickly remove your .htaccess file edits.

    Commentary:
    Your stats program more than likely separates visits or page views from hits and you probably get a lot of hits and don't pay much attention to the individual files getting grabbed. If you have a popular site this could save you on bandwidth but more importantly it can make your site appear quicker to your end users - of whom a large percentage probably uses IE6. I used this primarily to solve a css problem, and I'm not sure how IE deals with caching regular image content, but since I haven't seen flickering outside of CSS you probably won't see much if any benefit if your site is CSS free or if you don't have any "background: url(image.gif)" style declarations.

    For more on mod_expire, apache has documentation up at:
    http://httpd.apache.org/docs/1.3/mod/mod_expires.html (for 1.3x)
    and
    http://httpd.apache.org/docs/2.0/mod/mod_expires.html (for 2.x)
     
    nevetS, Aug 21, 2005 IP
    exam and Willy like this.
  2. exam

    exam Peon

    Messages:
    2,434
    Likes Received:
    120
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Thanks for the post Nevets, I'm bookmarking it for when I might need it :)
     
    exam, Aug 21, 2005 IP