SQUID反向代理安装+配置文件详解+缓存清理脚本+日志切割

http://blog.51cto.com/qiyishi/1597362

公司本身是做监控的,在全国各地都有节点,节点任务量不大,闲着也是闲着索性自制了cdn,代理加速公司的网站。

一、安装配置squid

1、软件下载、安装

#推荐使用3.1版本,比较稳定。

#squid软件下载地址:http://www.squid-cache.org/Versions/

1

2

3

4

5

6

[root@dev2-60 ~]# cd /usr/local/src/

[root@dev2-60 ~]# wget http://www.squid-cache.org/Versions/v3/3.1/squid-3.1.10.tar.gz 

[root@dev2-60 ~]# tar zxvf squid-3.1.10.tar.gz

[root@dev2-60 ~]# cd squid-3.1.10

[root@dev2-60 ~]# ./configure --prefix=/usr/local/squid --enable-gnuregex --enable-icmp --enable-linux-netfilter  --enable-arp-acl --enable-ssl ##--enable-ssl https代理必须参数。

[root@dev2-60 ~]# make && make install

2、配置文件详解

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

acl all src all      #定义IP访问列表

acl localhost src 127.0.0.1/255.255.255.255     #定义管理查看的地址段

acl manager proto cache_object              #定义管理规则

cache_dir ufs /data/cache 2048 16 256       #指定缓存目录 20G,16个1级目录,256个二级目录。

cache_swap_high 95                  #最多允许使用swap 95% 

cache_swap_low  90                  #最小允许使用swap 90% 

acl SSL_ports port 443              #定义端口

acl Safe_ports port 80              #定义端口

acl Safe_ports port 443             #定义端口

acl CONNECT method CONNECT          #请求方法以CONNECT,能动态切换到隧道的代理

acl allow_80 url_regex ^http://.*   #定义url的匹配规则

acl allow_443 url_regex  ^https://.* #定义url的匹配规则

acl managercache src 127.0.0.1 10.0.1.60  #定义PURGE模块管理地址     

acl Purge method PURGE                      #定义开启PURGE模块

acl QUERY urlpath_regex \.js \.css \.ico \.gif \.jpg \.jpeg \.png \.html \.htm  #定义缓存类型    

http_access allow manager localhost     #允许管理地址管理

http_access allow localhost manager     #允许管理地址管理

http_access allow managercache Purge    ##允许PURGE列表  

http_access deny Purge                  #拒绝其他管理PURGE

http_access deny !Safe_ports            #拒绝除safe_ports以外端口    

http_access deny CONNECT !SSL_ports     #拒绝除SSL_ports以外使用connect方式。  

http_access allow all                   #允许任何人访问                   

http_port 80 accel vhost vport                #监听80  ###accel 为加速模式  squid2.6以上版本加速模式配置方法。

cache_peer xx.xx.xx.11  parent 80  0 originserver no-query name=web1_80 ##定义代理IP 及端口  #NO-QUERY:不做查询操作,直接获取数据

#originserver 参数指明是源服务器,round-robin 参数指明 squid 通过轮询方式将请求分发到其中一台父节点

cache_peer xx.xx.xx.12  parent 80  0 originserver no-query name=web2_80 

cache_peer_access web1_80 allow allow_80   ##定义http开头的url访问name为web1、web2(根据上面定义的url_regex规则)

cache_peer_access web2_80 allow allow_80   ##定义http开头的url访问name为web1、web2(根据上面定义的url_regex规则)        

https_port 443 cert=/usr/local/squid/etc/xxxxxx.crt key=/usr/local/squid/etc/xxxxxx.key accel vhost vport

cache_peer xx.xx.xx.11  parent 443 0  no-query originserver round-robin  ssl sslflags=DONT_VERIFY_PEER name=web1_443  ##ssl为指定协议,sslflags=DONT_VERIFY_PEER 为不进行源端SSL证书验证,可以解决上级网站采用自签证书造成的中断,以及减少响应时间

cache_peer xx.xx.xx.12  parent 443 0  no-query originserver round-robin ssl sslflags=DONT_VERIFY_PEER name=web2_443

cache_peer_access web1_443 allow allow_443  ##定义https开头的url访问name为web1、web2(根据上面定义的url_regex规则)

cache_peer_access web2_443 allow allow_443  ##定义https开头的url访问name为web1、web2(根据上面定义的url_regex规则)

#emulate_httpd_log on                       ##开启apache默认的log格式,不过3.1中不认次参数,去掉一样生效       

logfile_rotate 24                           ##定义access.log轮训数到24,  access.log0  access.log1 ....access.log24

access_log /data/squid_log/access.log squid #定义路径

cache deny !QUERY                           #拒绝除定义的缓存类型以外的其他类型的缓存

refresh_pattern -i www\.xxxxxx\.com 240 90% 1440 ignore-reload     #ignore-reload让squid忽略所有请求里的reload

refresh_pattern -i qiye\.xxxxxxx\.com 240 90% 1440 ignore-reload

refresh_pattern . 120 50% 1440              # #配置Squid的刷新策略 . 所有类型 最小时间 刷新节点百分比 最大时间

acl apache rep_header Server ^Apache        #允许apache的编码 

visible_hostname cdn-xx.xxx.com             # 机器名称     

cache_mgr  [email protected]          #管理员邮箱

3、启动之前

1

2

3

4

5

6

[root@dev2-60 ~]# mkdir -p /data/cache    #创建缓存目录

[root@dev2-60 ~]# chown -R nobody.nobody /data/cache    #授权

[root@dev2-60 ~]# mkdir -p /data/squid_log #创建log目录

[root@dev2-60 ~]# chown -R nobody.nobody /data/squid_log #授权

[root@dev2-60 ~]# chwon -R nobody.nobody /usr/local/squid/var #授权var目录以便生成squid.pad

[root@dev2-60 ~]# rz   #上传https的证书认证文件。

4、启动服务

1

2

3

4

[root@dev2-60 ~]# /usr/local/squid/sbin/squid -z  #创建缓存里面的小目录

[root@dev2-60 ~]# /usr/local/squid/sbin/squid start #启动服务

[root@dev2-60 ~]# netstat -lntup  #检查监听端口是否已经正常监听。

## 绑定host查看代理情况  X-Cache:HIT from cdn-xx.xxxx.com #hit为正常缓存 miss为未缓存。

二、缓存清理脚本(转自网友,感觉还可以)

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

#!/bin/sh

#if [ $# -ne 1 ] 

#  then

#       echo "用法:$0 type [.js|.css|.jpg|.|and so on..]" 

#       exit 1

#fi

grep -a -r . /data/cache/* |strings  | grep "http:" awk -F'http:' '{print "http:"$2;}' >>cache_list.txt

for url in `cat cache_list.txt`

do

        /usr/local/squid/bin/squidclient -h 127.0.0.1 -p 80 -m PURGE $url >>$(date +%F)_pure.log

done

[ $? -eq 0 ] && echo "http rm_cache succ" ||print "http rm_cache fail"

rm -rf cache_list.txt

grep -a -r . /data/cache/* |strings  | grep "https:" awk -F'https:' '{print "https:"$2;}' >>cache_list.txt

for url in `cat cache_list.txt`

do

        /usr/local/squid/bin/squidclient -h 127.0.0.1 -p 80 -m PURGE $url >>$(date +%F)_pure_443.log

done

[ $? -eq 0 ] && echo "https rm_cache succ" ||print "https rm_cache fail"

rm -rf cache_list.txt

三、日志相关

我们除了清理缓存在关注的可能就是日志了,为了分析方便我们一般讲起一天切割一次。我们可以简单的使用定时任务执行squid自带命令完成。

##日志切割:

1

59 23 * * * /usr/local/squid/sbin/squid -k rotate

##日志时间格式转换。(squid若不开启apache的纪录方式纪录日志,那么时间格式将是格林威治时间,可以用下面的命令转换一下)

1

perl -pe 's/^\d+\.\d+/localtime($&)/e;' access.log

四、其他

##当squid运行久了,swap.state文件就会越来越大那么影响响应的时间,因此定时更新swap.state

1

2

###squid rm swap.state by mark on 20150122

57 23 * * * /usr/local/squid/sbin/squid -k rotate -f /usr/local/squid/etc/squid.conf

##关于squid的健康检查机制

根据我的测试得出的结论如下:

squid会对父节点做健康检查,如果父节点服务器down机,则squid会将其踢出,并做10次的尝试连接,也就是说前10次的轮询中会对其尝试连接,查过10次则不再进行尝试连接,cache.log记录节点dea 直接将分配到其他父节点,等服务器up后,cache.log再记录期lived继而继续分配。

but,如果服务down掉,squid却全然不知!! 没错,apache关闭了 squid也不知道 - -,因此猜想squid的健康检查机制很可能是根据ping来检查的,这只是我的猜想,当然也很有可能squid有期很完善的机制,我没发现,并且没有配置正确,如果是,还请大家相告,谢谢!

猜你喜欢

转载自blog.csdn.net/chen3888015/article/details/81174455