如何搭建squid反向代理?如何配置ACL访问控制与sarg日志?

前言

一:环境

1.1:环境

  • 继承上一个试验的环境:squid传统代理和透明代理
  • https://blog.csdn.net/CN_TangZheng/article/details/104226442
  • VMware软件
  • 一台centos7虚拟机作为squid服务器,添加一个网卡,IP地址为:192.168.79.133和192.168.10.1(仅主机模式)
  • 一台centos7虚拟机作为web服务器,IP地址为:192.168.79.134
  • 一台win10虚拟机作为client测试机,IP地址为:192.168.10.10(仅主机模式)

1.2:实验目的

  • 通过ACL访问控制实现对主机允许和拒绝访问
  • 通过sarg部署,每天生成日志文件,方便访问
  • 实现squid的反向代理

二:ACL访问控制

2.1:概述

  • ACL(Access Control List,访问控制列表),可以针对源地址、目标地址、访问的URL路径、访问的时间等各种条件进行过滤

  • ACL访问控制的步骤

    • 使用acl配置项定义需要控制的条件
    • 通过http_access配置项对已定义的列表做允许或拒绝的访问控制
  • ACL一些解释

    src          源地址
    dst              目标地址
    port            目标地址
    dstdomain       目标域
    time            访问时间
    maxconn       最大并发连接
    url_regex   目标URL地址 
    urlpath_regex  整个目标URL路径 
    

2.2:典型的访问规则配置

  • 设置ACL访问规则

  • [root@squid ~]# vim /etc/squid.conf	'//编辑squid配置文件'
    # should be allowed
    acl hostlocal src 192.168.10.10/32  '//监控client客户端的主机(192.168.10.10/32)取名为hostlocal'
    # Deny requests to certain unsafe ports
    http_access deny hostlocal  '//调用hostlocal,设置拒绝访问'
    [root@squid ~]# service squid restart
    
  • client客户端尝试访问web端,client客户端先清除浏览器数据

  • mark

  • ACL策略设置成功,已拒绝192.168.10.10主机访问

  • 因为我们接下来还需要使用client客户端做测试,所以删除刚刚设置的ACL规则,并重启squid服务

三:sarg日志

3.1:在squid服务器上部署sarg

  • [root@squid ~]# cd /mnt/company/
    [root@squid company]# tar zxvf sarg-2.3.7.tar.gz -C /opt	'//解压源码包'
    [root@squid company]# cd /opt/sarg-2.3.7/
    [root@squid sarg-2.3.7]# yum install gd gd-devel -y	'//安装gd库,gcc gcc-c++之前装过了'
    [root@squid sarg-2.3.7]# mkdir /usr/local/sarg	'//创建sarg目录'
    [root@squid sarg-2.3.7]# ./configure --prefix=/usr/local/sarg \		'//指定sarg目录'
    > --sysconfdir=/etc/sarg \	'//配置文件'
    > --enable-extraprotection	'//开启安全防护'
    [root@squid sarg-2.3.7]# make && make install
    

3.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-2.3.7]# sarg	'//生成报告'
    SARG: 纪录在文件: 123, reading: 100.00%
    SARG: 成功的生成报告在 /var/www/html/squid-reports/2020Feb08-2020Feb08	'//提示报告生成在这个目录,我们进入这个目录查看一下'
    [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
    
    

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

  • 查看192.168.79.133/squid-reports或者192.168.10.1/squid-reports

  • mark

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

    [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)
    
    
  • 再次查看sarg

  • mark

四:squid反向代理

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

  • 添加一台web2服务器,IP地址为192.168.79.135

  • '//原本的web服务器修改一下主页内容用来和新添加的web2服务器区分'
    [root@web ~]# cd /var/www/html
    [root@web html]# vim index.html	'//修改首页内容'
    <h1>this is erbao web!</h1>
    '//web2服务器设置'
    [root@web2 ~]# yum install httpd -y	'//安装httpd服务'
    [root@web2 ~]# vim /var/www/html/index.html	'//修改首页内容'
    <h1>this is sanbao web!</h1>
    [root@web2 ~]# systemctl stop firewalld.service '//关闭防火墙'	
    [root@web2 ~]# setenforce 0
    [root@web2 ~]# systemctl start httpd	'//开启httpd服务'
    [root@web2 ~]# route add -net 192.168.10.0/24 gw 192.168.79.133	'//添加一条静态路由,下一条指向squid服务器(因为squid服务器上有两个网卡)'
    
    
  • web和web2访问测试

  • mark

  • mark

4.2:squid服务器设置

  • 关闭httpd服务并设置防火墙规则

  • [root@squid squid-reports]# systemctl stop httpd	'//关闭httpd服务,因为会占用80端口,后面设置代理需要用到80端口'
    [root@squid squid-reports]# systemctl start firewalld.service 
    [root@squid squid-reports]# iptables -F
    [root@squid squid-reports]# iptables -t nat -F
    [root@squid squid-reports]# iptables -I INPUT -p tcp --dport 3128 -j ACCEPT
    
    
  • 设置反向代理

  • [root@squid squid-reports]# vim /etc/squid.conf
    http_port 192.168.79.133:80 accel vhost vport	'//监控本机80端口'
    cache_peer 192.168.79.134 parent 80 0 no-query originserver round-robin max_conn=30 weight=1 name=web1	'//节点服务器1最大访问30,权重1,别名web1'
    cache_peer 192.168.79.135 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 squid-reports]# netstat -ntap |grep 80	'//查看80端口是否被squid使用'
    tcp        0      0 192.168.79.133:80       0.0.0.0:*               LISTEN      5197/(squid-1)  
    

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

  • 设置域名解析(以administrator用户登录)

  • C盘-Windows-system32-drivers-etc-hosts

  • mark

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

  • mark

  • mark

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

  • mark

  • mark

  • 反向代理成功,实验结束

发布了126 篇原创文章 · 获赞 62 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/CN_TangZheng/article/details/104229912
今日推荐