Centos 7的squid服务器配置ACL访问控制及日志分析
部署squid服务及配置传统代理参考博文:Centos 7安装Squid代理服务及构建传统代理
配置squid透明代理参考博文:Centos7安装Squid代理服务及配置透明代理
一、ACL访问控制
squid提供了强大的代理控制机制,通过合理设置ACL并进行限制,可以针对源地址、目标地址、访问的URL路径、访问的时间等各种条件进行过滤。在配置文件squid.conf中,ACL访问控制通过两个步骤来实现:其一、使用ACL配置项定义需要控制的条件;其二、通过http_access配置项对已定义的列表做"允许"或"拒绝"访问的控制。
1、定义访问控制列表
每行acl配置可以定义一条访问控制列表,格式如下:
acl 列表名称 列表类型 列表内容其中,"列表名称"是由管理员自行指定,用来识别控制条件;;"列表类型"必须使用squid预定义的值,对应不同类别的控制条件;"列表内容"是要控制的具体对象,不同类型的列表所对应的内容也不一样,可以有多个值(以空格分隔,为"或"的关系)。
通过上述格式可以发现,定义访问控制列表时,关键在于选择"列表类型"并设置具体的条件对象。Squid预定义的列表类型有很多种,常用的包括源地址、目标地址、访问时间、访问端口等,如下表:
ACL定义示例:
[root@centos02~ ]# vim /etc/squid.conf ..........................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 localhost src 127.0.0.1/255.255.255.255 acl mylan src 192.168.1.0/24 192.168.4.0/24 acl to_localhost dst 127.0.0.0/8 acl mc20 maxconn 20 acl blackurl url_regex -i ^rtsp:// ^emule:// acl mediafile urlpath_regex -i \.mp3$ \.mp4$ \.rmvb$ acl worktime time MTWHF 9:00-18:00 ........................当需要限制的同一类对象较多时,可以使用独立的文件来存放,在acl配置行的内容处指定对应的文件位置即可。如下:
[root@centos02~ ]# mkdir /etc/squid[root@centos02~ ]# cd /etc/squid[root@centos02 squid]# vim ipblock.list 61.135.167.36125.39.127.2560.28.14.0[root@centos02 squid]# vim dmblock.list .qq.com.msn.com.live.com.verycd.com[root@centos02 squid]# vim /etc/squid.conf acl ipblock dst "/etc/squid/ipblock.list" acl dmblock dstdomain "/etc/squid/dmblock.list"当ACL设置好后,还需要通过http_access配置项来进行控制。必须注意的是,http_access配置行必须放在对应的acl配置行之后。每行http_access配置确定一条访问规则,格式如下:
http_access allow或deny 列表名
将刚才定义的acl应用到规则中,如下:
[root@centos02 squid]# vim /etc/squid.conf ......................http_access deny !Safe_ports http_access deny mediafile http_access deny ipblock http_access deny dmblock http_access deny mc20 http_access allow worktime reply_body_max_size 10 MB .......................http_access deny all 在配置访问权限时,需要注意以下几点 :
每条http_access规则中,可以同时包含多个访问控制列表名,各个列表之间以空格分隔,是"与"的关系,表示必须满足所有访问控制列表对应的条件才会进行限制。
需要使用取反条件时,可以在访问控制列表前添加" !"符号。
执行访问控制时,squid将按照各条规则的顺序依次进行检查,如果找到一条相匹配的规则就不再向后搜索(这点和iptables的规则匹配类似)。因此,规则的顺序非常重要。
没有设置任何规则时,squid服务将拒绝客户端的请求。这也就是为什么配置文件中默认存在三个内网网段的ACL规则,若想拒绝默认存在的三个网段中某个,还需将其注释掉,再进行限制,以免发生冲突,造成访问规则不生效。
- 有规则但找不到相匹配的项:squid将采用与最后一条规则相反的动作,即如果最好一条规则是allow,就拒绝客户端的请求,否则允许该请求,默认存在的最后一条规则为"http_access deny all "
通常情况下,把最常用的控制规则放在最前面,以减少squid的负载。在访问控制的总体策略上,建议采用"先拒绝后允许"或"先允许后拒绝"的方式。
二、squid日志分析
为了使我们查看日志更为直观一些,可以使用SARG,它是一款squid日志分析工具,采用HTML格式, 详细列出每位用户访问Internet的站点信息,时间占用信息、排名、连接次数、访问量等。
1、导入gd包
1)导入gd包
[root@centos02 ~]# rz 
[root@centos02 ~]# lsanaconda-ks.cfg gd-devel-2.0.35-11.el6.x86_64.rpm initial-setup-ks.cfg2、安装及配置sarg
[root@centos02 ~]# mount /dev/cdrom /mnt/ mount: /dev/sr0 写保护,将以只读方式挂载[root@centos02 ~]# tar zxvf /mnt/sarg-2.3.7.tar.gz -C /usr/src/ [root@centos02 ~]# umount /mnt/ [root@centos02 ~]# mount /dev/cdrom /mnt/ mount: /dev/sr0 写保护,将以只读方式挂载[root@centos02 ~]# rm -rf /etc/yum.repos.d/CentOS-* [root@centos02 ~]# yum -y install gd gd-devel httpd [root@centos02 ~]# rpm -ivh gd-devel-2.0.35-11.el6.x86_64.rpm [root@centos02 ~]# rpm -ivh gd-devel-2.0.35-11.el6.x86_64.rpm --nodeps [root@centos02 ~]# rpm -qa | grep gd [root@centos02 ~]# rpm -qa | grep gd-devel [root@centos02 ~]# rpm -qa | grep httpd [root@centos02 ~]# mkdir /usr/local/sarg [root@centos02 ~]# cd /usr/src/sarg-2.3.7/ [root@centos02 sarg-2.3.7]# ./configure --prefix=/usr/local/sarg --sysconfdir=/etc/sarg --enable-extraprotection [root@centos02 sarg-2.3.7]# make && make install [root@centos02 sarg-2.3.7]# ls -ld /etc/sarg/ drwxr-xr-x 2 root root 83 11月 18 10:35 /etc/sarg/ [root@centos02 sarg-2.3.7]# ls -ld /usr/local/sarg/ drwxr-xr-x 4 root root 30 11月 18 10:35 /usr/local/sarg/[root@centos02 ~]# cp /etc/sarg/sarg.conf /etc/sarg/sarg.conf.bak [root@centos02 ~]# vim /etc/sarg/sarg.conf 8 access_log /usr/local/squid/var/logs/access.log 27 title "Squid User Access Reports" 122 output_dir /var/www/html/squid-reports 182 user_ip no 189 topuser_sort_field BYTES reverse 196 user_sort_field BYTES reverse 211 exclude_hosts /usr/local/sarg/noreport 265 overwrite_report no 298 mail_utility mailx 444 charset utf-8 529 weekdays 0-6 537 hours 0-23 646 www_document_root /var/www/html [root@centos02 ~]# mkdir -p /usr/local/sarg/noreport [root@centos02 ~]# ln -s /usr/local/sarg/bin/sarg /usr/local/bin/ [root@centos02 ~]# sarg SARG: 纪录在文件: 27, reading: 100.00%SARG: 成功的生成报告在 /var/www/html/squid-reports/2019Nov18-2019Nov18[root@centos02 ~]# systemctl start httpd [root@centos02 ~]# systemctl enable httpd 3、windows客户端用浏览器访问测试
http://192.168.100.20/squid-reports/
http://192.168.100.20/sarg/
三、编写定期刷新日志的脚本
[root@centos02 ~]# vim log.sh #!/bin/bashTD=$(date -d '1 day ago' +%d/%M/%Y)/usr/local/sarg/bin/sarg -l /usr/local/squid/var/logs/access.log -o /var/www/html/sarg -z -d $YETD_$TD &> /dev/nullexit 0[root@centos02 ~]# chmod +x log.sh [root@centos02 ~]# ./log.sh & [1] 7798[root@centos02 ~]# vim /etc/rc.d/rc.local /root/log.sh[root@centos02 ~]# chmod +x /etc/rc.d/rc.local ------ 本文至此结束,感谢阅读 ------