搭建squid反向代理,配置ACL访问控制与sarg日志他来了

一:ACL访问控制

squid提供了强大的代理控制机制,通过合理的设置 ACL 并进行限制,可以针对很多方式来进行条件过滤,比如有以下的方式:针对源地址、目标地址、访问的URL路径、访问的时间等。

配置ACL 的两个步骤
1:在主配置文件中使用 acl 配置项定义需要控制的条件
2:通过 http_access配置项对已定义的列表做 “允许” ,“拒绝” 访问的控制

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路径

1.4:最简单的ACL控制

这边基于传统squid代理设置的!!

详情请访问https://blog.csdn.net/weixin_47151643/article/details/108433807

1.41:设置ACL访问规则

//编辑squid配置文件
[root@tom03 init.d]# vim /etc/squid.conf
# should be allowed
acl hostlocal 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@tom03 init.d]# service squid reload
  • ACL策略设置成功,已拒绝20.0.0.20主机访问
  • 因为我们接下来还需要使用client客户端做测试,所以删除刚刚设置的ACL规则,并重启squid服务
  • 客户机先清除浏览器数据!

mark

我们把注释关掉

#acl hostlocal src 20.0.0.20/32
#http_access deny hostlocal

mark

1.42:通过黑名单限制目标网站

[root@tom03 init.d]# vim /etc/squid.conf

# should be allowed
acl hostlocal src "/etc/squid/src.list"    //拒绝访问目录文件

# Deny requests to certain unsafe ports
http_access deny hostlocal        //调用hostlocal,设置拒绝访问


//创建地址列表文件
[root@tom03 init.d]# mkdir /etc/squid
[root@tom03 init.d]# cd /etc/squid/
[root@tom03 squid]# vim src.list
//查看文件里添加的IP
[root@tom03 squid]# cat src.list 
20.0.0.20

#重载服务
[root@tom03 squid]# service squid reload

进行访问测试

mark

mark

二:Squid日志分析工具Sarg

2.1:安装并配置Sarg

[root@tom03 ~]# tar zxvf sarg-2.3.7.tar.gz -C /opt
[root@tom03 ~]# cd /opt
[root@tom03 opt]# cd sarg-2.3.7/
[root@tom03 sarg-2.3.7]# yum install gd gd-devel pcre pcre-devel -y
//安装gc库 gcc gcc-c++之前装过了

[root@squid sarg-2.3.7]# mkdir /usr/local/sarg
[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@tom03 sarg]# pwd
/etc/sarg
[root@tom03 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 mailx.postfix              //发送邮件报告

434//
charset UTF-8                           //使用字符集

518//
weekdays 0-6                            //top排行的星期周期

525//
hours 0-23                               ///top排行的时间周期

633// 
www_document_root /var/www/html          //网页跟目录


//添加不计入站文件,添加的域名将不被显示
[root@tom03 sarg]# touch /usr/local/sarg/noreport
//创建sarg的软链接
[root@tom03 sarg]# ln -s /usr/local/sarg/bin/sarg /usr/local/bin/

//生成报告
[root@tom03 sarg]# sarg
SARG: 纪录在文件: 696, reading: 100.00%
SARG: 成功的生成报告在 /var/www/html/squid-reports/2020Sep07-2020Sep07

//提示报告在这个目录
[root@tom03 ~]# ls /var/www/html/squid-reports/
  images index.html

//下载httpd服务
[root@tom03 ~]# yum -y install httpd
//开启服务
[root@tom03 ~]# systemctl start httpd
//关闭防火墙跟防护功能
[root@tom03 ~]# systemctl stop firewalld.service
[root@tom03 ~]# setenforce 0
//查看端口
[root@tom03 ~]# netstat -ntap | grep httpd
tcp6       0      0 :::80                   :::*                    LISTEN      11516/httpd         

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

查看20.0.0.43/squid-reports

mark

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

[root@tom03 ~]# 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):

mark

四:squid反向代理

通过squid反向代理可以加速网站的访问速度,可以将不同的URL请求分发到后台不同的Web服务器上,同时互联网用户只能看到反向代理服务器的地址,加强了网站的访问安全。

4.1:反向代理概述

如果Squid反向代理服务器中缓存了该请求的资源,则将该请求的资源源直接返回给客户端;否则反向代理服务器将后台的WEB服务器请求资源,然后将请求的应答返回给客户,同时也将应答缓存在本地,提供给下一个请求者使用

mark

4.2:反向代理网站加速

工作机制

  • 缓存网页对象,减少重复请求

  • 将互联网请求轮询或按权重分配到内网Web服务器

  • 代理用户请求,避免用户直接访问web服务器,提高安全

mark

五:搭建反向代理实现加速

  • 环境介绍
名称 角色 地址
centos-1 squid 20.0.0.43
centos-2 web1 20.0.0.44
centos-3 web2 20.0.0.45
win 10 客户端 20.0.0.20:

5.1:添加一台web2服务器

web2下载httpd
[root@web2 ~]# yum -y install httpd
[root@web2 ~]# cd /var/www/html/
[root@web2 html]# vim index.html
//编写首页信息
<h1>this is shuai02</h1>

//编写web1首页信息
[root@web1 html]# cd /var/www/html/
[root@web1 html]# vim index.html 

//编写首页信息
<h1>this is shuai01</h1>
//重启关闭防火墙
[root@web1 html]# systemctl start httpd
[root@web1 html]# iptables -F
[root@web1 html]# setenforce 0

mark

mark

5.2:设置反向代理

#http_port 3128    //注释
http_port 20.0.0.43:80 accel vhost vport       //监听本机80端口 
cache_peer 20.0.0.44 parent 80 0 no-query originserver round-robin max_conn=30 weight=1 name=web1    //web1服务器最大访问30 权重1 
cache_peer 20.0.0.45 parent 80 0 no-query originserver round-robin max_conn=30 weight=1 name=web2    //web2服务器最大访问30 
权重为1
cache_peer_domain web1 web2 www.shuai.com    //访问shuai.com匹配web1跟web2节点

#关闭httpd服务,因为会占用80端口,后面设置代理需要用到80端口
[root@tom03 ~]# systemctl stop httpd

#重启squid服务
[root@tom03 ~]# service squid restart
正在关闭 squid...
正在启动 squid...

[root@tom03 ~]# netstat -ntap | grep squid
tcp        0      0 20.0.0.43:80            0.0.0.0:*               LISTEN      12:593/(squid-1)   

5.3:客户机设置域名解析和squi代理并测试

mark

mark

mark

客户机访问www.shuai.com查看是否代理成功

mark
mark

猜你喜欢

转载自blog.csdn.net/weixin_47151643/article/details/108458375
今日推荐