squid缓存php动态文件

acl QUERY urlpath_regex cgi-bin \?
no_cache deny QUERY

这段代码会不cache 所有带 cgi-bin 和 ? 的url path.表面看来,如果url 里面不带?还是可以被缓存的。可惜实际并非如此,squid 会根据header读取httpd的声明信息,例如生成时间等信息;实际上.php的页面不返回 last-modified 信息,因此很难被cache。

通过增加 php 的last-modified header强制 squid 的cache功能:

header(“Last-Modified: ” . gmdate(“D, d M Y H:i:s”) . ” GMT”);

header要放在所有.php文件中去。换而言之,就是要
1. 在 include.php中,构造一个当前时间转换为最近那次5分钟/n分钟划分的时间函数
2. 把这个时间函数的结果返回到 header
3. 在所有页面中调用这个 include

以上方法本人已经测试通过.

分别是加last-modified前台的日志

============================

由于网站的访问量越来越大,用户播放视频,都要从web服务器拿文件,而且又是动态连接,要调用一个php-cgi进程,这样的话很浪费资源,所以要加缓存服务器。但是问题来了,squid 默认是不缓存动态页面的,google 了半天,发现很多人的文章没有一个说到正点上的。最后自己不停的做测试,看squid.conf.documented,把问题解决了,所以就记录下来,以便以后大家遇到同样的问题。好解决。 列子:

http://www.nginxs.com/nginx/Grec.php?id=eric&b.php?=aaa

首选用 curl 抓 head头。

nginx $> curl -I http://www.nginxs.com/nginx/Grec.php
HTTP/1.1 200 OK
Server: nginx
Date: Fri, 27 Aug 2010 06:49:43 GMT
Content-Type: text/html; charset=utf-8
Connection: keep-alive
Vary: Accept-Encoding
X-Powered-By: PHP/5.2.13
Set-Cookie: PHPSESSID=2d4523a7c6a5a54dbb20f64f3bc04be3; path=/
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0 Pragma: no-cache

看红字部分,然后在看 squid.conf.documented 对应的部分

#       options: override-expire
#                override-lastmod
#                reload-into-ims
#                ignore-reload
#                ignore-no-cache
#                ignore-no-store
#                ignore-must-revalidate
#                ignore-private
#                ignore-auth
#                refresh-ims
#

把 Cache-Control 的信息,squid对应有违规则 那么我们修改 squid.conf

nginx $> vim /usr/local/squid/etc/squid.conf
# 添加如下:
acl nginx urlpath_regex ^/nginx/Grec.php
acl QUERY urlpath_regex cgi-bin \? \.php
no_cache allow nginx
no_cache deny QUERY

refresh_pattern -i ^http://www.nginxs.com/nginx/Grec.php      1440    50%     10080   override-expire override-lastmod reload-into-ims ignore-reload ignore-no-cache ignore-no-store ignore-must-revalidate

保存 重启 squid 然后访问网站查看 日志(参考:http://docs.haohtml.com/Squid/chap07.html#a7

nginx $> tailf /usr/local/squid/var/logs/access.log
1282112874.863    124 xxx.xxx.xx.xxx TCP_MEM_HIT/200 225990 GET http://www.nginxs.com/nginx/Grec.php? - NONE/- image/jpeg
1282112874.919    176 xxx.xxx.xx.xxx TCP_MEM_HIT/200 155673 GET http://www.nginxs.com/nginx/Grec.php? - NONE/- image/jpeg
1282112874.946    203 xxx.xxx.xx.xxx TCP_MEM_HIT/200 237069 GET http://www.nginxs.com/nginx/Grec.php? - NONE/- image/jpeg
1282112874.948    225 xxx.xxx.xx.xxx TCP_MEM_HIT/200 228811 GET http://www.nginxs.com/nginx/Grec.php? - NONE/- image/jpeg
1282112874.973    230 xxx.xxx.xx.xxx TCP_MEM_HIT/200 195373 GET http://www.nginxs.com/nginx/Grec.php? - NONE/- image/jpeg
1282112874.985    242 xxx.xxx.xx.xxx TCP_MEM_HIT/200 241978 GET http://www.nginxs.com/nginx/Grec.php? - NONE/- image/jpeg
1282112875.021    197 xxx.xxx.xx.xxx TCP_MEM_HIT/200 201420 GET http://www.nginxs.com/nginx/Grec.php? - NONE/- image/jpeg
1282112875.032    185 xxx.xxx.xx.xxx TCP_MEM_HIT/200 259924 GET http://www.nginxs.com/nginx/Grec.php? - NONE/- image/jpeg
1282112875.046    160 xxx.xxx.xx.xxx TCP_MEM_HIT/200 226011 GET http://www.nginxs.com/nginx/Grec.php? - NONE/- image/jpeg
1282112875.071    176 xxx.xxx.xx.xxx TCP_MEM_HIT/200 256656 GET http://www.nginxs.com/nginx/Grec.php? - NONE/- image/jpeg
1282112875.106    177 xxx.xxx.xx.xxx TCP_MEM_HIT/200 223641 GET http://www.nginxs.com/nginx/Grec.php? - NONE/- image/jpeg
1282112875.125    167 xxx.xxx.xx.xxx TCP_MEM_HIT/200 223972 GET http://www.nginxs.com/nginx/Grec.php? - NONE/- image/jpeg

本文出自 “linuxer” 博客,请务必保留此出处http://deidara.blog.51cto.com/400447/385074

清除指定squid缓存文件的脚本:http://blog.haohtml.com/archives/3189

Leave a Reply