Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

I'm really confused by all that caching stuff. I'm trying to setup mod_expires to reduce the number of HTTP Requests from my website to the server.

I did well so far, I installed mod_expires and wrote a little .conf file from the instructions on http://httpd.apache.org/docs/2.0/mod/mod_expires.html.

Now, for instance, all my .png, .gif, .jpeg files have a Cache-Control header. My expected result was, that the browser won't do any GET Request within the time period (given from the Cache-Control value). But it does, every single file fires a request and receives HTTP 304 not Modified.

That is the wrong behavior isn't it ? It should load that files from the internal cache. One thing I don't understand is, that the browser sends a Response header: Cache-Control: max-age=0. Should it be like that?

Here is an complete example Request + Response headers for a single .png file:

Request

Host             dev-mgg.localdomain
User-Agent     Mozilla/5.0 (Windows; U; Windows NT 5.1; de; rv:1.9.2.8) Gecko/20100722     Firefox/3.6.8
Accept         image/png,image/*;q=0.8,*/*;q=0.5
Accept-Language  de-de,de;q=0.8,en-us;q=0.5,en;q=0.3
Accept-Encoding  gzip,deflate
Accept-Charset   ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive     115
Connection     keep-alive
Referer       http://dev-mgg.localdomain/css/global/icons.css?18224
Cookie         IR_SQLPwdStore=; IR_SQLUser=sysadm
If-Modified-Since   Thu, 24 Jul 2008 06:24:11 GMT
If-None-Match   "4010127-3c4-452bf1aefd8c0"
Cache-Control   max-age=0

Response

Date         Mon, 02 Aug 2010 14:00:28 GMT
Server     Apache/2.2.9 (Debian) PHP/5.2.6-1+lenny8 with Suhosin-Patch mod_perl/2.0.4     Perl/v5.10.0
Connection   Keep-Alive
Keep-Alive   timeout=15, max=59
Etag         "4010127-3c4-452bf1aefd8c0"
Expires   Mon, 02 Aug 2010 14:04:28 GMT
Cache-Control   max-age=240
share|improve this question

2 Answers 2

up vote 0 down vote accepted

and welcome to the wonderful world of web (servers|browsers)!

How web browsers ultimately behave, that's almost up to the web browser. Some browsers will not request already cached images at all, some will. Some will request them only if tuned for private browsing or something similar.

And if you have a dynamic web page, most likely your browser will still try to load the static images, too, even if the browser would have been told earlier that it does not need to come back for the images. In those cases the browser will try to request for an image, and the server will respond back 304 Not Modified.

But don't worry, that is the only response your browser will get back. No more data is transferred for that particular request, so from the resource side of view that is a very light thing.

Sometimes it helps if you serve the images from a different domain name (say, static.yourdomain.com instead of www.yourdomain.com), but rest assured, the browsers are like Terminator 2, they'll be back sooner than you thought. A simple Ctrl+F5 or Ctrl+Shift+R is enough to force your typical browser to refresh EVERYTHING.

(Please correct me if I'm wrong.)

share|improve this answer
    
Hi there, thanks for the respond. Tho your answer does not really help me at all. Most of the things I already mentioned, and I'm not fairly new to this topic. It's just about the caching, I ran tests on Chrome and Firefox, both showed the same behavior, just returning 304. I got a lot of pictures and each HTTP request is so much overhead, I really want to avoid those for static images. So you're saying one can't reach that with headers at all? What about the strange request header, which contains the cache-control: max-age 0 ? I also added the private flag, no success. –  jAndy Aug 2 '10 at 16:54
    
It needs to be cache-control: public if you want it to be stored. Private is never cached. You may want to increase the max-age, that can be overridden with mod_headers and its unset/set functions. Still I claim you will see visits from your browsers. Good luck in your journey to caching :) –  Janne Pikkarainen Aug 2 '10 at 17:09
    
whoopa, I meant Public. I added that into the cache-control header with mod_headers. But it also does show no effect on chrome/firefox. I'm really disappointed here, I guess I'll try to use ANT to setup the headers for me, but I'm afraid it will not do any magic that I'm not doing right now. –  jAndy Aug 2 '10 at 17:15

The Cache-Control max-age=240 you set in server with mod_expires is a little bit low to make tests, it tells browser to keeps stuff in cache for only 4 minutes (240 seconds).

So the 1st thing you could do is to increase such value to something longer like a week so you can do test without running.

#Set caching on image files for 11 months
<filesMatch "\.(ico|gif|jpg|png)$">
  ExpiresActive On
  ExpiresDefault "access plus 1 week"
  Header append Cache-Control "public"
</filesMatch>

Anyway your test was performed after you hit thebrowser relaod page button. Once you do that that browser sends a request to with Cache-Control max-age=0 telling server to give it back the entire resource again. You should test if caching works NOT by forcing a page relaod but simply:

  • by navigating to another website and then come back to the test page.

  • Or you could close the browser and than open it again on the same page.

Doing the test I listed above you should see browser not asking for the same respurce again because server told browser it must keep the resource in cache and take it out form there for 1 week.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.