httpd-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Nicholas Sherlock <>
Subject [users@httpd] Strange behaviour from mod_cache
Date Mon, 20 Jul 2009 07:24:36 GMT
Hi everyone,

mod_cache behaves strangely, but I'm not sure if this is expected 
behaviour or not. I have these cache-related lines in my virtual host 

CacheRoot C:/temp
CacheEnable disk /

And this PHP file called test.php in /:


/* Generate our ETag. Assume that generating the ETag is
  * a whole lot less expensive than generating the content
  * (e.g. it could be based on revision counts for documents
  * from a database).

header("Etag: $etag");
//Expires ages away
header("Expires: " . gmdate("D, d M Y H:i:s", time()
	+ 60 * 60 * 24 * 30) . " GMT");

//Let's work out what's going on, shall we?

if (isset($_SERVER['HTTP_IF_NONE_MATCH']) &&
	$_SERVER['HTTP_IF_NONE_MATCH'] == $etag) {

	/* At a users' request, the cache has been bypassed, but the
	 * document is still the same. Avoid costly response generation
	 * and waste of bandwidth by just sending not-modified.
	header('HTTP/1.0 304 Not Modified');
	fwrite($fp, date('r')." - Response: 304 Not Modified\n");
	exit(); //Don't generate or send the body

fwrite($fp, date('r')." - Response: 200. Generated document.\n");

echo "Document body goes here";


My web browser requests the document the first time, the (trimmed) 
response is:

Status=OK - 200
Date=Mon, 20 Jul 2009 07:16:05 GMT
Expires=Wed, 19 Aug 2009 07:16:05 GMT

The log performed by test.php indicates:

Mon, 20 Jul 2009 19:16:05 +1200 - Response: 200. Generated document.

So far so good. But now I press refresh in my web browser. This makes a 
conditional request for the document:


With the max-age of 0, the cache must be bypassed. That's perfectly 
fine. I understand that if I include the directive 
"CacheIgnoreCacheControl yes" in my virtual host, it will force it to 
use the cache and not pass it on to the backend. That isn't what I want, 
though, I want the backend to really validate that it is still up to 
date so it doesn't serve a stale response, so I'm not using 

Anyway, the cache passes this conditional request onto the backend, and 
the backend logs it:

Mon, 20 Jul 2009 19:16:12 +1200 - Response: 304 Not Modified

So the backend is trying to tell the client that it already has an 
up-to-date body. But the response sent to the browser by the caching 
system is:

Status=OK - 200
Date=Mon, 20 Jul 2009 07:16:12 GMT
Expires=Wed, 19 Aug 2009 07:16:12 GMT

Argh, there goes my monthly bandwidth allowance! Why isn't mod_cache 
passing the 304 response code onto the client? I have users pressing 
refresh on dynamically-generated image pages all the time and I need to 
reduce bandwidth in this usecase.

My Apache is:

Apache/2.2.11 (Win32) DAV/2 mod_ssl/2.2.11 OpenSSL/0.9.8i SVN/1.6.3 

Nicholas Sherlock

The official User-To-User support forum of the Apache HTTP Server Project.
See <URL:> for more info.
To unsubscribe, e-mail:
   "   from the digest:
For additional commands, e-mail:

View raw message