1、squid 安装
- wget http://www.squid-cache.org/Versions/v3/3.1/squid-3.1.18-20111219-r10418.tar.gz
- tar xvf squid-3.1.18-20111219-r10418.tar.gz
- cd squid-3.1.18-20111219-r10418
- ./configure --prefix=/usr/local/squid \
- --enable-epoll \
- --disable-internal-dns \
- --enable-storeio=aufs,diskd,ufs \
- --enable-default-err-languages=Simplify_Chinese \
- --enable-linux-netfilter \
- --enable-err-language=Simplify_Chinese \
- --enable-default-err-languages=Simplify_Chinese \
- --enable-arp-acl \
- --enable-snmp \
- --enable-async-io=90 \
- --enable-cahce-digests \
- --enable-underscore \
- --enable-gnuregex \
- --enable-icmp \
- --enable-kill-parent-hack \
- make&&make install
- groupadd squid
- useradd -g squid:squid squid
- mkdir -p /data/squid/logs
- chown -R squid /data/squid/
- 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配置
- ###########################squid.conf#######################
- http_port 192.168.0.1:3128 transparent
- cache_mem 100 MB
- maximum_object_size 9000 KB
- maximum_object_size_in_memory 6128 KB
- #dns_nameservers 202.106.0.20 8.8.8.8
- cache_dir ufs /data/squid/cache 100 16 256
- error_directory /usr/local/squid/share/errors/zh-cn
- acl nocache urlpath_regex cookie.*\.php *\.jsp *\.asp *\.pl *\.cgi
- no_cache deny nocache
- acl audio urlpath_regex -i .torrent$ .avi$ .mp3$ .mp4$
- http_access deny audio
- acl name url_regex -i
- http_access deny name
- cache_access_log /data/squid/logs/cache_access.log
- cache_log /data/squid/logs/cache.log
- cache_store_log /data/squid/logs/cache_store.log
- acl localhost src 192.168.0.0/24
- http_access allow localhost
- #acl all src 0.0.0.0/0.0.0.0
- http_access deny all
- acl safe port 80 21 443 3128
- http_access allow safe
- cache_effective_user squid
- cache_effective_group squid
- cache_mgr [email protected]
- acl conncount maxconn 20
- visible_hostname LinuxProxyServer
- icon_directory /usr/local/squid/share/icons
- ipcache_size 2024
- ipcache_low 90
- ipcache_high 95
- fqdncache_size 2024
- #######################squid.conf_END#########################
配置文件是自己情况而定
- /usr/local/squid/sbin/squid -z 生成缓存目录
- /usr/local/sbin/squid -NCd1 显示日志运行
- /usr/local/sbin/squid 后台运行
- echo "/usr/local/sbin/squid" >> /etc/rc.local
2、安装sarg
安装httpd服务yum install httpd(我这里采用了yum安装,也可以源码编译看个人爱好了)
- wget http://fossies.org/unix/privat/sarg-2.3.2.tar.gz
- tar xvf sarg-2.3.2.tar.gz
- cd sarg-2.3.2
- chmod +x configure
- ./configure --perfix=/usr/local/sarg
- make && install
注意:此时可能提示错误 修改方法为 编辑 util.c 186 行 LLONG改为 LONG
配置文件
- #######################sarg.conf################################
- access_log /data/squid/logs/cache_access.log
- graphs yes
- title "Squid User Access Reports"
- font_face Tahoma,Verdana,Arial
- header_color darkblue
- header_bgcolor blanchedalmond
- font_size 10px
- header_font_size 11px
- title_font_size 13px
- background_color white
- text_color #000000
- text_bgcolor lavender
- title_color green
- logo_image none
- logo_text ""
- logo_text_color #000000
- image_size 80 45
- background_image none
- password none
- temporary_dir /tmp
- output_dir /var/www/html
- anonymous_output_files no
- charset UTF-8
- #######################sarg.conf_END#################################
每天生成一次报表(时间根据自己需求定)
- echo "* 1 * * * root /usr/local/sarg/bin/sarg" >>/etc/crontab
3、透明代理 iptables 设置
- #!/bin/bash
- /sbin/iptables -F
- /sbin/iptables -Z
- /sbin/iptables -X
- /sbin/iptables -t nat -F
- /sbin/iptables -t nat -Z
- /sbin/iptables -t nat -X
- /sbin/iptables -t filter -F
- /sbin/iptables -t mangle -X
- /sbin/iptables -P INPUT DROP
- /sbin/iptables -P OUTPUT ACCEPT
- /sbin/iptables -P FORWARD ACCEPT
- /sbin/iptables -t nat -P PREROUTING ACCEPT
- /sbin/iptables -t nat -P POSTROUTING ACCEPT
- /sbin/iptables -t nat -P OUTPUT ACCEPT
- #启动转发功能
- echo "1" > /proc/sys/net/ipv4/ip_forward
- #打开本机回路
- /sbin/iptables -A INPUT -i lo -j ACCEPT
- /sbin/iptables -A OUTPUT -o lo -j ACCEPT
- #打开内网连接
- /sbin/iptables -A INPUT -i eth1 -j ACCEPT
- #允许已建立的连接
- /sbin/iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
- #允许ping
- /sbin/iptables -A INPUT -p icmp -j ACCEPT
- #允许22,80端口
- /sbin/iptables -A INPUT -p tcp --dport 22 -j ACCEPT
- /sbin/iptables -A INPUT -p tcp --dport 80 -j ACCEPT
- #192.168.1源地址转换
- /sbin/iptables -t nat -A POSTROUTING -s 192.168.0.0/24 -o eth0 -j SNAT --to-source 192.168.1.22
- #将eth1去往80端口的数据包重定向去本机的3128端口,也就是SQUID处理
- /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
- #SQUID允许的客户
- #/sbin/iptables -A INPUT -s 192.168.0.0/24 -p tcp --dport 3128 -j ACCEPT
- #/sbin/iptables -A OUTPUT -d 192.168.0.0/24 -p tcp --sport 3128 -j ACCEPT
- #现在过滤不是做在output与input里,因为数据是从一个网卡流动到另一个网卡,得在forward里
- /sbin/iptables -A FORWARD -s 192.168.0.0/24 -d 0/0 -p tcp -j ACCEPT
- /sbin/iptables -A FORWARD -s 0/0 -d 192.168.0.0/24 -p tcp -j ACCEPT
- 备注:
- squidclient使用简介:
- 查看帮助:./squidclient -h 192.168.0.1 -p 3128 mgr:
- /usr/local/squid/squidclient
- squidclient -h 192.168.0.1 -p 3128 mgr:client_list //查看客户端列表
- squidclient -h 192.168.0.1 -p 3128 mgr:objects //取得已缓存的列表
- squidclient -h 192.168.0.1 -p 3128 mgr:info //取得运行状态
补充(转载):
1. Squidclient 的基本的使用方法
- squidclient -p 80 mgr:info #取得squid运行状态信息:
- squidclient -p 80 mgr:mem #取得squid内存使用情况:
- squidclient -p 80 mgr:objects. use it carefully,it may crash #取得squid已经缓存的列表:
- squidclient -p 80 mgr:diskd #取得squid的磁盘使用情况:
- 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
当然,我更加喜欢命令使用下面的方法
- squidclient -t 1 -h localhost -p 80 mgr:inf #这样也行
下面是一些基本内容
Squid Object Cache: Version 2.6 //Squid的版本
- HTTP/1.0 200 OK
- Date: Tue, 11 Mar 2008 16:08:14 GMT
- Content-Type: text/plain
- Expires: Tue, 11 Mar 2008 16:08:14 GMT
- Last-Modified: Tue, 11 Mar 2008 16:08:14 GMT
- Connection: close
- Squid Object Cache: Version 2.6.STABLE6
- Start Time: Tue, 11 Mar 2008 10:21:47 GMT
- Current Time: Tue, 11 Mar 2008 16:08:14 GMT
- Connection information for squid:
- Number of clients accessing cache: 2023 #使用proxy的电脑数量
- Number of HTTP requests received: 81787 #客户端http要求数量
- Number of ICP messages received: 0 #接受到的icp query数量
- Number of ICP messages sent: 0 #发出icp query数量
- Number of queued ICP replies: 0
- Request failure ratio: 0.00
- Average HTTP requests per minute since start: 236.1 #每分钟http request的数量
- Average ICP messages per minute since start: 0.0
- Select loop called: 24789642 times, 0.839 ms avg
- Cache information for squid:
- Request Hit Ratios: 5min: 99.6%, 60min: 98.7% #Cache Request命中率
- Byte Hit Ratios: 5min: 100.0%, 60min: 100.0% #Cache Byte命中率
- Request Memory Hit Ratios: 5min: 1.6%, 60min: 1.2%
- Request Disk Hit Ratios: 5min: 82.0%, 60min: 90.5%
- Storage Swap size: 7723212 KB #存放cache的硬盘使用
- Storage Mem size: 7992 KB #存放cache的内存使用
- Mean Object Size: 264.01 KB #平均的对象大小
- Requests given to unlinkd: 279
- Median Service Times (seconds) 5 min 60 min:
- HTTP Requests (All): 2.94900 3.46762
- Cache Misses: 0.03427 0.03427
- Cache Hits: 5.06039 4.79440
- Near Hits: 0.30459 0.35832
- Not-Modified Replies: 0.00179 0.00179
- DNS Lookups: 0.00000 0.00000
- ICP Queries: 0.00000 0.00000
- Resource usage for squid:
- UP Time: 20787.011 seconds
- CPU Time: 128.799 seconds
- CPU Usage: 0.62%
- CPU Usage, 5 minute avg: 0.44%
- CPU Usage, 60 minute avg: 0.51%
- Process Data Segment Size via sbrk(): 34292 KB
- Maximum Resident Size: 0 KB
- Page faults with physical i/o: 0
- Memory usage for squid via mallinfo():
- Total space in arena: 34424 KB
- Ordinary blocks: 27031 KB 8599 blks
- Small blocks: 0 KB 0 blks
- Holding blocks: 6152 KB 2 blks
- Free Small blocks: 0 KB
- Free Ordinary blocks: 7392 KB
- Total in use: 33183 KB 82%
- Total free: 7392 KB 18%
- Total size: 40576 KB
- Memory accounted for: #内存使用状态
- Total accounted: 21777 KB
- memPoolAlloc calls: 15446992
- memPoolFree calls: 15337015
- File descriptor usage for squid:
- Maximum number of file descriptors: 16384 系统最大file descriptor数
- Largest file desc currently in use: 1165 目前使用file descriptor最大值
- Number of file desc currently in use: 571 目前正在使用的file descriptor数
- Files queued for open: 0
- Available number of file descriptors: 15813
- Reserved number of file descriptors: 100
- Store Disk files open: 266
- IO loop method: epoll
- Internal Data Structures:
- 29315 StoreEntries #Cache中存放的 object 的数量
- 190 StoreEntries with MemObjects #内存中 object 数量
- 176 Hot Object Cache Items #热点的 object 的数量
- 29253 on-disk objects # 不在硬盘上的 object 的数量