If a CDN-cached page has expired, when next requested by a web-browser, the CDN will simply fetch a fresh copy from the web-server, update the CDN-cached copy and forward the resource back to the web-browser in the HTTP response. There would normally be no requirement for
If-Modified-Since
headers reaching your web-server at all, though my testing has revealed that CloudFlare do still issue this header.To reduce your server load, enable the CDN to provide a level of caching beyond that already done by the web browsers, by using the
s-maxage
directive. Similar to themax-age
directive on yourCache-Control
header, however this one will be observed by CDN's (and similar services) in precedence over any other headers, while themax-age
will be observed by the web-browser clients.
Edited on 11-Nov-2014 @ 12:45pm UTC-0:
On a side note that might also have an impact, there are issues with your PHP code that may be impacting on its correct operation - your ETag and Expires header lines of code when I test them produce the following headers:
ETag: W/""
Expires: Thu, 01 Jan 1970 00:00:20 GMT
Perhaps instead try these lines in your test so that you can see your sent headers too:
<?php
$iClientCacheSecs = 20;
$iProxyCacheSecs = 60;
$dtNow = time();
$dtExpires = strtotime( sprintf( '+%s seconds', $iClientCacheSecs ));
$aHeaders = array();
$aHeaders[] = 'ETag: ' . $dtNow;
$aHeaders[] = 'Expires: ' . date( 'r', $dtExpires );
$aHeaders[] = 'Last-Modified: ' . date( 'r', $dtNow );
$aHeaders[] = sprintf( 'Cache-Control: public, max-age=%s, s-maxage=%s',
$iClientCacheSecs, $iProxyCacheSecs );
foreach( $aHeaders as $sHeader ) header( $sHeader );
echo( 'Now: ' . date( 'r', $dtNow ) . '<br />' );
foreach( $aHeaders as $sHeader ) echo( $sHeader . '<br />' );
echo( '<hr />' );
foreach( $_SERVER as $sParam => $sValue ) {
if(( strpos( $sParam, 'HTTP_CF' )) !== false )
echo( $sParam . ': ' . $sValue . '<br />' );
if(( strpos( $sParam, 'HTTP_IF' )) !== false )
echo( $sParam . ': ' . $sValue . '<br />' );
}