Squid代理服务器——ACL访问控制,日志分析Sarg,以及反向代理配置实操

一:ACL控制访问

1.1:ACL访问控制方式

  • 根据源地址、目标URL、文件类型等定义列表
acl 列表名称 列表类型 列表内容...
  • 针对已定义的acl列表进行限制
http_access allow或deny 列表名称 ...

1.2:ACL规则优先级

  • 一个用户访问代理服务器时,Squid会顺序匹配Squid中定义的所有规则列表,一旦匹配成功,立即停止匹配
  • 所有规则都不匹配时,Squid会使用与最后一条相反的规则

1.3:常用的ACL列表类型

src 源地址
dst 目的地址
port 端口
dstdomain 目标域
time 访问时间
maxconn 最大并发连接
url_regex 目标URL地址
Urlpath_regex 整个目标URL路径
  • 例如
acl localhost src 20.0.0.20/32
acl MYLAN src 20.0.0.0/24
acl destionhost dst 20.0.0.52/32
acl MC20 max-conn 20
acl BURL url_regex -i ^rtsp:// ^emule://
acl PURL urlpath_regex -i \.mp3$ \.mp4$ \.rmvb$
acl work time MTWHFAC 08:30-17:30

1.4:实验配置

1.4.1:环境

  • VMware软件
  • 一台centos7虚拟机作为squid服务器,IP地址为:20.0.0.51
  • 一台centos7虚拟机作为web服务器,IP地址为:20.0.0.52
  • 一台win7虚拟机作为client测试机,IP地址为:20.0.0.20

1.4.2:实验目的

  • 通过ACL访问控制实现对主机允许和拒绝访问

1.4.3:实验步骤

[root@squid ~]# vim /etc/squid.conf
# should be allowed
acl localhost src 20.0.0.20/32   '添加,监控client客户端的主机(20.0.0.20/32)取名为hostlocal'
# Deny requests to certain unsafe ports 
http_access deny hostlocal          '添加,调用hostlocal,设置拒绝访问'
[root@squid ~]# service squid restart
  • 方式二:设置ACL访问规则(当多个IP地址时)
mkdir /etc/squid    '启用对象列表管理'
vim dest.list
20.0.0.52   '目标WEB'
vim /etc/squid.conf
acl destionhost dst "/etc/squid/dest.list"
http_access deny destionhost     拒绝列表(注意置顶)
[root@squid ~]# service squid restart
  • client客户端尝试访问web端,client客户端先清除浏览器数据

二:Squid日志分析工具Sarg

2.1:在squid服务器上部署sarg

[root@squid ~]# yum install -y gd gd-devel
[root@squid ~]# mkdir /usr/local/sarg
[root@squid ~]# tar zxvf sarg -C /opt
[root@squid ~]# cd /opt/sarg-2.3.7/
[root@squid sarg-2.3.7]# ./configure --prefix=/usr/local/sarg \
> --sysconfdir=/etc/sarg \
> --enable-extraprotection
[root@squid sarg-2.3.7]# make && make install

2.2:修改Sarg配置文件

[root@squid sarg-2.3.7]# vim /etc/sarg/sarg.conf 
'配置文件中所有的都被注释了,我们需要取消注释一下内容'
access_log /usr/local/squid/var/logs/access.log  '指定访问日志文件'
title "Squid User Access Reports"  '网页标题'
output_dir /var/www/html/squid-reports  '报告输出目录'
user_ip no  '使用用户名显示'
exclude_hosts /usr/local/sarg/noreport  '不计入排序的站点列表文件'
topuser_sort_field connect reverse  
'top排序中有连接次数,访问字节,降序排列,升序是normal'
overwrite_report no  '同名日志是否覆盖'
mail_utility mailq.postfix  '发送邮件报告命令'
charset UTF-8  '使用字符集'
weekdays 0-6  'top排行的时间周期'
hours 0-23  'top排行的时间周期'
www_document_root /var/www/html  '网页根目录'
[root@squid sarg-2.3.7]# touch /usr/local/sarg/noreport	'添加不计入站点文件,添加的域名将不被显示'
[root@squid sarg-2.3.7]# ln -s /usr/local/sarg/bin/sarg /usr/local/bin/	'创建sarg命令的软连接'
[root@squid sarg]# sarg  '生成报告'
SARG: 纪录在文件: 158, reading: 100.00%
SARG: 成功的生成报告在 /var/www/html/squid-reports/2020Sep07-2020Sep07'提示报告生成在这个目录,我们进入这个目录查看一下'
[root@squid sarg-2.3.7]# cd /var/www/html/squid-reports/	'进入目录'
[root@squid squid-reports]# ls
2020Feb08-2020Feb08  images  index.html
[root@squid squid-reports]# yum install httpd -y	'安装httpd'
[root@squid squid-reports]# systemctl start httpd	'开启httpd'
[root@squid squid-reports]# systemctl stop firewalld.service 	'关闭防火墙'
[root@squid squid-reports]# setenforce 0

2.3:使用client客户端访问sarg日志

  • 访问20.0.0.51/squid-reports
    在这里插入图片描述

  • 执行周期性计划任务,每天生成报告

[root@squid squid-reports]# sarg -l /usr/local/squid/var/logs/access.log -o /var/www/html/squid-reports/ -z -d $(date -d "1 day ago" +%d/%m/%Y)-$(date +%d/%m/%Y)
  • 再次查看

三:Squid反向代理

3.1:添加一台web服务器,并设置主页内容

  • 添加一台web2服务器,IP地址为20.0.0.47
'原本的web服务器修改一下主页内容用来和新添加的web2服务器区分'
[root@web ~]# cd /var/www/html
[root@web html]# vim index.html	'修改首页内容'
<h1>this is web01</h1>
'web2服务器设置'
[root@web2 ~]# yum install httpd -y	'安装httpd服务'
[root@web2 ~]# vim /var/www/html/index.html	'修改首页内容'
<h1>this is web02</h1>
[root@web2 ~]# systemctl stop firewalld.service '关闭防火墙'	
[root@web2 ~]# setenforce 0
[root@web2 ~]# systemctl start httpd	'开启httpd服务'
  • 设置反向代理
[root@squid squid-reports]# vim /etc/squid.conf
http_port 20.0.0.51:80 accel vhost vport	'监控本机80端口'
cache_peer 20.0.0.47 parent 80 0 no-query originserver round-robin max_conn=30 weight=1 name=web1	'节点服务器1最大访问30,权重1,别名web1'
cache_peer 20.0.0.52 parent 80 0 no-query originserver round-robin max_conn=30 weight=1 name=web2	'节点服务器2最大访问30,权重1,别名web2'
cache_peer_domain web1 web2 www.yun.com  '访问www.yun.com匹配web1,web2节点'
[root@squid squid-reports]# service squid restart	'重启服务'
正在关闭 squid...
正在启动 squid...
[root@squid sarg]# netstat -ntap |grep squid   '查看80端口是否被squid使用'
tcp        0      0 20.0.0.51:80            0.0.0.0:*               LISTEN      53968/(squid-1) 

3.2:client客户端设置域名解析和squid代理并测试

  • 设置域名解析(以administrator用户登录)
  • 位置:C盘-Windows-system32-drivers-etc-hosts

在这里插入图片描述

  • 设置浏览器代理:因为使用的是谷歌浏览器,以谷歌浏览器为例

在这里插入图片描述
在这里插入图片描述

  • client客户端访问www.yun.com查看是否代理成功

在这里插入图片描述

在这里插入图片描述

  • 客户端实现轮询访问,反向代理成功

猜你喜欢

转载自blog.csdn.net/m0_47219942/article/details/108457018