squid终于支持多核心了~~~~~~~~~~~
从squid官方网站 http://www.squid-cache.org/Versions/下载
稳定版有2.7和3.1供选择 测试版3.2已经加入了SMP支持,可以最大利用处理器资源,3.1之前的所有版本都是不支持SMP和多CPU。
 
  
  1. yum -y install ntp make openssl openssl-devel pcre pcre-devel libpng libpng-devel libjpeg-6b libjpeg-devel-6b freetype freetype-devel gd gd-devel zlib zlib-devel gcc gcc-c++ libXpm libXpm-devel ncurses ncurses-devel libmcrypt libmcrypt-devel libxml2 libxml2-devel imake autoconf automake screen sysstat compat-libstdc++-33 curl curl-devel

  2. wget http://www.squid-cache.org/Versions/v3/3.2/squid-3.2.0.18.tar.gz

  3. tar -zvxf squid-3.2.0.18.tar.gz

  4. cd squid-3.2.0.18

  5. ./configure --prefix=/usr/local/squid --enable-gnuregex --enable-async-io=80 --enable-cache-digests --enable-err-language="zh-cn"--enable-default-err-language="zh-cn" --enable-epoll --disable-internal-dns --with-filedescriptors=20480 --enable-kill-parent-hack

  6. make

  7. make install

#创建squid相关目录

 
  
  1. groupadd squid #创建squid squid用户组

  2. useradd -g squid -s /sbin/nologin #创建squid用户,并加入到squid组里,不允许登录系统

  3. chown -R squid /usr/local/squid/ #修改squid的安装目录所属用户为squid用户

  4. mkdir -p /var/cache1 #创建squid的第一个缓存目录

  5. mkdir -p /var/cache2 #创建squid的第二个缓存目录

  6. chown squid.squid -R /var/cache1 /var/squid2 #设置目录所有者

  7. chmod -R 777 /var/cache1 /var/squid #设置目录权限

#配置squid
将squid的初始配置文件备份,我们要重新创建配置文件

mv /usr/local/squid/etc/squid.conf /usr/local/squid/etc/squid.conf.bak

vi /usr/local/squid/etc/squid.conf
 
  
  1. max_filedescriptors 65535

  2. visible_hostname rui

  3. acl SSL_ports port 443

  4. acl Safe_ports port 80

  5. acl CONNECT method CONNECT

  6. acl myip src localhost

  7. #http_access deny OverConnLimit

  8. #acl web1 src 117.21.227.134

  9. #acl web2 src .....

  10. http_access allow manager localhost

  11. http_access deny !Safe_ports

  12. http_access deny CONNECT !SSL_ports

  13. acl OverConnLimit maxconn 300

  14. http_access deny OverConnLimit

  15. http_access allow all

  16. #header_access X-Forwarded-For allow all

  17. #acl_uses_indirect_client on

  18. #follow_x_forwarded_for allow all

  19. allow_underscore on

  20. half_closed_clients off

  21. cache_peer 222.174.95.21 parent 9011 0 no-query originserver name=web21

  22. cache_peer 122.226.213.41 parent 80 0 no-query originserver name=web41

  23. http_port 80 accel vhost vport

  24. cache_peer_domain web21 .upbar.net mypig.upbar.net .mypig.net .8goo.com .vitas.com.cn .ibar.cc .360loss.com .99zyz.com

  25. cache_peer_domain web21 .grow100.com.cn .jpstore.net .jk0769.com .maxmancapsule.com .maxmancapsule.com.cn .maxmancapsules.com.tw .72sun.com .30jf.com .356jf.com .24jf.com .aidashan.com .she36.com

  26. cache_peer_domain web21 .grow100.net .gao36.com .gao36.net .way200.com .easy900.com .show1000.com .gft400.com .topgouwu.net

  27. cache_peer_domain web21 .igaofei.com .igaofei.net .mek123.com .mek123.net .yugutoo.com yututoo.com

  28. cache_peer_domain web41 .bibe.cn .yein.cc .youxia.cn

  29. cache_peer_access web21 allow all

  30. cache_peer_access web41 allow all

  31. forwarded_for on

  32. acl QUERY urlpath_regex cgi-bin .cgi .php .avi .wmv .rm .ram .mpg .mpeg .zip .exe .asp .aspx

  33. cache deny QUERY

  34. reload_into_ims on

  35. refresh_pattern ^ftp: 1440 20% 10080

  36. refresh_pattern ^gopher: 1440 0% 1440

  37. refresh_pattern . 0 20% 4320

  38. refresh_pattern -i \.html 1440 100% 129600 reload-into-ims

  39. refresh_pattern -i \.shtml 1440 100% 129600 reload-into-ims

  40. refresh_pattern -i \.htm 1440 100% 129600 reload-into-ims

  41. refresh_pattern -i \.gif 1440 100% 129600 ignore-reload

  42. refresh_pattern -i \.jpg 1440 100% 129600 ignore-reload

  43. refresh_pattern -i \.png 1440 100% 129600 reload-into-ims

  44. refresh_pattern -i \.bmp 1440 100% 129600 reload-into-ims

  45. refresh_pattern -i \.swf 1440 100% 129600 reload-into-ims

  46. refresh_pattern -i \.flv 129600 100% 129600 ignore-reload

  47. refresh_pattern -i \.js 1440 100% 129600 reload-into-ims

  48. refresh_pattern -i \.css 1440 100% 129600 reload-into-ims

  49. pid_filename /usr/local/squid/var/logs/squid.pid

  50. #logformat squid %ts.%03tu %6tr %>a %Ss/%03Hs %<st %rm %ru %un %Sh/%<A %mt

  51. #cache_log /usr/local/squid/var/logs/cache.log

  52. #access_log /usr/local/squid/var/logs/access.log

  53. access_log /dev/null

  54. cache_log /dev/null

  55. cache_store_log none

  56. #error_directory none

  57. cache_mem 256 MB

  58. memory_pools_limit 312 MB

  59. maximum_object_size_in_memory 4096 KB

  60. max_open_disk_fds 0

  61. minimum_object_size 0 KB

  62. maximum_object_size 4096 KB

  63. maximum_object_size_in_memory 4096 KB

  64. memory_replacement_policy lru

  65. cache_dir ufs /var/cache1 1024 16 256

  66. #cache_dir null /tmp


  67. EOF

maximum_object_size 是 能cache最大的文件大小。对应wmv,rm文件,建议设置为32768 kB

maximum_object_size_in_memory 是在内存中cache的最大文件大小。

cache_mem 是SQUID可用到的最大内存。经实践,4G内存的服务器用2G;超过2G导致SQUID运行不稳

首先要分析SQUID所cache内容:

运行

squidclient -p 80 cache_object://localhost/info

能看到如下内容:

Storage Swap size: 7549104 KB

Storage Mem size: 418804 KB

Mean Object Size: 160.46 KB

Mean Object Size是平均内容大小,一般要把maximum_object_size_in_memory设置成离它最近的128的倍数。在这个例子中maximum_object_size_in_memory 的值应该是256kB。

cache_mem 一般设置成服务器内存的一半或更多,只要运行过程中LINUX没有使用SWAP就可以。

再就是按业务分SQUID。

比如某个论坛,用户能上载图片和视频;当然我们要把上载的图片、视频放在单独的域名上,比如img.example.com, video.example.com;这两个域名只提供静态文件服务。

根据统计,图片的平均大小在100KB,视频的平均大小在4M,差别是很大,应该建两个squid分别作图片和视频的CACHE。图片SQUID的 maximum_object_size_in_memory 设置为256KB,视频的SQUID的maximum_object_size_in_memory设置为8196KB。

Squid优化(2)

Hot!探讨动态内容的CACHE。

BBS,论坛是典型动态内容,要保证内容更新及时的同时,提高访问速度,降低数据库负担不是个简单任务。经实践发现如下办法取得很好效果:

1) 配置SQUID,对动态内容强制CACHE,用到的配置参数是refresh_pattern

refresh_pattern ^/forum/viewthread.php 1440 1000% 1440 ignore-reload

/forum/viewthread.php的内容将强制保持1天

2) 修改论坛程序在用户回复帖子后,向SQUID发送PURGE命令清除相应帖子的页面CACHE,保证失效性

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~实现过这一功能,但是有时候生效,有时候无效,还未进一步查明原因.(Edit by Sean)

3) 有些频繁更新的页面可以不CACHE,用no_cache参数

acl no_forum_cache urlpath_regex ^/forum/forumdisplay.php

no_cache DENY no_forum_cache


启动squid
检查 squid 配置文件正确与否
/usr/local/squid/sbin/squid -Nk parse
生成缓存目录
/usr/local/squid/sbin/squid -N –z
测试squid
/usr/local/squid/sbin/squid -N -d1
启动squid
/usr/local/squid/sbin/squid
ps -ef |grep squid # 检查squid是否正常启动


配置squid开机启动
vi /etc/rc.d/rc.local
在最后添加
 
  
  1. /usr/local/squid/sbin/squid

5.滚动日志文件
squid将大量的重要信息写入日志,假如写不进去了,squid会发生错误并退出。为了合理控制磁盘空间消耗,必须周期性的滚动日志文件。
/usr/local/squid/sbin/squid -k rotate
ls -alh /usr/local/squid/var/logs/
通过上图,可以看到日志已经被分割了,将此命令加入到crontab里,让它在每天的凌晨4点运行。
crontab -e
在最后添加
 
  
  1. 0 4 * * * /usr/local/squid/sbin/squid -k rotate


 
  
  1. 结束squid进程 /squid/sbin/squid –k shutdown

  2. 取得squid运行状态信息:/squid/bin/squidclient -p 80 mgr:info

  3. 取得squid内存使用情况:/squid/bin/squidclient -p 80 mgr:mem

  4. 取得squid已经缓存的列表:/squid/bin/squidclient -p 80 mgrbjects. use it carefully,it may crash

  5. 取得squid的磁盘使用情况:/squid/bin/squidclient -p 80 mgr:diskd

  6. 强制更新某个url:/squid/bin/squidclient -p 80 -m PURGE http://www.rui.com/

  7. 查命中率:

  8. /soft/squid/bin/squidclient -h111.222.111.111 -p80 mgr:info

  9. /soft/squid/bin/squidclient -h具体的IP -p80 mgr:info

清理缓存~~~

qingli.sh www.xiuxiukan.com

qingli.sh jpg

qingli.sh xiuxiukan.com 123 bbb jpg

 
  
  1. #!/bin/sh

  2. squidcache_path="/squidcache"

  3. squidclient_path="/home/local/squid/bin/squidclient"

  4. #grep -a -r $1 $squidcache_path/* | grep "http:" | awk -F 'http:' '{print "http:"$2;}' | awk -F\' '{print $1}' > cache.txt

  5. if [[ "$1" == "swf" || "$1" == "png" || "$1" == "jpg" || "$1" == "ico" || "$1" == "gif" || "$1" == "css" || "$1" == "js" || "$1" == "html" || "$1" == "shtml" || "$1" == "htm" ]]; then

  6. grep -a -r .$1 $squidcache_path/* | strings | grep "http:" | awk -F 'http:' '{print "http:"$2;}' | awk -F\' '{print $1}' | grep "$1$" | uniq > cache.txt

  7. else

  8. grep -a -r $1 $squidcache_path/* | strings | grep "http:" |grep $2$ |grep $3$|grep $4$|grep $5$ |grep $6$| awk -F 'http:' '{print "http:"$2;}' | awk -F\' '{print $1}' | uniq > cache.txt

  9. fi

  10. sed -i "s/\";$//g" cache.txt

  11. cat cache.txt | while read LINE

  12. do

  13. $squidclient_path -p 80 -m PURGE $LINE

  14. done


squid启动文件

 
  
  1. cat >> /etc/init.d/squid <<EOF

  2. #!/bin/bash

  3. #chkconfig: 345 85 15

  4. #description: squid test

  5. #BY liyaoyi 2012-05-04

  6. . /etc/rc.d/init.d/functions

  7. squid="/usr/local/squid/sbin/squid"

  8. prog="squid"

  9. RETVAL=0

  10. start() {

  11. echo -n $"Starting $prog: "

  12. daemon $squid -s

  13. RETVAL=$?

  14. echo

  15. return $RETVAL

  16. }

  17. stop () {

  18. echo -n $"Stoping $prog: "

  19. daemon $squid -k shutdown

  20. echo

  21. return $RETVAL

  22. }

  23. reload () {

  24. echo -n $"Reloading $prog: "

  25. daemon $squid -k reconfigure

  26. echo

  27. return $RETVAL

  28. }

  29. case "$1" in

  30. start)

  31. start

  32. ;;

  33. stop)

  34. stop

  35. ;;

  36. reload)

  37. reload

  38. ;;

  39. restart)

  40. stop

  41. start

  42. ;;

  43. *)

  44. echo $"Usage: $0 {start|stop|restart|reload}"

  45. RETVAL=1

  46. esac

  47. exit $RETVAL

  48. EOF

  49. chmod a+x /etc/init.d/squid

  50. chkconfig --add squid

  51. chkconfig squid on

  52. /etc/init.d/squid start