理论+实验——Squid 反向代理、Sarg日志分析、ACL访问控制

一、Squid反向代理

1.1 实验环境

服务器ip
squid20.0.0.21
web120.0.0.22
web220.0.0.23
client192.168.100.10

1.2 实验步骤

web1(20.0.0.22)

[root@web1 ~]# yum -y install httpd
[root@web1 ~]# systemctl start httpd
[root@web1 ~]# systemctl enable httpd
[root@web1 ~]# cd /var/www/html/
[root@web1 html]# vim index.html
<h1>web1</h1>

在这里插入图片描述
web2(20.0.0.23)

[root@web2 ~]# yum -y install httpd
[root@web2 ~]# systemctl start httpd
[root@web2 ~]# systemctl enable httpd
[root@web2 ~]# cd /var/www/html/
[root@web2 html]# vim index.html
<h1>web2</h1>

在这里插入图片描述
squid(20.0.0.21)
将squid压缩包拖到 /opt 目录下

[root@squid ~]# cd /opt
[root@squid opt]# yum -y install gcc gcc-c++
[root@squid opt]# tar zxvf squid-3.4.6.tar.gz
[root@squid squid-3.4.6]# ./configure --prefix=/usr/local/squid \          ###安装路径
> --sysconfdir=/etc \             ###配置文件所在目录
> --enable-arp-acl \             ###启动acl访问控制列表的功能
> --enable-linux-netfilter \        ###内核过滤
> --enable-linux-tproxy \          ###支持透明模式
> --enable-async-io=100 \         ###io优化
> --enable-err-language="Simplify_Chinese" \               ###报错提示支持中文   
> --enable-underscore \           ###在url中支持下划线
> --enable-poll \                 ###poll是一个功能,功能提升
> --enable-gnuregex              ###支持正则表达式
[root@squid squid-3.4.6]# make -j3
[root@squid squid-3.4.6]# make install
[root@squid squid-3.4.6]# ln -s /usr/local/squid/sbin/* /usr/local/sbin/        ###让系统能识别
[root@squid squid-3.4.6]# useradd -M -s /sbin/nologin squid
[root@squid squid-3.4.6]# chown -R squid.squid /usr/local/squid/var/
[root@squid squid-3.4.6]# vim /etc/squid.conf
http_access allow all
#http_access deny all
http_port 20.0.0.21:80 accel vhost vport
cache_peer 20.0.0.22 parent 80 0 no-query originserver round-robin max_conn=30 weight=1 name=web1
cache_peer 20.0.0.23 parent 80 0 no-query originserver round-robin max_conn=30 weight=1 name=web2
cache_peer_domain web1 web2 20.0.0.21

[root@squid squid-3.4.6]# cd /etc/init.d/
[root@squid init.d]# vim squid
#! /bin/bash
#chkconfig: 2345 90 25
PID="/usr/local/squid/var/run/squid.pid"
CONF="/etc/squid.conf"
CMD="/usr/local/squid/sbin/squid"

case "$1" in
   start)
     netstat -anpt | grep squid &> /dev/null
     if [ $? -eq 0 ]
     then
       echo "squid is running"
       else
       echo "正在启动 squid..."
       $CMD
     fi
   ;;
   stop)
     $CMD -k kill &> /dev/null
     rm -rf $PID &> /dev/null
   ;;
   status)
     [ -f $PID ] &> /dev/null
       if [ $? -eq 0 ]
         then
           netstat -anpt | grep squid
         else
           echo "squid is not running"
       fi
   ;;
   restart)
      $0 stop &> /dev/null
      echo "正在关闭 squid..."
         $0 start &> /dev/null
      echo "正在启动 squid..."
   ;;
   reload)
      $CMD -k reconfigure
   ;;
   check)
      $CMD -k parse
   ;;
   *)
      echo "用法:$0{start|stop|status|reload|check|restart}"
   ;;
esac

[root@squid init.d]# chmod +x squid
[root@squid init.d]# chkconfig --add squid
[root@squid init.d]# cd /opt/squid-3.4.6/

客户机浏览器登录:20.0.0.21
在这里插入图片描述
再点击一次会出现:
在这里插入图片描述

二、squid日志分析

先将sarg的软件包拖到 /opt 目录下

[root@squid opt]# yum -y install gd gd-devel       ###图像处理
[root@squid opt]# mkdir /usr/local/sarg
[root@squid opt]# tar zxvf sarg-2.3.7.tar.gz
[root@squid opt]# cd 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
[root@squid sarg-2.3.7]# make install
[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
190 #user_sort_field BYTES reverse         ###用户访问记录 连接次数、访问字节按降序排序
206 exclude_hosts /usr/local/sarg/noreport         ###不计入排序的站点列表文件
257 overwrite_report no          ###同名日志是否覆盖
289 mail_utility mail.postfix       ###发送邮件报告命令
434 charset UTF-8              ###使用字符集
518 weekdays 0-6             ###top排行的星期周期
525 hours 0-23               ###top排行的时间周期
633 www_document_root /var/www/html        ###网页根目录

[root@squid sarg]# yum -y install httpd
[root@squid sarg]# touch /usr/local/sarg/noreport
[root@squid sarg]# ln -s /usr/local/sarg/bin/sarg /usr/local/bin/
[root@squid sarg]# sarg
SARG: 纪录在文件: 17, reading: 100.00%
SARG: 成功的生成报告在 /var/www/html/squid-reports/2020Nov03-2020Nov03
[root@squid sarg]# cd /var/www/html/squid-reports/
[root@squid squid-reports]# ls
2020Nov03-2020Nov03  images  index.html
[root@squid squid-reports]# systemctl start httpd
Job for httpd.service failed because the control process exited with error code. See "systemctl status httpd.service" and "journalctl -xe" for details.

报错:进程存在,需要改下squid的配置文件的80端口

[root@squid squid-reports]# vim /etc/squid.conf
http_port 20.0.0.21:8080 accel vhost vport          ###将80端口改为8080

[root@squid squid-reports]# service squid stop
[root@squid squid-reports]# service squid start
正在启动 squid...
[root@squid squid-reports]# netstat -anpt | grep squid
tcp        0      0 20.0.0.21:8080          0.0.0.0:*               LISTEN      47538/(squid-1) 
[root@squid squid-reports]# systemctl start httpd
[root@squid squid-reports]# netstat -anpt | grep httpd
tcp6       0      0 :::80                   :::*                    LISTEN      47567/httpd  

客户机浏览器登录:20.0.0.21:8080
在这里插入图片描述
再点击一次会出现:
在这里插入图片描述
客户机浏览器登录:20.0.0.21/squid-reports
在这里插入图片描述
点击文件可以看到信息
在这里插入图片描述

[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)

客户机浏览器登录:20.0.0.21/squid-reports ###发现多出了日志信息
在这里插入图片描述
同时还可以设置周期性计划任务执行每天生成报告

[root@squid squid-reports]# crontab -e
0 0 * * * /bin/bash /usr/local/bin/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)

三、ACL访问控制

3.1 ACL访问控制方式

  • ACL(Access Control List,访问控制列表),可以针对源地址、目标地址、访问的URL路径、访问的时间等各种条件进行过滤
  • ACL访问控制的步骤
    ◆ 使用acl配置项定义需要控制的条件
    ◆ 通过http_access配置项对已定义的列表做允许或拒绝的访问控制

3.2 常用的ACL列表类型

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

3.3 设置ACL访问规则

[root@squid ~]# vim /etc/squid.conf
# should be allowed
acl hostlocal src 192.168.100.10/24  '//监控client客户端的主机(192.168.100.10/24)取名为hostlocal'
# Deny requests to certain unsafe ports
http_access deny hostlocal  '//调用hostlocal,设置拒绝访问'
[root@squid ~]# service squid restart

client客户端尝试访问web端是,就会显示错误,访问不了

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值