Squid反向代理、ACL控制及Sarg日志统计

一、Squid服务器ACL访问控制

(一)ACL访问控制原理:

1.首先定义acl列表 :格式:acl 列表名称 列表类型 列表内容 ……
2.基于acl列表指定规则 :格式:http_access allow|deny列表名称

(二)acl列表的类型:

类型 示例
src 源地址 acl localhost src 192.168.175.136/32 ##定义源主机ip
acl MYLAN src 192.168.175.0/24 ##定义网段
dst 目标地址 acl destionhost dst 192.168.175.130/32 ##定义具体目标主机ip
port 目标端口 //
dstdomain 目标域名 //
time 访问时间 acl work time MTWHFAC 08:30-17:30 注意:MTWHFAC 字母分别指周一到周日
maxconn 最大并发量 acl MC20 maxconn 20 ##定义MC20列表,指明最大连接数
url_regex 目标URL acl BURL ur1_regex -i ^rtsp:// ^emule:// ##可以通过正则定义域名
url path_regex 整个目标URL 路径 acl PURL urlpath_regex -i .mp3$ .mp4$ .rmvb$ ##以.mp3,.mp4等结尾

(三)将列表名单存放在文件中,定义文件进行访问控制

示例:

[root@localhost ~]# vi /etc/squid/ipblock.list   ##创建ip名单,注意,要在
61.135.167.36
60.28.14.0/24
[root@localhost ~]# vi /etc/squid/dmblock.list
.qq.com
.msn.com
[root@squid ~]# vim /etc/squid.conf
acl lPBLOCK dst "/etc/squid/ipblock.list"    ##定义目标ip的文件
acl DMBLOCK dstdomain "/etc/squid/dmblock.list"    ##定义目标域名的文件
thttp_access deny IPBLOCK
http_access deny DMBLOCK

示范:

1.添加ACl规则

[root@squid ~]# vim /etc/squid.conf
acl MMM src 192.168.30.0/24 ##定义一条名为MMM的访问控制列表
http_access deny MMM ##拒绝所有MMM列表中名单的http请求
http_access allow all ##放通所有(默认与最后一条ACL规则相反,可以不用设)

2.Client访问Web测试

(1)配置规则前
在这里插入图片描述
(2)配置ACL则后,请求失败
在这里插入图片描述

二、Squid反向代理

在这里插入图片描述

实验拓扑:

在这里插入图片描述
基于上次传统模式上继续试验https://blog.csdn.net/CN_LiTianpeng/article/details/109387464

【Web1端】IP:192.168.30.30

1.制作一个测试页面

[root@localhost httpd]# cd /var/www/html/
[root@localhost html]# vi index.html
<title>HELLO</title>
<h1>this is test1 Web</h1>
[root@localhost html]# systemctl restart httpd

2.访问Web测试,页面显示正常

在这里插入图片描述

【Web2端】IP:192.168.30.40

1.制作一个测试页面

[root@localhost httpd]# cd /var/www/html/
[root@localhost html]# vi index.html
<title>GOOD</title>
<h1>this is test2 Web</h1>
[root@localhost html]# systemctl restart httpd

2.访问Web测试,页面显示正常

在这里插入图片描述

【Squid端】 IP:192.168.30.10

1.清空防火墙规则,并放通80端口

[root@squid ~]# iptables -F
[root@squid ~]# iptables -t nat -F
[root@squid ~]# systemctl start firewalld
[root@squid ~]# iptables -I INPUT -p tcp --dport 80 -j ACCEPT 

2.修改配置文件,反向代理配置

[root@squid init.d]# vim /etc/squid.conf   ##修改配置
#http_port 3128
http_port 192.168.10.10:80 accel vhost vport   ##配置监听地址80端口作为虚拟加速地址和端口,配置80端口后,客户机浏览器就可以不用做代理服务器了,可以http直接访问。
cache_peer 192.168.10.20 parent 80 0 no-query originserver round-robin max_conn=30 weight=1 name=web1    ##匹配192.168.10.20地址80端口,并且禁止查询真实域内服务器,轮询调度,最大连接数30,权重为1,该域内服务器命名为web1
cache_peer 192.168.10.40 parent 80 0 no-query originserver round-robin max_conn=30 weight=1 name=web2
cache_peer_domain web1 web2 www.yun.com 192.168.30.10  ##匹配域内主机web1,web2;可以使用域名www.yun.com或IP地址192.168.30.10访问
//注意:若在cache_peer_domain配置的末尾不加域名或者ip,就无法使用域名或ip进行负载均衡反向代理,并且如果只有域名,那么只能访问域名进行反向代理轮询调度,访问ip无效,反之同理。一般只需要配置域名即可
[root@squid init.d]# service squid stop
[root@squid init.d]# service squid start
正在启动 squid....
[root@squid init.d]# netstat -lanpt |grep 80
tcp        0      0 192.168.30.10:80        0.0.0.0:*               LISTEN      3935/(squid-1)   

【客户端访问测试】 IP :192.168.30.100

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

三、Squid——Sarg日志统计功能

1.安装sarg日志程序

[root@squid ~]# ls   ##查看sarg软件包
  sarg-2.3.7.tar.gz  ……省略部分
[root@squid ~]# yum -y install gd gd-devel
[root@squid ~]# yum -y install gcc gcc-c++ make
[root@squid ~]# tar zxvf sarg-2.3.7.tar.gz -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 -j3 && make install

2.修改配置文件

[root@squid sarg-2.3.7]# cd /etc/sarg/
[root@squid sarg]# vim sarg.conf 
7行//    access_log /usr/local/squid/var/logs/access.log   ##指定访问日志文件
25行//   title "Squid User Access Reports"    ##网页标题
120行//  output_dir /var/www/html/squid-reports   ##报告输出目录
178行//  user_ip no    ##使用用户名显示
184行//  topuser_sort_field connect reverse  ### 在 top 排序中,指定连接次数,访问字节数,采用降序排列,升序是normal
206行//  exclude_hosts /usr/local/sarg/noreport  ##不计入排序的站点列表文件
257行//  overwrite_report no    ##同名日志是否覆盖
289行//  mail_utility mailq.postfix    ##发送邮箱报告的命令
434行//  charset UTF-8    ##使用字符集
516行//  weekdays 0-6    ##指定 top 排序时的星期周期,0 为周日
525行//  hours 0-23    ## top排序的时间周期
633行//  www_document_root /var/www/html    ##网页根目录
[root@squid sarg]# egrep -vn '^#|^$' /etc/sarg/sarg.conf   ##过滤检查配置是否修改正确
7:access_log /usr/local/squid/var/logs/access.log
25:title "Squid User Access Reports"
120:output_dir /var/www/html/squid-reports
178:user_ip no
184:topuser_sort_field connect reverse
206:exclude_hosts /usr/local/sarg/noreport
257:overwrite_report no
289:mail_utility mailq.postfix 
434:charset UTF-8
516:weekdays 0-6
525:hours 0-23
633:www_document_root /var/www/html
[root@squid sarg]# touch /usr/local/sarg/noreport

3.优化路径,并生成报告文件

[root@squid ~]# ln -s /usr/local/sarg/bin/sarg /usr/local/bin/
[root@squid ~]# sarg    ##生成报告
SARG: Records in file: 2528, reading: 100.00%
SARG: Successful report generated on /var/www/html/squid-reports/2020Oct30-2020Nov01
[root@squid ~]# cd /var/www/html/squid-reports/
[root@squid squid-reports]# ls  ##查看,里面有index.html首页文件
2020Oct30-2020Nov01  images  index.html
[root@squid sarg-2.3.7]# cd /etc/sarg/
[root@squid sarg]# vim sarg.conf
190行//  user_sort_field reverse     ##对于用户访问记录,连接次数按降序排列。注意,这个功能一定要在使用sarg命令生成报告文件后开启,否则会报错

4.安装Apache服务

[root@squid ~]# yum -y install httpd   ##安装一个Apache访问
[root@squid ~]# systemctl start httpd   ##启动Apache
[root@squid ~]# cd /var/www/html/
[root@squid html]# ls
squid-reports

5.客户端访问查看

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

6.制定周期性计划任务

[root@squid html]# 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)
##执行这条语句,制定每日计划任务,sarg -l 生成访问日志,-o输出到/var/www/html/squid-reports/目录下,-d指定时间间隔“当天时间减去前一天时间” 即一天间隔。

猜你喜欢

转载自blog.csdn.net/CN_LiTianpeng/article/details/109410338
今日推荐