1、squid 安装

  1. wget http://www.squid-cache.org/Versions/v3/3.1/squid-3.1.18-20111219-r10418.tar.gz  
  2. tar xvf squid-3.1.18-20111219-r10418.tar.gz  
  3. cd squid-3.1.18-20111219-r10418  
  4. ./configure --prefix=/usr/local/squid   \  
  5. --enable-epoll   \  
  6. --disable-internal-dns  \  
  7. --enable-storeio=aufs,diskd,ufs  \  
  8. --enable-default-err-languages=Simplify_Chinese  \  
  9. --enable-linux-netfilter    \  
  10. --enable-err-language=Simplify_Chinese  \  
  11. --enable-default-err-languages=Simplify_Chinese  \  
  12. --enable-arp-acl  \  
  13. --enable-snmp   \  
  14. --enable-async-io=90   \  
  15. --enable-cahce-digests        \  
  16. --enable-underscore    \  
  17. --enable-gnuregex    \  
  18. --enable-icmp  \
  19. --enable-kill-parent-hack  \ 
  20.  
  21. make&&make install
  22. groupadd squid
  23. useradd -g squid:squid squid
  24. mkdir -p /data/squid/logs
  25. chown -R squid /data/squid/
  26. chown -R squid /usr/local/squid

: 1>--enable-epoll        //支持epoll非阻塞io模型
      2>--enable-default-err-languages=Simplify_Chinese  //默认的错误语言
      3>--enable-err-language=Simplify_Chinese   //支持错误代理
      4>--enable-arp-acl    //允许基于MAC地址的存取过滤
      5>--enable-snmp     //支持snmp
      6>--enable-async-io=90     //采用同步io提高性能,此值视具体配置情况而定
      7>--enable-cahce-digests     //允许缓存摘要,可以加快请求缓冲内容的速度(集群用)
      8>--enable-icmp       //icmp协议代理(默认不允许)
      9>--enable-linux-netfilter        //要想实现透明代理,启用linux netfilter支持
      10>--enable-async-io=180        //1G内存,并且是双核的选180,否则选100以下
 

squid配置

  1. ###########################squid.conf#######################  
  2. http_port 192.168.0.1:3128 transparent  
  3. cache_mem 100 MB   
  4. maximum_object_size 9000 KB       
  5. maximum_object_size_in_memory 6128 KB  
  6.  
  7. #dns_nameservers  202.106.0.20 8.8.8.8  
  8. cache_dir ufs /data/squid/cache 100 16 256   
  9. error_directory /usr/local/squid/share/errors/zh-cn  
  10.  
  11. acl nocache urlpath_regex cookie.*\.php *\.jsp *\.asp *\.pl *\.cgi   
  12. no_cache deny nocache       
  13.  
  14. acl audio urlpath_regex -i .torrent$ .avi$ .mp3$ .mp4$  
  15. http_access deny audio       
  16.  
  17. acl name url_regex -i    
  18. http_access deny name  
  19.  
  20. cache_access_log /data/squid/logs/cache_access.log  
  21. cache_log /data/squid/logs/cache.log  
  22. cache_store_log /data/squid/logs/cache_store.log  
  23.  
  24. acl localhost src 192.168.0.0/24  
  25. http_access allow localhost  
  26.  
  27. #acl all src 0.0.0.0/0.0.0.0  
  28. http_access deny all  
  29.  
  30. acl safe port 80 21 443 3128  
  31. http_access allow safe     
  32.  
  33. cache_effective_user squid  
  34. cache_effective_group squid   
  35. cache_mgr 103194669@qq.com  
  36. acl conncount maxconn 20  
  37.  
  38. visible_hostname LinuxProxyServer  
  39. icon_directory /usr/local/squid/share/icons  
  40.  
  41. ipcache_size 2024  
  42. ipcache_low 90  
  43. ipcache_high 95  
  44. fqdncache_size 2024  
  45. #######################squid.conf_END#########################  

配置文件是自己情况而定
 

  1. /usr/local/squid/sbin/squid -z 生成缓存目录  
  2. /usr/local/sbin/squid -NCd1  显示日志运行  
  3. /usr/local/sbin/squid  后台运行  
  4.  
  5. echo "/usr/local/sbin/squid" >> /etc/rc.local 

2、安装sarg

安装httpd服务yum install httpd(我这里采用了yum安装,也可以源码编译看个人爱好了)

  1. wget http://fossies.org/unix/privat/sarg-2.3.2.tar.gz  
  2. tar xvf sarg-2.3.2.tar.gz  
  3. cd sarg-2.3.2  
  4. chmod +x configure  
  5. ./configure --perfix=/usr/local/sarg  
  6. make && install 

注意:此时可能提示错误 修改方法为 编辑 util.c 186 行 LLONG改为 LONG
 

配置文件

  1. #######################sarg.conf################################  
  2. access_log /data/squid/logs/cache_access.log  
  3. graphs yes  
  4. title "Squid User Access Reports"  
  5. font_face Tahoma,Verdana,Arial  
  6. header_color darkblue  
  7. header_bgcolor blanchedalmond  
  8. font_size 10px  
  9. header_font_size 11px  
  10. title_font_size 13px  
  11. background_color white  
  12. text_color #000000  
  13. text_bgcolor lavender  
  14. title_color green  
  15. logo_p_w_picpath none  
  16. logo_text ""  
  17. logo_text_color #000000  
  18. p_w_picpath_size 80 45  
  19. background_p_w_picpath none  
  20. password none  
  21. temporary_dir /tmp  
  22. output_dir /var/www/html  
  23. anonymous_output_files no  
  24. charset UTF-8  
  25. #######################sarg.conf_END################################# 

每天生成一次报表(时间根据自己需求定)

  1. echo "* 1 * * * root /usr/local/sarg/bin/sarg" >>/etc/crontab   

3、透明代理 iptables 设置

  1. #!/bin/bash  
  2. /sbin/iptables -F  
  3. /sbin/iptables -Z  
  4. /sbin/iptables -X  
  5. /sbin/iptables -t nat -F  
  6. /sbin/iptables -t nat -Z  
  7. /sbin/iptables -t nat -X  
  8. /sbin/iptables -t filter -F  
  9. /sbin/iptables -t mangle -X  
  10. /sbin/iptables -P INPUT DROP  
  11. /sbin/iptables -P OUTPUT ACCEPT  
  12. /sbin/iptables -P FORWARD ACCEPT  
  13. /sbin/iptables -t nat -P PREROUTING ACCEPT  
  14. /sbin/iptables -t nat -P POSTROUTING ACCEPT  
  15. /sbin/iptables -t nat -P OUTPUT ACCEPT  
  16.  
  17. #启动转发功能  
  18. echo "1" > /proc/sys/net/ipv4/ip_forward  
  19.  
  20. #打开本机回路  
  21. /sbin/iptables -A INPUT -i lo -j ACCEPT  
  22. /sbin/iptables -A  OUTPUT -o lo -j ACCEPT  
  23.  
  24. #打开内网连接  
  25. /sbin/iptables -A INPUT -i eth1 -j ACCEPT  
  26.  
  27. #允许已建立的连接  
  28. /sbin/iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT  
  29.  
  30. #允许ping  
  31. /sbin/iptables -A INPUT -p icmp -j ACCEPT  
  32.  
  33. #允许22,80端口  
  34. /sbin/iptables -A INPUT -p tcp --dport 22 -j ACCEPT  
  35. /sbin/iptables -A INPUT -p tcp --dport 80 -j ACCEPT  
  36.  
  37.  
  38.  
  39. #192.168.1源地址转换  
  40. /sbin/iptables -t nat -A POSTROUTING -s 192.168.0.0/24 -o eth0 -j SNAT --to-source 192.168.1.22  
  41.  
  42.  
  43. #将eth1去往80端口的数据包重定向去本机的3128端口,也就是SQUID处理  
  44. /sbin/iptables -t nat -A PREROUTING -i eth1 -s 192.168.0.0/24 -p tcp -m tcp --dport 80 -j REDIRECT --to-ports 3128  
  45.  
  46.  
  47. #SQUID允许的客户  
  48. #/sbin/iptables -A INPUT -s 192.168.0.0/24 -p tcp --dport 3128 -j ACCEPT  
  49. #/sbin/iptables -A OUTPUT -d 192.168.0.0/24 -p tcp --sport 3128 -j ACCEPT  
  50.  
  51. #现在过滤不是做在output与input里,因为数据是从一个网卡流动到另一个网卡,得在forward里  
  52. /sbin/iptables -A FORWARD -s 192.168.0.0/24 -d 0/0 -p tcp  -j ACCEPT  
  53. /sbin/iptables -A FORWARD -s 0/0 -d 192.168.0.0/24 -p tcp  -j ACCEPT  
  54.  
  55. 备注:  
  56. squidclient使用简介:  
  57. 查看帮助:./squidclient -h 192.168.0.1 -p 3128 mgr:  
  58. /usr/local/squid/squidclient  
  59. squidclient -h 192.168.0.1 -p 3128 mgr:client_list //查看客户端列表  
  60. squidclient -h 192.168.0.1 -p 3128 mgr:objects //取得已缓存的列表  
  61. squidclient -h 192.168.0.1 -p 3128 mgr:info     //取得运行状态 

补充(转载)

1. Squidclient 的基本的使用方法

  1. squidclient -p 80 mgr:info      #取得squid运行状态信息:  
  2. squidclient -p 80 mgr:mem     #取得squid内存使用情况:  
  3. squidclient -p 80 mgr:objects. use it carefully,it may crash   #取得squid已经缓存的列表:  
  4. squidclient -p 80 mgr:diskd  #取得squid的磁盘使用情况:  
  5. squidclient -p 80 -m PURGE http://www.test.com/test.htlm  #强制更新某个url 

更多的请查看:squidclient -h 或者 squidclient -p 80 mgr: 

2.  如何得知 squid 执行中的状态?

还有一个容易的方法,就是使用 cachemgr.cgi 这个安装好 squid 后就有,只要在 apache 配置一下

要查看 Cache Manager 时,只要用下面的方法,就行了,我以后晚点介绍一下这个

http://IP/cgi-bin/cachemgr.cgi

当然,我更加喜欢命令使用下面的方法

  1. squidclient -t 1 -h localhost -p 80 mgr:inf #这样也行 

下面是一些基本内容

Squid Object Cache: Version 2.6 //Squid的版本

  1. HTTP/1.0 200 OK  
  2. Date: Tue, 11 Mar 2008 16:08:14 GMT  
  3. Content-Type: text/plain  
  4. Expires: Tue, 11 Mar 2008 16:08:14 GMT  
  5. Last-Modified: Tue, 11 Mar 2008 16:08:14 GMT  
  6. Connection: close  
  7.    
  8. Squid Object Cache: Version 2.6.STABLE6  
  9. Start Time:     Tue, 11 Mar 2008 10:21:47 GMT  
  10. Current Time:   Tue, 11 Mar 2008 16:08:14 GMT  
  11. Connection information for squid:  
  12.         Number of clients accessing cache:      2023            #使用proxy的电脑数量  
  13.         Number of HTTP requests received:       81787        #客户端http要求数量  
  14.         Number of ICP messages received:        0        #接受到的icp query数量  
  15.         Number of ICP messages sent:    0            #发出icp query数量  
  16.         Number of queued ICP replies:   0  
  17.         Request failure ratio:   0.00  
  18.         Average HTTP requests per minute since start:   236.1    #每分钟http request的数量  
  19.         Average ICP messages per minute since start:    0.0  
  20.         Select loop called: 24789642 times, 0.839 ms avg  
  21. Cache information for squid:  
  22.         Request Hit Ratios:     5min: 99.6%, 60min: 98.7%    #Cache Request命中率  
  23.         Byte Hit Ratios:        5min: 100.0%, 60min: 100.0%    #Cache Byte命中率  
  24.         Request Memory Hit Ratios:      5min: 1.6%, 60min: 1.2%  
  25.         Request Disk Hit Ratios:        5min: 82.0%, 60min: 90.5%  
  26.         Storage Swap size:      7723212 KB            #存放cache的硬盘使用  
  27.         Storage Mem size:       7992 KB               #存放cache的内存使用  
  28.         Mean Object Size:       264.01 KB              #平均的对象大小  
  29.         Requests given to unlinkd:      279  
  30. Median Service Times (seconds)  5 min    60 min:  
  31.         HTTP Requests (All):   2.94900  3.46762  
  32.         Cache Misses:          0.03427  0.03427  
  33.         Cache Hits:            5.06039  4.79440  
  34.         Near Hits:             0.30459  0.35832  
  35.         Not-Modified Replies:  0.00179  0.00179  
  36.         DNS Lookups:           0.00000  0.00000  
  37.         ICP Queries:           0.00000  0.00000  
  38. Resource usage for squid:  
  39.         UP Time:        20787.011 seconds  
  40.         CPU Time:       128.799 seconds  
  41.         CPU Usage:      0.62%  
  42.         CPU Usage, 5 minute avg:        0.44%  
  43.         CPU Usage, 60 minute avg:       0.51%  
  44.         Process Data Segment Size via sbrk(): 34292 KB  
  45.         Maximum Resident Size: 0 KB  
  46.         Page faults with physical i/o: 0  
  47. Memory usage for squid via mallinfo():  
  48.         Total space in arena:   34424 KB  
  49.         Ordinary blocks:        27031 KB   8599 blks  
  50.         Small blocks:               0 KB      0 blks  
  51.         Holding blocks:          6152 KB      2 blks  
  52.         Free Small blocks:          0 KB  
  53.         Free Ordinary blocks:    7392 KB  
  54.         Total in use:           33183 KB 82%  
  55.         Total free:              7392 KB 18%  
  56.         Total size:             40576 KB  
  57. Memory accounted for:               #内存使用状态  
  58.         Total accounted:        21777 KB  
  59.         memPoolAlloc calls: 15446992  
  60.         memPoolFree calls: 15337015  
  61. File descriptor usage for squid:  
  62.         Maximum number of file descriptors:   16384        系统最大file descriptor数  
  63.         Largest file desc currently in use:   1165        目前使用file descriptor最大值  
  64.         Number of file desc currently in use:  571        目前正在使用的file descriptor数  
  65.         Files queued for open:                   0  
  66.         Available number of file descriptors: 15813  
  67.         Reserved number of file descriptors:   100  
  68.         Store Disk files open:                 266  
  69.         IO loop method:                     epoll  
  70. Internal Data Structures:  
  71.          29315 StoreEntries            #Cache中存放的 object 的数量  
  72.            190 StoreEntries with MemObjects    #内存中 object 数量  
  73.            176 Hot Object Cache Items        #热点的 object 的数量  
  74.          29253 on-disk objects              # 不在硬盘上的 object 的数量