Apache and mod_headers for cache control and increased performance

kevin360

Joined: 2003-09-06
Posts: 50
Posted: Tue, 2007-07-10 00:45

I tried searching around on the forums but didn't come up with much about this, so I thought I'd post what I've just now been testing and see what others think of it. Maybe there are reasons I shouldn't be doing this and others can point it out to me!

Long story short, I have mod_headers installed and use it to set the cache-control headers for other parts of my website. With G2 though I was having problems in the admin section, the pages would get cached and wouldn't show the most up to date changes that I had just made, got a little frustrating because I wasn't sure if changes were made or I hadn't made them, etc - so I put in Apache's config to turn off caching for the gallery2 directory.

Looking at the logs and thinking about how the images were pulled up I wanted to have the cache headers turned on for the jpg thumbnails though, but at first I couldn't get that to work. What I realized was I have mod_rewrite also turned on, so any request for a thumbnail jpg actually got re-written by apache for a request like:

main.php?g2_view=core.DownloadItem&g2_itemId=37065&g2_serialNumber=4&g2_fileName=image.jpg

This meant that my files match section in apache config file:

<FilesMatch "\.jpg$">
  Header set Cache-control "max-age=3600, must-revalidate"
</FilesMatch>

Never matched the request. Turns out the <Files> or <FilesMatch> directive only matches the main part of the url, it won't try to match the parameters passed to the url. So what I did was I made a copy of main.php to main2.php and edited the .htaccess for the url rewrite so that any request for a jpg actually called main2.php:

RewriteRule .   /gallery2/main2.php?g2_view=core.DownloadItem&g2_itemId=%1&....

And then I could change apache's config so that any request for main2 would set the cache headers:

<Files "*main2\.php*">
  Header set Cache-control "max-age=3600, must-revalidate"
</Files>

Using FF along with the FasterFox extension I did some page timings. These times are after the pages have been cached and going back to the cached pages - these are the average of 10 viewings:

G2 no acceleration, apache telling browser not to cache
1.04s

G2 no acceleration, apache setting cache headers to 'must-revalidate'
0.76s

G2 medium accel, apache telling browser not to cache
0.78s

G2 medium accel, apache setting cache headers to 'must-revalidate'
0.72s

G2 high accel, apache telling browser not to cache
0.50s

G2 high accel, apache setting cache headers to 'must-revalidate'
0.39s

So that's getting me a decent performance increase. The question I have though, can anyone think of any reason I shouldn't do the main2.php file setup like I'm doing? Will that cause any strangeness in other parts of gallery?