httpd-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Nicholas Sherlock <n.sherl...@gmail.com>
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 
definition:

CacheRoot C:/temp
CacheEnable disk /

And this PHP file called test.php in /:

<?php

/* 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).
  */
$etag="\"ComputedETag\"";

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?
$fp=fopen('log.txt','a');

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");
	fclose($fp);
	exit(); //Don't generate or send the body
}	

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

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
Etag="ComputedETag"

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:

If-None-Match="ComputedETag"
Cache-Control=max-age=0

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 
CacheIgnoreCacheControl.

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
Etag="ComputedETag"
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 
PHP/5.3.0

Cheers,
Nicholas Sherlock


---------------------------------------------------------------------
The official User-To-User support forum of the Apache HTTP Server Project.
See <URL:http://httpd.apache.org/userslist.html> for more info.
To unsubscribe, e-mail: users-unsubscribe@httpd.apache.org
   "   from the digest: users-digest-unsubscribe@httpd.apache.org
For additional commands, e-mail: users-help@httpd.apache.org


Mime
View raw message