Web缓存原理(二)
基于Squid的反向代理加速实现
Squid是一个更专用的代理服务器,性能和效率会比Apache的mod_proxy高很多。
如果需要combined格式日志补丁:
http://www.squid-cache.org/mail-archive/squid-dev/200301/0164.html
squid的编译:
./configure –enable-useragent-log –enable-referer-log –enable-default-err-language=Simplify_Chinese –enable-err-languages=”Simplify_Chinese English” –disable-internal-dns
make
#make install
#cd /usr/local/squid
make dir cache
chown squid.squid *
vi /usr/local/squid/etc/squid.conf
在/etc/hosts中:加入内部的DNS解析,比如:
192.168.0.4 www.chedong.com
192.168.0.4 news.chedong.com
192.168.0.3 bbs.chedong.com
———————cut here———————————-
# visible name
visible_hostname cache.example.com
# cache config: space use 1G and memory use 256M
cache_dir ufs /usr/local/squid/cache 1024 16 256
cache_mem 256 MB
cache_effective_user squid
cache_effective_group squid
http_port 80
httpd_accel_host virtual
httpd_accel_single_host off
httpd_accel_port 80
httpd_accel_uses_host_header on
httpd_accel_with_proxy on
# accelerater my domain only
acl acceleratedHostA dstdomain .example1.com
acl acceleratedHostB dstdomain .example2.com
acl acceleratedHostC dstdomain .example3.com
# accelerater http protocol on port 80
acl acceleratedProtocol protocol HTTP
acl acceleratedPort port 80
# access arc
acl all src 0.0.0.0/0.0.0.0
# Allow requests when they are to the accelerated machine AND to the
# right port with right protocol
http_access allow acceleratedProtocol acceleratedPort acceleratedHostA
http_access allow acceleratedProtocol acceleratedPort acceleratedHostB
http_access allow acceleratedProtocol acceleratedPort acceleratedHostC
# logging
emulate_httpd_log on
cache_store_log none
# manager
acl manager proto cache_object
http_access allow manager all
cachemgr_passwd pass all
———————-cut here———————————
创建缓存目录:
/usr/local/squid/sbin/squid -z
启动squid
/usr/local/squid/sbin/squid
停止squid:
/usr/local/squid/sbin/squid -k shutdown
启用新配置:
/usr/local/squid/sbin/squid -k reconfig
通过crontab每天0点截断/轮循日志:
0 0 * * * (/usr/local/squid/sbin/squid -k rotate)
可缓存的动态页面设计
什么样的页面能够比较好的被缓存服务器缓存呢?如果返回内容的HTTP HEADER中有”Last-Modified”和”Expires”相关声明,比如:
Last-Modified: Wed, 14 May 2003 13:06:17 GMT
Expires: Fri, 16 Jun 2003 13:06:17 GMT
前端缓存服务器在期间会将生成的页面缓存在本地:硬盘或者内存中,直至上述页面过期。
因此,一个可缓存的页面:
- 页面必须包含Last-Modified: 标记
一般纯静态页面本身都会有Last-Modified信息,动态页面需要通过函数强制加上,比如在PHP中:
// always modified now
header(“Last-Modified: ” . gmdate(“D, d M Y H:i:s”) . ” GMT”); - 必须有Expires或Cache-Control: max-age标记设置页面的过期时间:
对于静态页面,通过apache的mod_expires根据页面的MIME类型设置缓存周期:比如图片缺省是1个月,HTML页面缺省是2天等。
<IfModule mod_expires.c>
ExpiresActive on
ExpiresByType image/gif “access plus 1 month”
ExpiresByType text/css “now plus 2 day”
ExpiresDefault “now plus 1 day”
</IfModule>对于动态页面,则可以直接通过写入HTTP返回的头信息,比如对于新闻首页index.php可以是20分钟,而对于具体的一条新闻页面可能是1天后过期。比如:在php中加入了1个月后过期:
// Expires one month later
header(“Expires: ” .gmdate (“D, d M Y H:i:s”, time() + 3600 * 24 * 30). ” GMT”); - 如果服务器端有基于HTTP的认证,必须有Cache-Control: public标记,允许前台
ASP应用的缓存改造 首先在公用的包含文件中(比如include.asp)加入以下公用函数: <%
‘ Set Expires Header in minutes
Function SetExpiresHeader(ByVal minutes)
‘ set Page Last-Modified Header:
‘ Converts date (19991022 11:08:38) to http form (Fri, 22 Oct 1999 12:08:38 GMT)
Response.AddHeader “Last-Modified”, DateToHTTPDate(Now())
‘ The Page Expires in Minutes
Response.Expires = minutes
‘ Set cache control to externel applications
Response.CacheControl = “public”
End Function
‘ Converts date (19991022 11:08:38) to http form (Fri, 22 Oct 1999 12:08:38 GMT)
Function DateToHTTPDate(ByVal OleDATE)
Const GMTdiff = #08:00:00#
OleDATE = OleDATE – GMTdiff
DateToHTTPDate = engWeekDayName(OleDATE) & _
“, ” & Right(“0″ & Day(OleDATE),2) & ” ” & engMonthName(OleDATE) & _
” ” & Year(OleDATE) & ” ” & Right(“0″ & Hour(OleDATE),2) & _
“:” & Right(“0″ & Minute(OleDATE),2) & “:” & Right(“0″ & Second(OleDATE),2) & ” GMT”
End Function
Function engWeekDayName(dt)
Dim Out
Select Case WeekDay(dt,1)
Case 1:Out=”Sun”
Case 2:Out=”Mon”
Case 3:Out=”Tue”
Case 4:Out=”Wed”
Case 5:Out=”Thu”
Case 6:Out=”Fri”
Case 7:Out=”Sat”
End Select
engWeekDayName = Out
End Function
Function engMonthName(dt)
Dim Out
Select Case Month(dt)
Case 1:Out=”Jan”
Case 2:Out=”Feb”
Case 3:Out=”Mar”
Case 4:Out=”Apr”
Case 5:Out=”May”
Case 6:Out=”Jun”
Case 7:Out=”Jul”
Case 8:Out=”Aug”
Case 9:Out=”Sep”
Case 10:Out=”Oct”
Case 11:Out=”Nov”
Case 12:Out=”Dec”
End Select
engMonthName = Out
End Function
%>
然后在具体的页面中,比如index.asp和news.asp的”最上面”加入以下代码:HTTP Header
<!–#include file=”../include.asp”–>
<%
‘页面将被设置20分钟后过期
SetExpiresHeader(20)
%>
如何检查目前站点页面的可缓存性(Cacheablility)呢?可以参考以下2个站点上的工具:
http://www.ircache.net/cgi-bin/cacheability.py
