---------------------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
可缓存的静态页面设计
甚么样的页面可以对照好的被缓存办事器缓存呢?假如前往内容的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
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
附:SQUID功能测尝尝验
phpMan.php是一个基于php的man page server,每一个man page需求挪用后台的man号令和良多页面格局化东西,体系负载对照高,供应了Cache Friendly的URL,以下是针对一样的页面的功能测试材料:
测试情况:Redhat 8 on Cyrix 266 / 192M Mem
测试法式:利用apache的ab(apache benchmark):
测试前提:恳求50次,并发50个毗连
测试项目:直接经由过程apache 1.3 (80端口) vs squid 2.5(8000端口:减速80端口)
测试1:无CACHE的80端口静态输入:
ab -n 100 -c 10 http://www.chedong.com:81/phpMan.php/man/kill/1
This is ApacheBench, Version 1.3d <$Revision: 1.1 $> apache-1.3
Copyright (c) 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Copyright (c) 1998-2001 The Apache Group, http://www.apache.org/
Benchmarking localhost (be patient).....done
Server Software: Apache/1.3.23
Server Hostname: localhost
Server Port: 80
Concurrency Level: 5
Time taken for tests: 63.164 seconds
Complete requests: 50
Failed requests: 0
Broken pipe errors: 0
Total transferred: 245900 bytes
HTML transferred: 232750 bytes
Requests per second: 0.79 [#/sec] (mean)
Time per request: 6316.40 [ms] (mean)
Time per request: 1263.28 [ms] (mean, across all concurrent requests)
Transfer rate: 3.89 [Kbytes/sec] received
Connnection Times (ms)
min mean[+/-sd] median max
Connect: 0 29 106.1 0 553
Processing: 2942 6016 1845.4 6227 10796
Waiting: 2941 5999 1850.7 6226 10795
Total: 2942 6045 1825.9 6227 10796
Percentage of the requests served within a certain time (ms)
50% 6227
66% 7069
75% 7190
80% 7474
90% 8195
95% 8898
98% 9721
99% 10796
100% 10796 (last request)
测试2:SQUID缓存输入
/home/apache/bin/ab -n50 -c5 "http://localhost:8000/phpMan.php/man/kill/1"
This is ApacheBench, Version 1.3d <$Revision: 1.1 $> apache-1.3
Copyright (c) 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Copyright (c) 1998-2001 The Apache Group, http://www.apache.org/
Benchmarking localhost (be patient).....done
Server Software: Apache/1.3.23
Server Hostname: localhost
Server Port: 8000
Concurrency Level: 5
Time taken for tests: 4.265 seconds
Complete requests: 50
Failed requests: 0
Broken pipe errors: 0
Total transferred: 248043 bytes
HTML transferred: 232750 bytes
Requests per second: 11.72 [#/sec] (mean)
Time per request: 426.50 [ms] (mean)
Time per request: 85.30 [ms] (mean, across all concurrent requests)
Transfer rate: 58.16 [Kbytes/sec] received
Connnection Times (ms)
min mean[+/-sd] median max
Connect: 0 1 9.5 0 68
Processing: 7 83 537.4 7 3808
Waiting: 5 81 529.1 6 3748
Total: 7 84 547.0 7 3876
Percentage of the requests served within a certain time (ms)
50% 7
66% 7
75% 7
80% 7
90% 7
95% 7
98% 8
99% 3876
100% 3876 (last request)