Chinaunix首页 | 论坛 | 博客
  • 博客访问: 482860
  • 博文数量: 127
  • 博客积分: 7010
  • 博客等级: 少将
  • 技术积分: 1972
  • 用 户 组: 普通用户
  • 注册时间: 2006-12-01 12:42
文章分类

全部博文(127)

文章存档

2009年(33)

2008年(94)

我的朋友

分类: LINUX

2008-04-08 09:20:28

文件:squid-2.6.STABLE181.tar.bz2
一.安装
#tar -zxvf squid-2.6.STABLE181.tar.bz2
#cd squid-2.6
#configure --prefix=/usr/local/squid --enable-dlmalloc --with-pthreads --enable-poll --disable-internal-dns --enable-stacktrace --enable-removal-policies="heap,lru" --enable-delay-pools --enable-storeio="aufs,coss,diskd,ufs"
#make
#make install

二,配置
squid的配置文件:

1./usr/local/squid/etc/squid.conf  
visible_hostname localhost                #你的主机名可以是localhost
http_port 10.122.89.104:80 vhost vport    #你需要做 squid服务器的IP及端口号
http_port 10.122.89.222:80 vhost vport
icp_port 0
cache_mem 900 MB                    #squid  所使用的内存
cache_swap_low 90                   #缓存最低使用
cache_swap_high 95
maximum_object_size 20000 KB        #当大于200000K 时不对文件进
maximum_object_size_in_memory 4096 KB     #放入内存的最小大小
cache_dir ufs /home/tmp 1000 16 256

access_log /usr/local/squid/var/logs/access.log
cache_log /usr/local/squid/var/logs/cache.log
emulate_httpd_log on                     #日志
pid_filename /usr/local/squid/var/logs/squid.pid


acl Safe_ports port 80
http_access deny !Safe_ports
tcp_recv_bufsize 65535 bytes


negative_ttl 1 second
cache_store_log none
cache_vary on
logfile_rotate 0                  #日志不保存
acl all src 0.0.0.0/0.0.0.0   
acl QUERY urlpath_regex cgi-bin .html .php .cgi .avi .wmv .rm .ram .mpg .mpeg .zip .exe .rar                        # 对这一些扩名的文件不缓存
cache deny QUERY

 
acl Safe_ports port 80          #  开放80端口允许进来。
acl Safe_ports port 21          #  开放21端口允许进来。
http_access deny !Safe_ports
tcp_recv_bufsize 65535 bytes
refresh_pattern -i .shtml 1440 100% 129600 reload-into-ims   #对shtml 进行缓存
refresh_pattern -i .gif 1440 100% 129600 reload-into-ims
refresh_pattern -i .swf 1440 100% 129600 reload-into-ims
refresh_pattern -i .png 1440 100% 129600 reload-into-ims
refresh_pattern -i .bmp 1440 100% 129600 reload-into-ims
refresh_pattern -i .css 1440 100% 129600 reload-into-ims
refresh_pattern -i .jpg 1440 100% 129600 reload-into-ims
refresh_pattern -i .txt 1440 100% 129600 reload-into-ims
refresh_pattern -i .rhtml 1440 100% 129600 reload-into-ims
refresh_pattern -i .xml 1440 100% 129600 reload-into-ims

cache_peer 127.0.0.1 parent 8080 0 no-query originserver login=PASS
cache_effective_user nobody
cache_effective_group nobody

三,完成
#mkdir -p /home/tmp
#chmod 777 /home/tmp

#touch  /usr/local/squid/var/logs/access.log
#chmod 777 /usr/local/squid/var/logs/access.log

usr/local/squid/sbin/squid -z   创建缓存
/usr/local/squid/sbin/squid       启动squid
/usr/local/squid/sbin/squid -k shutdown 重启squid


四.测试
查看你的日志文档。
#more /usr/local/squid/var/logs/access.log | grep TCP_MEM_HIT
该指令可以看到在squid运行过程中,有那些文件被squid缓存到内存中,并返回给访问用户。
#more /usr/local/squid/var/logs/access.log | grep TCP_HIT
该指令可以看到在squid运行过程中,有那些文件被squid缓存到cache目录中,并返回给访问用户。
#more /usr/local/squid/var/logs/access.log | grep TCP_MISS
该指令可以看到在squid运行过程中,有那些文件没有被squid缓存,而是现重原始服务器获取并返回给访问用户。


五.在squid2.6中refresh_pattern的一些理解和建议.

概念LM,LM就是页面Header里时间(Date)和Last-Modified时间的差。Date一般是Squid从后面取页面的时间,Last-Modified 一般是页面生成时间。
refresh_pattern 的语法是
refresh_pattern [-i] regexp min percent max [options]

min, max的单位是分钟,percent就是百分比。
refresh_pattern 的算法如下:(当前时间定义为CURRENT_DATE)
1) If ((CURRENT_DATE-DATE(就是LM里定义的时间)) < min),cache是新鲜的
2) else if ((CURRENT_DATE-DATE) < (min + (max-min)*percent),cache是新鲜的
3) else cache是过期的
cache过期就需要从后面server取新鲜内容。

常用的几个参数的意思

override-expire
该选项导致squid在检查Expires头部之前,先检查min值。这样,一个非零的min时间让squid返回一个未确认的cache命中,即使该响应准备过期。

override-lastmod
改选项导致squid在检查LM-factor百分比之前先检查min值。

reload-into-ims
该选项让squid在确认请求里,以no-cache指令传送一个请求。换句话说,squid在转发请求之前,对该请求增加一个If-Modified- Since头部。注意这点仅仅在目标有Last-Modified时间戳时才能工作。外面进来的请求保留no-cache指令,以便它到达原始服务器。
一般情况可以使用 reload-into-ims。它其实是强行控制对象的超时时间,这违反了http协议的精神,但是在带宽较窄的场合,可以提高明显系统相应时间。
举例:
refresh_pattern -i \.css$ 1440 50% 129600 reload-into-ims
refresh_pattern -i \.xml$ 1440 50% 129600 reload-into-ims
refresh_pattern -i \.html$ 1440 90% 129600 reload-into-ims-
refresh_pattern -i \.shtml$ 1440 90% 129600 reload-into-ims
refresh_pattern -i \.hml$ 1440 90% 129600 reload-into-ims
refresh_pattern -i \.jpg$ 1440 90% 129600 reload-into-ims
refresh_pattern -i \.png$ 1440 90% 129600 reload-into-ims
refresh_pattern -i \.gif$ 1440 90% 129600 ignore-reload
refresh_pattern -i \.bmp$ 1440 90% 129600 reload-into-ims
refresh_pattern -i \.js$ 1440 90% 129600 reload-into-ims

ignore-reload
该选项导致squid忽略请求里的任何no-cache指令。
所以。如果希望内容一进入cache就不删除,直到被主动purge掉为止,可以加上ignore-reload选项,这个我们常用在mp3,wma,wmv,gif之类。
Examples:

refresh_pattern -i \.mp3$ 1440 50% 2880 ignore-reload
refresh_pattern -i \.wmv$ 1440 50% 2880 ignore-reload
refresh_pattern -i \.rm$ 1440 50% 2880 ignore-reload
refresh_pattern -i \.swf$ 1440 50% 2880 ignore-reload
refresh_pattern -i \.mpeg$ 1440 50% 2880 ignore-reload
refresh_pattern -i \.wma$ 1440 50% 2880 ignore-reload
resource age =对象进入cache的时间-对象的last_modified
response age =当前时间-对象进入cache的时间
LM-factor=(response age)/(resource age)

举个例子,这里只考虑percent, 不考虑min 和max

例如:refresh_pattern 20%

假设源服务器上 -----lastmodified 是 2007-04-10 02:00:00
squid上 proxy.aaa.com/index.htm index.htm进入cache的时间 2007-04-10 03:00:00

1)如果当前时间 2007-04-10 03:00:00
resource age =3点-2点=60分钟
response age =0分钟
index.htm还可以在cache停留的时间(resource age)*20%=12分钟
也就是说,index.htm进入cache后,可以停留12分钟,才被重新确认。

2)如果当前时间 2007-04-10 03:05:00
resource age =3点-2点=60分钟
response age =5分钟
index.htm还可以在cache停留的时间(resource age)*20%=12分钟-5=7
LM-factor=5/60=8.3%<20%

一直到2007-04-10 03:12:00 LM-factor=12/60=20% 之后,cache中的页面index.htm终于stale。
如果这时没有index.htm的请求,index.htm会一直在缓存中,如果有index.htm请求,squid收到该请求后,由于已经过期, squid会向源服务器发一个index.htm是否有改变的请求,源服务器收到后,如果index.htm没有更新,squid就不用更新缓存,直接把缓存的内容放回给客户端,同时,重置对象进入cache的时间为与源服务器确认的时间,比如2007-04-10 03:13:00,如果正好在这个后重新确认了页面。重置后,resource age变长,相应在cache中存活的时间也变长。

如果有改变则把最新的index.htm返回给squid,squid收到会更新缓存,然后把新的index.htm返回给客户端,同时根据新页面中的Last_Modified和取页面的时间,重新计算resource age,进一步计算出存活时间。

实际上,一个页面进入cache后,他的存活时间就确定了,即 (resource age) * 百分比,一直到被重新确认。



六.squid常用命令:
/usr/local/squid/sbin/squid -z 初始化缓存空间
/usr/local/squid/sbin/squid 启动
/usr/local/squid/sbin/squid -k shutdown 停止
/usr/local/squid/sbin/squid -k reconfigure 重新载入配置文件
/usr/local/squid/sbin/squid -k rotate 轮循日志

七.滚动日志文件

   除非你在squid.conf 里禁止,squid 会写大量的日志文件。你必须周期性的滚动日志文件,以阻止它们变得太大。squid 将大量的重要信息写入日志,假如写不进去了,squid 会发生错误并退出。为了合理控制磁盘空间消耗,在cron 里使用如下命令:
%squid -k rotate

例如,如下任务接口在每天的早上4 点滚动日志:
0 4 * * * /usr/local/squid/sbin/squid -k rotate

该命令做两件事。首先,它关闭当前打开的日志文件。然后,通过在文件名后加数字扩展名,它重命名cache.log,store.log,和 access.log。例如,cache.log 变成cache.log.0,cache.log.0变成cache.log.1,如此继续,滚动到logfile_rotate 选项指定的值。

squid 仅仅保存每个日志文件的最后logfile_rotate 版本。更老的版本在重命名过程中被删除。假如你想保存更多的拷贝,你需要增加logfile_rotate 限制,或者编写脚本用于将日志文件移动到其他位置。


 
阅读(2192) | 评论(2) | 转发(0) |
0

上一篇:Apache rewrite规则

下一篇:screen命令

给主人留下些什么吧!~~

chinaunix网友2009-09-23 11:16:44

proxy server QQ群,29736483

chinaunix网友2008-04-14 00:06:03

今天刚装了2.6,好多参数不太明白。正好学习学习,谢谢。