Proxy缓存因为是读取硬盘的内容,所心应用Proxy的瓶颈就是硬盘的读取。

 
通过上层代理服务器的辅助,达到自动数据分流的效果
 
Apache也可以提供代理服务;
 
配置文件:
1./etc/squid/squid.conf
2./etc/squid/mime.conf   #支持的Internet文件格式
 
相关程序与文件:
1./usr/sbin/squid
2./var/spool/squid       #默认缓存目录
3./usr/lib64/squid       #squid控制模块,密码认证等
 
默认启动squid程序的用户为squid这个账号(与磁盘调整缓存目录权限有关)
 
帮助文件:
[root@Centosszm ~]# vi /usr/share/doc/squid-3.1.4/squid.conf.documented
 
 
[root@Centosszm ~]# grep -v '^[#]' /etc/squid/squid.conf
acl manager proto cache_object   #定义manager为管理功能
acl localhost src 127.0.0.1/32   #定义localhost
acl localhost :1 /128
acl to_localhost dst 127.0.0.0/8 0.0.0.0/32   #定义可连接到本机,也可以用“-”
acl to_localhost dst ::1/128
 
#这个localnet也可以自己改为其它的名字,一个名字对一种限制(src)
acl localnet src 10.0.0.0/8     # RFC1918 possible internal network   #开放代理网络
acl localnet src 172.16.0.0/12  # RFC1918 possible internal network
acl localnet src 192.168.0.0/16 # RFC1918 possible internal network
acl localnet src fc00::/7   # RFC 4193 local private network range
acl localnet src fe80::/10  # RFC 4291 link-local (directly plugged) machines
 
#例子(src,dstdomain,dst,dstdomain,url_regex,urlpath_regex)
acl denyip src 172.16.128.1-172.16.128.100/32
acl denyrule1 url_regex ^http://www.baidu.com/.*  #^开头,.*结尾
acl denyrule2 urlpath_regex /sexy.*\.jpg$         #
acl denydomain dstdomain "/etc/squid/dropdomain.txt"
acl denydomain dstdomain .163.com
 
 
 
 
acl SSL_ports port 443
acl Safe_ports port 80          # http
acl Safe_ports port 21          # ftp
acl Safe_ports port 443         # https
acl Safe_ports port 70          # gopher
acl Safe_ports port 210         # wais
acl Safe_ports port 1025-65535  # unregistered ports
acl Safe_ports port 280         # http-mgmt
acl Safe_ports port 488         # gss-http
acl Safe_ports port 591         # filemaker
acl Safe_ports port 777         # multiling http
acl CONNECT method CONNECT
 
#例子:
http_access deny denyrule1
http_access deny denyrule2
http_access deny denydomain
http_access deny denyip
http_access allow manager localhost
http_access deny manager
 
http_access deny !Safe_ports
http_access deny CONNECT !SSL_ports
 
 
 
http_access allow localnet     #放行localnet网络
http_access allow localhost    #放行localhost网络
http_access deny all
 
http_port 3128
 
hierarchy_stoplist cgi-bin ?
 
cache_dir ufs /var/spool/squid 100 16 256  #100M缓存    一级目录  二级目录
#两层缓存目录较佳的配置就是16 256和64 64
 
 
coredump_dir /var/spool/squid
 
#磁盘缓存时间(regex,失效时间、百分比、最大时间)后面两个为重新获取时间
refresh_pattern ^ftp:           1440    20%     10080
refresh_pattern ^gopher:        1440    0%      1440
refresh_pattern -i (/cgi-bin/|\?) 0     0%      0          
#除了上述的两个开发数据外,其他的数据都是被定义为新
refresh_pattern .               0       20%     4320
 
#add by szm
minimum_object_size 0 KB      #这两个是缓存页面大小要求
maximum_object_size 4096 KB
cache_swap_low 90             #高于95%后删除到占用90%
cache_swap_high 95
 
access_log /var/log/squid/access.log squid   #日志
ftp_user Squid@         #FTP匿名登录时使用的账号名称
ftp_passive on          #使用被动FTP连接
 
cache_mgr root@www.Centosszm.com  #管理员邮箱
visible_hostname www.Centosszm.com  #自定义主机名
cache_effective_user squid
cache_effective_group squid
ipcache_size 8          #这个值与cache_mem 8 MB 相同
ipcache_low 90
ipcache_high 95
 
#设置不缓存PHP页面
acl denyphp urlpath_regex \.php$
cache deny denyphp
 
#上层Proxy与获取数据分流的设定:
 
#下成是上层Proxy设置
#Proxy 角色:parent     sibling
#Proxy port      icp port
#proxy-only:不缓存,只查询
#weight=n:权重
#no-query:向上层Proxy要求数据时,可以不需要发送icp数据包
#no-digest:不向附近主机要求建立digest记录表格
#no-netdb-exchange:不向附近的Proxy主机送出ICMP的数据包要求
cache_peer szm.test.com parent 3128 3130 proxy-only no-query no-digest
 
#要求这台服务器向哪个域名要求数据(.cn)分流.cn的数据
cache_peer_domain szm.test.com .cn
 
 
#cache_peer_access [上层Proxy主机名] [allow|deny] [acl 名称]
 与cache_peer_domain相当类似,只是cache_peer_domain直接规范了主机名,而如果你想要设计的并非域名,而是某些特定的IP网段,就需要先用Acl设计一个名称后,再以cache_peer_access去放行或拒绝读取了。

 

 

[root@Centosszm ~]# netstat -tulnp | grep squid
tcp        0      0 :::3128                     :::*                        LISTEN      14205/(squid)
udp        0      0 :::37572                    :::*                                    14205/(squid)
 
3130(UDP)仅是负责与邻近Proxy互相沟通彼此的缓存数据库的功能,与实际的用户要求无关。
 
 
增加缓存大小:
[root@Centosszm ~]# vi /etc/squid/squid.conf
cache_dir ufs /srv/squid 200 16 256
[root@Centosszm ~]# mkdir /srv/squid
[root@Centosszm ~]# chmod 750 /srv/squid
[root@Centosszm ~]# chown squid:squid /srv/squid
[root@Centosszm ~]# chcon --reference /var/spool/squid/ /srv/squid
[root@Centosszm ~]# ll -Zd /srv/squid
drwxr-x---. squid squid system_u:object_r:squid_cache_t:s0 /srv/squid
[root@Centosszm ~]# /etc/init.d/squid restart
 
 某些特定的目录(/home)是不允许建立缓存目录的。
 
Squid占用内存计算:
缓存*10%+15(squid本身占用)+cache_mem大小
 
防火墙设置:
[root@Centosszm ~]# iptables -A INPUT -p tcp -m tcp --dport 3128 -j ACCEPT
 
重新应用配置文件:
[root@Centosszm ~]# vi /etc/squid/dropdomain.txt
[root@Centosszm ~]# service squid reload
 
Proxy服务放在NAT服务器上:透明代理
1.[root@Centosszm ~]# vi /etc/squid/squid.conf
http_port 3128 transparent
 
2.Port 80 转Port 3128
[root@Centosszm ~]# iptables -t nat -A PREROUTING -i eth1 -s 172.16.128.0/24 -p tcp --dport 80 -j REDIRECT --to-ports 3128
 
 
关闭所有的缓存;只代理,不缓存:
[root@Centosszm ~]# /etc/init.d/squid stop
[root@Centosszm ~]# rm -rf /var/spool/squid/*
[root@Centosszm ~]# vim /etc/squid/squid.conf
cache_dir ufs /var/spool/squid 100 16 256 read-only
#cache_dir ufs /srv/squid 200 16 256
cache_mem 0 MB
[root@Centosszm ~]# /etc/init.d/squid start
 
 
Proxy认证的设置:
 使用的是squid主动提供的ncsa_auth认证模块,这个模块会利用apacher提供的账号密码建立命令(htpasswd)所制作的密码文件作为验证依据。
[root@Centosszm ~]# rpm -ql squid | grep ncsa
/usr/lib/squid/ncsa_auth
/usr/share/man/man8/ncsa_auth.8.gz
[root@Centosszm ~]# rpm -ql httpd | grep htpasswd
 
#上面的没有内容,找看结果如下,应该是这个包独立出来了
[root@Centosszm ~]# rpm -qf /usr/bin/htpasswd
httpd-tools-2.2.15-5.el6.centos.i686
 
[root@Centosszm ~]# rpm -ql httpd-tools | grep htpasswd
/usr/bin/htpasswd
/usr/share/man/man1/htpasswd.1.gz
 
[root@Centosszm ~]# vim /etc/squid/squid.conf
#通过ncsa_auth读取squid_user.txt
#启动5个程序来管理验证的需求
#欢迎信息
auth_param basic program /usr/lib/squid/ncsa_auth /etc/squid/squid_user.txt
auth_param basic children 5
auth_param basic realm Welcome to szm's proxy-only web server   #在放在前面,因为要加载模块
 
#proxy_auth是关键词
#REQUIRED指定任何在密码文件内的用户都能够使用验证的意思
acl squid_user proxy_auth REQUIRED 
http_access allow squid_user
 
[root@Centosszm ~]# htpasswd -c /etc/squid/squid_user.txt szm
New password:
Re-type new password:
Adding password for user szm
[root@Centosszm ~]# cat /etc/squid/squid_user.txt
szm:pWQO53hxTn06w
[root@Centosszm ~]# /etc/init.d/squid reload
 
 
日志分析软件:SARG
http://localhost/sarg/
 
[root@Centosszm ~]# wget http://pkgs.repoforge.org/sarg/sarg-2.3.1-1.el6.rft.i686.rpm
yum install gd gd-devel
[root@Centosszm ~]# rpm -ivh sarg-2.3.1-1.el6.rft.i686.rpm
title "Squid User Access Reports"
font_size 12px
charset UTF-8
 
#制作所有日志文件内的数据报表
[root@Centosszm ~]# sarg
SARG: Records in file: 3410, reading: 100.00%
SARG: (grepday) Fontname /usr/share/sarg/fonts/DejaVuSans.ttf not found
 
#制作4月8号的报表
[root@Centosszm ~]# sarg -d 08/04/2013
SARG: Records in file: 3410, reading: 100.00%
SARG: Period covered by log files: 08/04/2013-08/04/2013
SARG: (grepday) Fontname /usr/share/sarg/fonts/DejaVuSans.ttf not found
 
#上面这两个命令都会将数据放到/var/www/sarg/ONE-SHOT
[root@Centosszm ~]# sh /etc/cron.daily/sarg
[root@Centosszm ~]# ll /var/www/sarg/ONE-SHOT/
total 16
drwxr-xr-x. 4 root root 4096 Apr  8 21:09 2013Apr08-2013Apr08
drwxr-xr-x. 4 root root 4096 Apr  8 21:07 2013Apr08-2013Apr08.1
drwxr-xr-x. 4 root root 4096 Apr  8 21:08 2013Apr08-2013Apr08.2
drwxr-xr-x. 2 root root 4096 Apr  8 21:07 p_w_picpaths
 
HTTP查阅数据:
http://127.0.0.1/sarg