Forum Moderators: coopster

Message Too Old, No Replies

Cache control to return 200/304/404

Need help, works locally but not on server

         

tomda

2:49 pm on Oct 13, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Did an update of my blog and I have issue with my cache control.

I am calling images using php (e.g. http://www.example.com/wp-content/?img=p85-100x100.jpg ) then a small PHP script will either return 404 (not found), 200 (OK) or 304 (not modified)

Script works well on local machine as I get a 304 after refreshing page but on server I always get a 200.

On localhost I get this


[b]Response Headers[/b]
Date Tue, 13 Oct 2009 14:29:29 GMT
Server Apache/2.2.9 (Win32) DAV/2 mod_ssl/2.2.9 OpenSSL/0.9.8i mod_autoindex_color PHP/5.2.6
X-Powered-By PHP/5.2.6
Content-Length 3820
Expires Wed, 13 Oct 2010 14:29:22 GMT
Accept-Encoding gzip, deflate
Cache-Control public, max-age=31536000, s-maxage=31536000
Last-Modified Fri, 17 Apr 2009 08:56:45 GMT
Etag b894c26a6053bc42f738a84c74116848
Content-Encoding gzip
Vary Accept-Encoding
Content-Type image/jpeg
Content-Language en-IS
[b]Request Headers[/b]
Host localhost
User-Agent Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.9.0.14) Gecko/2009082707 Firefox/3.0.14
Accept text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language en-gb,en;q=0.5
Accept-Encoding gzip,deflate
Accept-Charset ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive 300
Connection keep-alive
If-Modified-Since Fri, 17 Apr 2009 08:56:45 GMT
If-None-Match b894c26a6053bc42f738a84c74116848
Cache-Control max-age=0

On server I get this


[b]Response Headers[/b]
Date Tue, 13 Oct 2009 14:28:50 GMT
Server Apache/2.2.11 (Unix) mod_ssl/2.2.11 OpenSSL/0.9.8i DAV/2 mod_auth_passthrough/2.1 mod_bwlimited/1.4 FrontPage/5.0.2.2635
X-Powered-By PHP/5.2.9
Expires Wed, 13 Oct 2010 14:29:14 GMT
Accept-Encoding gzip, deflate
Cache-Control public, max-age=31536000, s-maxage=31536000
Etag b894c26a6053bc42f738a84c74116848
Content-Encoding gzip
Vary Accept-Encoding
Content-Length 3959
Last-Modified Wed, 07 Oct 2009 19:05:35 GMT
Content-Type image/jpeg
Content-Language en-IS
Proxy-Connection keep-alive
[b]Request Headers[/b]
Host www.example.com
User-Agent Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.9.0.14) Gecko/2009082707 Firefox/3.0.14
Accept text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language en-gb,en;q=0.5
Accept-Encoding gzip,deflate
Accept-Charset ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive 300
Proxy-Connection keep-alive

As you can see, the request header on server doesn't have a If-Modified-Since and If-None-Match when calling photos/?img=img_name.jpg. However, it is there when calling directly photos/img_name.jpg. Any idea why ?

And my PHP script is actually using these

if ((isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) && strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE'])==$last_modified_time) ¦¦ (isset($_SERVER['HTTP_IF_NONE_MATCH']) && trim($_SERVER['HTTP_IF_NONE_MATCH'])==$etag)) {
header("HTTP/1.1 304 Not Modified");
exit();
} else {
header("Content-type: ".$mime);
header("Content-length: ".filesize($img_path));
header("Expires: ".gmdate("D, d M Y H:i:s", time() + $offset)." GMT");
header("Accept-Encoding: gzip, deflate");
header("Cache-Control: public, max-age=".$offset.", s-maxage=".$offset."");
header("Last-Modified: ".gmdate("D, d M Y H:i:s", $last_modified_time)." GMT");
header("ETag: ".$etag);

ob_start('ob_gzhandler');

$fn = @fopen("./".$img, "r");
if($fn) {fpassthru($fn);}
exit();
}

Your help would be greatly appreciated

tomda

10:49 am on Oct 14, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hey guys, would love to get an expert advice on this...

As you can see above, response header from my server really sucks (no expires date, no gzip compression - only etags is returned).
www.example.com/photo/test.jpg

DateWed, 14 Oct 2009 10:17:55 GMT
ServerApache/2.2.11 (Unix) mod_ssl/2.2.11 OpenSSL/0.9.8i DAV/2 mod_auth_passthrough/2.1 mod_bwlimited/1.4 FrontPage/5.0.2.2635
Last-ModifiedWed, 07 Oct 2009 12:42:09 GMT
Etag"5b74223-a3fd-47557ae41e040"
Accept-Rangesbytes
Content-Length41981
Content-Typeimage/jpeg
Content-Languageen-IS
X-CacheMISS from proxy2.unon.org
Via1.0 proxy2.unon.org:3128 (squid/2.6.STABLE12)

Since I don't have mod_headers (shared server), I am just trying to call the images throught PHP $_GET variable using the script above (see extract - set far expire date, etags and gzip content).
www.example.com/photo/?img=test.jpg

MAIN ISSUE - $_SERVER['HTTP_IF_MODIFIED_SINCE'] and $_SERVER['HTTP_IF_NONE_MATCH'] doesn't exist ! And I have no idea why ?

MY SOLUTION - Using curl to get HTTP response header data and fetch Last-Modified and Etags from response header => working on it now, almost done need to finalise regex to extract data

BEST SOLUTION - Using curl to get HTTP request header data.

*************

I have three questions:

A/ Is this method dumb ? From previous discussion I had on WW forum, this method is the only one acceptable when mod_headers is not available; anyone using it ? Please share your experience.

B/ Can I fetch REQUEST header data using Curl (I am only able to get RESPONSE header currently) ?

C/ Strangely, it seems that my pages load much faster when calling images directly (at first load). I am afraid that my PHP script + curl will use too much CPU resources and make things worst (knowing that I have appr. 30 images per pages)

Thanks

coopster

11:56 pm on Oct 29, 2009 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Looks like it is running through a proxy to me. That may be your issue.