squid代理介绍----ACL控制应用+sarg日志分析+反向代理

一、squid的ACL访问控制

  • Squid提供了强大的代理控制机制,通过合理设置ACL(Access Control List,访问控制列表)并进行限制,可以针对源地址、目标地址、访问的URL路径、访问的时间等各种条件进行过滤。

  • ACL访问控制的步骤:

    • 1、使用 acl 配置项定义需要控制的条件
      定义格式:acl 列表名称 列表类型 列表内容 …
    • 2、通过http_access配置项对已定义的列表做“允许”或“拒绝”访问的控制

二、ACL设置的两种方式

2.1、直接在squid的配置文件里修改

vim /etc/squid.conf
acl localhost src 192.168.10.10/32        #针对固定的源ip地址
acl MYLAN src 192.168.220.0/24              #针对某一网段
acl destionhost dst 192.168.220.130/32   #针对具体的目标ip地址
acl MC20 maxconn 20                             #访问的最大并发连接数量
acl BURL url_regex -i ^rtsp:// ^emule://    #正则表达式的访问协议
acl PURL urlpath_regex -i \.mp3$ \.mp4$ \.rmvb$   #访问的文件资源末尾
acl work time MTWHF 08:30-17:30          #访问时间
MTWHF:周一到周五
MTWHFAS:周一到周日

http_access deny localhost     #拒绝列表 (注意置顶)

2.2、创建新文件,在配置文件里声明文件位置

mkdir /etc/squid      #启用对象列表管理
vim dest.list
       192.168.220.128
       192.168.220.140
       192.168.220.130     #目标web
vim /etc/squid.conf
    acl destionhost dst "/etc/squid/dest.list"
  
    http_access deny destionhost    #拒绝列表 (注意置顶)

三、sarg日志分析

3.1、sarg安装

//图像处理
yum -y install gd gd-devel httpd 

//解压安装包
tar zxvf sarg-2.3.7.tar.gz -C /opt/

cd /opt/sarg-2.3.7/
./configure --prefix=/usr/local/sarg \
--sysconfdir=/etc/sarg \
--enable-extraprotection         #额外安全防护
 
make && make install

3.2、修改配置文件

vi /etc/sarg/sarg.conf
对应行号去掉注释即开启
7 access_log /usr/local/squid/var/logs/access.log              ###squid的访问日志位置
25 title "Squid User Access Reports"                                     ###网页标题
120 output_dir /var/www/html/squid-reports                     ###分析报告的存放位置
178 user_ip no                                                                         ###不使用IP代替用户ID
184 topuser_sort_field BYTES reverse                                   ###升序排列
190 user_sort_field BYTES reverse 
206 exclude_hosts /usr/local/sarg/noreport                        ###设置不生成报告的主机
257 overwrite_report no
289 mail_utility mailx                                                              ###指定发邮件命令
434 charset UTF-8
518 weekdays 0-6                                                                   ###指定top排序星期周期
523 hours 7-12,14,16,18-20                                                   ###指定top排序时间周期
633 www_document_root /var/www/html                           ###网页根目录

touch /usr/local/sarg/noreport            ###建立不生成报告的主机列表文件

ln -s /usr/local/sarg/bin/sarg /usr/local/bin/

//启动sarg日志分析和squid网页服务
sarg
systemctl start httpd

在这里插入图片描述

3.3、验证

1、在客户端机浏览器输入:http://192.168.10.1/squid-reports/index.html

2、squid服务器设置周期性任务

crontab -e 
*/1 * * * * /usr/local/bin/sarg    #这里为了效果快速呈现出,设置了每隔1分钟

在这里插入图片描述

四、squid反向代理

4.1、介绍

  • 传统和透明是为客户端服务的,借助squid加快访问web服务的速度,或者是公司内部对员工上网行为做限制使用的。
  • 反向代理模式下的squid的服务对象是web服务器,通过squid来隐藏真实web服务器IP,加快客户的访问速度,还有负载均衡的功能。
  • 反向代理的设置,需要通过三个步骤来完成:
    • DNS解析
    • SQUID配置
    • 端口转发

4.2、部署操作

一台squid服务器
两台web服务器,web1:192.168.100.131 web2:192.168.100.132
一台win10客户端

1、web服务器部署

//安装httpd
yum install httpd -y

//设置网页内容
echo "this is test02 web" > /var/www/html/index.html    #web1换一下数字
route add -net 192.168.10.0/24 gw 192.168.220.128  #静态路由

//开启web服务
systemctl start httpd

2、squid反向代理配置

//设置防火墙规则
systemctl start firewalld
iptables -L    #查看防火墙规则
iptables -F
iptables -t nat -F
iptables -I INPUT -p tcp --dport 3128 -j ACCEPT

vim /etc/squid.conf
#去掉透明代理设置反向代理
http_port 192.168.100.128:80 accel vhost vport

#节点服务器1最大访问30,权重1,别名web1
cache_peer 192.168.100.131 parent 80 0 no-query originserver round-robin max_conn=30 weight=1 name=web1
#节点服务器2最大访问30,权重1,别名web2
cache_peer 192.168.100.132 parent 80 0 no-query originserver round-robin max_conn=30 weight=1 name=web2
#访问yun.com匹配web1,web2节点
cache_peer_domain web1 web2 www.yun.com

service squid restart

3、客户端验证

在win10客户端的浏览器输入web的两个IP地址可以访问代理服务器。
在这里插入图片描述
在这里插入图片描述
接下来是域名验证。
在客户机的admin用户下配置解析域名地址,并设置代理
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

原创文章 112 获赞 44 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_28361541/article/details/104926402