1.ACL访问控制
在配置文件 squid.conf 中,ACL 访问控制通过以下两个步骤来实现:
- 使用 acl 配置项定义需要控制的条件;
- 通过 http_access 配置项对已定义的列表做“允许”或“拒绝”访问的控制。
1.1 访问控制列表格式
格式:acl 列表名称 列表类型 列表内容 …
列表类型 | 定义的列表内容 |
---|
src | 源地址 | dst | 目标地址 | maxconn | 最大并发连接 | port | 目标地址 | dstdomain | 目标域 | url_regex | 匹配请求URL的任何部分 | urlpath_regex | 与url_regex非常相似(不包含传输协议和主机名) | time | 访问时间 |
例如:
vim /etc/squid.conf
......
acl localhost src 192.168.148.12/32
acl MYLAN src 192.168.148.0/24
acl destionhost dst 192.168.148.13/32
acl MC20 maxconn 20
acl PORT port 21
acl DMBLOCK dstdomain .qq.com
acl BURL url_regex -i ^rtsp:
acl PURL urlpath_regex -i \.mp3$ \.mp4$ \.rmvb$
acl WORKTIME time MTWHF 09:00-18:00
1.2 测试部署
环境配置(直接使用之前实验的实例即可)
主机 | 操作系统 | IP地址 | 软件、安装包、工具 |
---|
Squid-Server | CentOS7 | ens33:192.168.148.12 ens36:12.0.0.1 | squid-3.5.28.tar.gz | Web-Server | CentOS7 | 192.168.148.13 | httpd | 客户机 | Windows10 | 192.168.163.100 | |
客户机 Windows10 12.0.0.12
Squid-Server(192.168.148.12)
vim /etc/squid.conf
......
acl localhost src 12.0.0.12/24
acl mylan src 12.0.0.0/24
......
http_access deny localhosttest
systemctl restart squid
netstat -natp |grep squid


Web-Server(192.168.148.13) 之前已经安装好了,这里就不安装了
systemctl stop firewalld.service
setenforce 0
yum -y install httpd
systemctl start httpd
浏览器(192.168.148.13)访问Web服务器 
2.Squid 日志分析
2.1 安装图像处理软件包
- 出现报错,将网卡配置文件中的dns和网关修改回原样
- 安装在线源
yum install -y pcre-devel gd gd-devel
mkdir /usr/local/sarg
tar zxvf sarg-2.3.7.tar.gz -C /opt/
cd /opt/sarg-2.3.7
./configure
make && make install
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
topuser_sort_field connect reverse
user_sort_field connect reverse
exclude_hosts /usr/local/sarg/noreport
overwrite_report no
mail_utility mailq.postfix
charset UTF-8
weekdays 0-6
hours 0-23
www_document_root /var/www/html
  
     
2.2 添加不计入站点文件,添加的域名将不被显示在排序中
touch /usr/local/sarg/noreport
ln -s /usr/local/sarg/bin/sarg /usr/local/bin/
sarg

2.3 运行
sarg

2.4 验证
yum install httpd -y
systemctl start httpd
浏览器访问 http://192.168.148.12/squid-reports ,查看sarg报告网页

2.5 添加计划任务,执行每天生成报告
vim /usr/local/sarg/report.sh
TODAY=$(date +%d/%m/%Y)
YESTERDAY=$(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/squid-reports -z -d $YESTERDAY-$TODAY &> /dev/null
exit 0
chmod +x /usr/local/sarg/report.sh
crontab -e
0 0 * * * /usr/local/sarg/report.sh

3.Squid 反向代理
??如果 Squid 反向代理服务器中缓存了该请求的资源,则将该请求的资源直接返回给客户端;否则反向代理服务器将向后台的 Web 服务器请求资源,然后将请求的应答返回给客户端,同时也将该应答缓存在本地,供下一个请求者使用。
工作机制:
- 缓存网页对象,减少重复请求
- 将互联网请求轮训或按权重分配到内网Web服务器
- 代理用户请求,避免用户直接访问Web服务器,提高安全
环境配置
主机 | 操作系统 | IP地址 | 软件、安装包、工具 |
---|
Squid-Server | CentOS7 | 192.168.148.12 | squid-3.5.28.tar.gz | Web1-Server | CentOS7 | 192.168.148.13 | httpd | Web2-Server | CentOS7 | 192.168.148.136 | httpd | 客户机 | Windows10 | 192.168.148.5 | |
Squid-Server(192.168.148.12)
vim /etc/squid.conf
......
http_port 192.168.148.12:80 accel vhost vport
cache_peer 192.168.148.13 parent 80 0 no-query originserver round-robin max_conn=30 weight=1 name=web1
cache_peer 192.168.148.138 parent 80 0 no-query originserver round-robin max_conn=30 weight=1 name=web2
cache_peer_domain web1 web2 www.test.com
systemctl stop httpd
systemctl restart squid
http_port 80 accel vhost vport
- accel:反向代理加速模式
- vhost:支持域名或主机名来表示代理节点
- vport:支持ip和端口来表示代理节点
- parent:代表为父节点
- 80:HTTP_PORT
- 0:ICP_PORT
- no-query:不做查询操作,直接获取数据
- originserver:指定是源服务器
- round-robin:指定 squid 通过轮询方式将请求分发到其中一台父节点
- max_conn:指定最大连接数
- weight:指定权重
- name:设置别名
web1、web2
systemctl stop firewalld.service
setenforce 0
yum install -y httpd
systemctl start httpd
Web1 (192.168.148.13)
echo "this is web1" >> /var/www/html/index.html

Web2 (192.168.148.136)
echo "this is web2" >> /var/www/html/index.html

客户机(192.168.148.5)
3.1 修改客户端的域名映射
如果提示权限不够,需要修改hosts文件权限 右击hosts文件,属性–>安全–>编辑–>组或用户名中的(Users)–>选择允许权限–>确定,图示在下面
192.168.148.12 www.test.com

3.2 客户机的代理配置

3.3 浏览器访问 http://www.test.com
   说明代理成功!
总结
1.Squid的配置文件squid.conf更改后必须重新运行squid; 2.Squid服务器的hosts文件更改后必须重新运行squid; 3.不要忘记关闭防火墙和安全系数设为0 4.在测试代理时,记得要浏览器开启代理服务,并设置相应的IP和端口。最好再清除历史记录
|