相关:什么是squid及其配置介绍
主要包括 expires last-modified cache-control 现在线上前面一个硬件的分发服务器,后面两台nginx服务器,在expires last-modified cache-control 等都设置无误的情况下 ,对于php文件还有rewrite后的html文件,还是每次都200,没有出现304
然后上了一台squid,在同样设置的情况下,对于rewrite后的html有效,对于php的缓存仍然无效
附php代码
$sec=1200;
$last_modified_time = intval(time() / $sec) * $sec; $etag = md5($last_modified_time); header(“Cache-Control: max-age=$sec”); header(“Expires: $sec”); header(“Last-Modified: ” . gmdate(“D, d M Y H:i:s”, $last_modified_time) . ” GMT”); header(“Expires:” . gmdate(“D, d M Y H:i:s”, $last_modified_time + $sec) . ” GMT”); 这种是通过nginx或者squid实现的,设置expires是通过php但是再次请求的时候通过nginx和squid等就可以实现304了,无需再次运行php文件
另一种通过php实现的方法如下,设置周期内同意etag,php通过etag来判断是否需要304然后中断 $last_modified_time = intval(time() / $sec) * $sec; $etag = md5($last_modified_time); header(“Cache-Control: maxage=$sec”); header(“Last-Modified: ” . gmdate(“D, d M Y H:i:s”, $last_modified_time) . ” GMT”); header(“Etag: $etag”);
if (@strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) == $last_modified_time || trim($_SERVER['HTTP_IF_NONE_MATCH']) == $etag) { header(“HTTP/1.1 304 Not Modified”); exit; }
(责任编辑:admin) |