0

I have the same PHP code on two different websites on two different pages. The code simply displays the current time, and sets a http caching header using max-age to 60 seconds. I have a CDN server. When I run the pages, I see that the CDN is caching content from Server 1, but not Server 2. I looked at the header entries in web-sniffer.net and both of them are showing max-age as 60.

  1. Are http headers dependent on the server. Since both of the websites are hosted on shared servers, but on different providers, do I have to look at anything else other than the code and .htaccess??

  2. Why is CDN caching only content from just one server. Why is it not respecting the max-age entry from the other server?

  3. When I try to actually view the header entries in the same page itself, I see HTTP_CACHE_CONTROL = max-age=0. Then, why is web-sniffer.net and curl showing max-age as 60. And which of them is correct.

Code that I have on the page:

header('Cache-Control: max-age=60, public');
echo date("F j, Y - H:i:s");
echo "<hr />";

foreach($_SERVER as $h=>$v)
if(ereg('HTTP_(.+)',$h,$hp))
    echo "<li>$h = $v</li>\n";

?>

PS: I asked a similar question on Stackoverflow, but didn't get any helpful answers. So, I thought of posting it here. The question at Stackoverflow is at: https://stackoverflow.com/questions/8022529/max-age-doesnt-work

Ctroy
  • 101
  • 1
  • The headers you set in the source code isn't as interesting as the headers actually sent by the webserver. Also; What CDN/Cache service are we talking about? – Kvisle Nov 06 '11 at 02:41
  • We are on Softlayer. They think that the issue might be with the webhost, since the same code on the other host is working. – Ctroy Nov 06 '11 at 04:36

1 Answers1

0

I was able to fix the max-age http header problem.

The CDN was not able to honor the "max-age" http header because of the presence of "Vary: Accept-Encoding" in the header. Apparently, the Accept-Encoding header was added by Apache automatically. It looks like the CDN will not be able to cache the response if the Vary: Accept-Encoding header exists.

Adding SetEnv force-no-vary in .htaccess suppressed the Vary header in response. This fixed the problem and the CDN is now honoring the max-age http header now.

For reference: http://blog.iosart.com/2004/05/26/fooling-apache/

Ctroy
  • 101
  • 1
  • `Vary: Accept-Encoding` just means that they should cache separate objects depending on what the Accept-Encoding header on the client says. If the presence of this header makes cache not work completely, it's a big bug. – Kvisle Nov 06 '11 at 11:42