squid原理与实验 (传统/透明模式,ACL访问控制,sarg日志)!!!

Squid概述

squid是一种用来缓存Internet数据的软件。接受来自客户机需要下载的目标的请求并适当的处理这些请求。也就是说,如果一个客户端想要打开默认网站,它请求squid为它取得这个页面。squid随之连接到远程服务器并向这个页面发出请求。然后,squid显式地聚集数据到客户端机器,而且同时复制一份。当下一次有人需要同一页面时,squid可以简单的从磁盘中读到它,那样数据会立即传输到客户机上。

Squid(Squid cache,简称Squid)是Linux系统中最常用的一款开源代理服务软件,可以很好地实现HTTP和FTP,以及DNS查询、SSL等应用的缓存代理,功能十分强大,本篇博客详细介绍了传统代理、透明代理,squid日志分析的配置

1.知识拓展

Squid相当于阿里云的CDN

静态资源可以被缓存加速在CDN服务器上

CDN上跑的都是BGP,同样云平台也是如此

BGP有个特点,允许多线接入,不同运营商接入

2.Squid工作机制

在这里插入图片描述
工作原理:

●客户机访问www.163.com,中间有一个squid代理服务器,提供缓存加速的功能,
它会找相对应的缓存资源,如果没有资源,squid会主动找源站服务器拿资源,而不是客户机去找源站,然后163.com把资源下载到squid代理器,代理器再把资源返回给客户机 (1-5-6-4)

●如果它有资源,会直接通过squid将数据返回给客户机 1-2-3-4

●不管有没有资源,客户机请求的地址都是163.com,只不过squid代理端在中间,客户机需要在本地设置代理IP用于加速访问,代理IP指向squid服务器,但请求的时候仍然是163

3.代理的类型与好处

代理的类型:

传统代理:适用于lnternet,需要明确指定服务器
透明代理:客户机不需要指定代理服务的地址和端口,而是通过默认路由、防火墙策略将Web访问重定向给代理服务器处理

使用代理的好处

提高Web访问速度
隐藏客户机的真实IP地址

在这里插入图片描述

Squid代理实验

1.实验环境

服务器IP网关
squid双网卡:20.0.0.21/192.168.100.8820.0.0.1/192.168.100.1
web服务器20.0.0.2220.0.0.1
客户机192.168.100.188192.168.100.88

2.安装Squid

######配置20.0.0.21  squid ########
[root@localhost ~]# hostnamectl set-hostname squid
[root@localhost ~]# su

#############20.0.0.21  squid ########
上传squid-3.4.6.tar.gz到opt

[root@squid ~]# cd /opt/
[root@squid squid-3.4.6]# tar zxvf squid-3.4.6.tar.gz
[root@squid squid-3.4.6]# yum -y install gcc gcc-c++

[root@squid squid-3.4.6]# cd squid-3.4.6/
./configure \
--prefix=/usr/local/squid \
--sysconfdir=/etc \
--enable-arp-acl \
--enable-linux-netfilter \
--enable-linux-tproxy \
--enable-async-io-100 \
--enable-err-language="Simplify Chinese" \
--enable-underscore \
--enable-poll \
--enable-gnuregex
################注释####################
--prefix=/usr/local/squid \             ######指定安装路径
--sysconfdir=/etc \                         ###配置文件所存在的目录
--enable-arp-acl \                           ###启用acl访问控制列表,基于访问控制列表进行管理,主要对于mac地址的管控
--enable-linux-netfilter \                  ###内核过滤
--enable-linux-tproxy \                   ###开启支持透明模式 
--enable-async-io-100                    ###对IO的优化
--enable-err-language="Simplify Chinese" \   ###报错提示设置成中文
--enable-underscore \                      ###支持下划线
--enable-poll \                                  ###poll功能提升
--enable-gnuregex                          ###支持正则表达式
##########make
[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/

为什么修改/var权限,因为里面有一个run,跑里面当中的PID运行文件

###修改配置文件,主要分配缓存空间#######里面全是acl控制
[root@squid squid-3.4.6]# cd /etc/
[root@squid etc]# vim squid.conf
http_access allow all        ###默认允许所有访问,自上往下匹配,所以拒绝所有不注释也行
http_access deny all      ###默认拒绝所有访问

http_port 3128   在这个下面添加
cache_effective_user squid                         
cache_effective_group squid



#####解释:
acl                 ###访问控制
localnet         列表名称
src                 基于源地址或基于目标地址段落
10.0.0.0/8      基于具体的IP还是IP段落


#####################检查语法
[root@squid etc]# squid -k parse
2020/10/30 11:52:15| Startup: Initializing Authentication Schemes ...
......................省略部分///
2020/10/30 11:52:15| Processing: refresh_pattern .		0	20%	4320

####语法检查没问题后,初始化########
[root@squid etc]# squid -z              ###这一步耐心等待
2020/10/30 11:54:02 kid1| Set Current Directory to /usr/local/squid/var/cache/squid          ####设置当前目录为/usr/local/squid/var/cache/squid
2020/10/30 11:54:02 kid1| Creating missing swap directories                                                ####创建丢失的交换目录
2020/10/30 11:54:02 kid1| No cache_dir stores are configured.                                             ####kid1|没有配置cache_dir存储
这个是容灾,刚刚修改配置文件的时候没做,但是不影响

###########启动squid
[root@squid etc]# squid
[root@squid etc]# netstat -anutp | grep 3128
tcp6       0      0 :::3128                 :::*                    LISTEN      36726/(squid-1)     


####为了便于server管理,
[root@squid etc]# 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 -natp | 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 -natp | 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|reload|status|check|restart}"
     ;;
esac


[root@squid init.d]# chmod +x squid 
[root@squid init.d]# chkconfig --add squid                              ###让service列表中添加这个名称
[root@squid init.d]# chkconfig --level 35 squid on                  开机自启动,35说的是init 3字符界面   init 5图行界面


#####测试脚本有没有问题###########
[root@squid init.d]# service squid start
正在启动 squid...
[root@squid init.d]# service squid status
tcp6       0      0 :::3128                 :::*                    LISTEN      75354/(squid-1)     
[root@squid init.d]# service squid stop
[root@squid init.d]# service squid status
squid is not running
[root@squid init.d]# service squid start
正在启动 squid...

#########这里完成之后,接下来开始配置传统模式与透明模式######################

4.传统模式配置

#######在squid代理服务器上操作####20.0.0.21###
[root@squid init.d]# vim /etc/squid.conf
##去添加###
56 http_access allow all            ###默认允许所有访问,自上往下匹配,所以拒绝所有不注释也行
57 http_access deny all             ###默认拒绝所有访问

#指定缓存功能所使用的内存空间大小,便于保持访问频繁的WEB对象,容量最好为4的倍数,单位MB
63 cache_mem 64 MB

#允许用户下载的最大文件大小,以字节为单位,默认设置为0,表示不限制
64 reply_body_max_size 10 MB

#允许保存到缓存空间最大对象大小,以KB为单位,超过大小限制的文件不被缓存,而是直接转去源站
65 maximum_object_size 4096 KB


###因为涉及到iptables防火墙规则转发###
[root@squid ~]# iptables -L            ##查看防火墙规则
[root@squid ~]# iptables -F           ##清空规则
[root@squid ~]# iptables -t nat -L   ###查看nat表规则
[root@squid ~]# iptables -t nat -F   ###清空nat表规则

[root@squid ~]# iptables -I INPUT -p tcp --dport 3128 -j ACCEPT   ##在filter表的开头添加一条规则:允许tcp协议,从3128端口访问

[root@squid ~]# service squid restart        重载配置文件
正在关闭 squid...
正在启动 squid...


#################下面配置web服务器:20.0.0.22########
[root@localhost ~]# hostnamectl set-hostname web
[root@localhost ~]# su

[root@web ~]# yum -y install httpd
[root@web ~]# netstat -anutp | grep httpd
tcp6       0      0 :::80                   :::*                    LISTEN      38520/httpd         

####这个时候你用浏览器访问一下WEB服务器,20.0.0.22####然后
[root@web ~]# cd /var/log/httpd/
[root@web httpd]# ls
20.0.0.1 - - [30/Oct/2020:21:54:29 +0800] "GET / HTTP/1.1" 403 4897 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.111 Safari/537.36"

###因为你没做代理,所以是笔记本直接访问的源站####

在这里插入图片描述

###下面我们在浏览器做代理####首先清除历史浏览记录!!!!!!
打开谷歌浏览器,点击设置,点击高级,点击设置,点击打开您计算机的代理设置
点击代理服务器,填写
地址:20.0.0.21(代理服务器的地址)  端口:3128

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

####打开浏览器,还是访问20.0.0.22#####
[root@web httpd]# tail -2 access_log 
20.0.0.21 - - [30/Oct/2020:22:02:52 +0800] "GET /noindex/css/fonts/Bold/OpenSans-Bold.ttf HTTP/1.1" 404 238 "http://20.0.0.22/noindex/css/open-sans.css" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.111 Safari/537.36"
20.0.0.21 - - [30/Oct/2020:22:02:52 +0800] "GET /noindex/css/fonts/Light/OpenSans-Light.ttf HTTP/1.1" 404 240 "http://20.0.0.22/noindex/css/open-sans.css" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.111 Safari/537.36"
###可以看到去访问的是代理服务器访问的IP地址,OK  传统代理成功######

5.透明代理配置

在做透明代理之前,先要了解一下网络,这里我画了一张图以便更好的了解下面的操作
在这里插入图片描述
双网卡配置过程
在这里插入图片描述
在这里插入图片描述

################首先搞一下网络################
squid配置双网卡
nat:20.0.0.21
仅主机:192.168.100.88

####配置双网卡:
[root@squid ~]# nmcli connection                               ###查看UUID
名称        UUID                                  类型            设备   
ens33       f4ed258a-4300-45bb-b41b-27af30fd273b  802-3-ethernet  ens33  
virbr0      0e979ba7-81e8-4494-86eb-15855effb54a  bridge          virbr0 
有线连接 1  a454dfa9-79ba-36ef-ac8d-e9be5cd5bc84  802-3-ethernet  ens36  


[root@squid ~]# cd /etc/sysconfig/network-scripts/
[root@squid network-scripts]# cp -p ifcfg-ens33 ifcfg-ens36
33换成36
换UUID,然后留下两行就够了
IPADDR=192.168.100.88
NETMASK=255.255.255.0

[root@squid network-scripts]# systemctl restart network
[root@squid network-scripts]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 20.0.0.21  netmask 255.255.255.0  broadcast 20.0.0.255

ens36: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.100.88  netmask 255.255.255.0  broadcast 192.168.100.

####路由重分发####
[root@squid network-scripts]# vim /etc/sysctl.conf 
[root@squid network-scripts]# sysctl -p
net.ipv4.ip_forward = 1

####用web服务:20.0.0.22 ping 192.168.100.88 看网络是否连通###
[root@web httpd]# ping 192.168.100.88
PING 192.168.100.88 (192.168.100.88) 56(84) bytes of data.
64 bytes from 192.168.100.88: icmp_seq=1 ttl=128 time=1.43 ms

###为了让数据能出去也能回来,添加一条静态路由#########
[root@web ~]# route add -net 192.168.100.0/24 gw 20.0.0.21



####这个时候回到squid服务器开启透明代理####
[root@squid ~]# vim /etc/squid.conf
###将原本的http_port 3128改成以下这条###
60 http_port 192.168.100.88:3128 transparent

####修改完以后重启squid服务########
[root@squid ~]# service squid stop
[root@squid ~]# service squid start
正在启动 squid...


###为了确保端口能放通,最好先清理所有规则
[root@squid ~]# iptables -F                                
[root@squid ~]# iptables -t nat -F

###下面做防火墙规则####
##在nat表第一行添加一条路由选择前处理规则:所有从ens36网口来访问的192.168.100.0/24网段地址,用的是TCP协议的,访问的是80端口的,变成访问3128端口    
[root@squid ~]# iptables -t nat -I PREROUTING -i ens36 -s 192.168.100.0/24 -p tcp --dport 80 -j REDIRECT --to 3128  

##在nat表第一行添加一条路由选择前处理规则:所有从ens36网口来访问的192.168.100.0/24网段地址,用的是TCP协议的,访问的是443端口的,变成访问3128端口
[root@squid ~]# iptables -t nat -I PREROUTING -i ens36 -s 192.168.100.0/24 -p tcp --dport 443 -j REDIRECT --to 3128

##在filter表第一行添加处理入站规则:使用tcp协议,目的端口是3128的请求全部放通
[root@squid ~]# iptables -I INPUT -p tcp --dport 3128 -j ACCEPT


####这几有几个注意点####
1:首先外网访问内网使用的是PREROUTING链,对于WEB来说,你是外网,所以squid的配置规则为上面三条
2:放通的端口不仅仅是80  还要有443 ,因为有http和https


#######做完以后,为了验证代理服务器,重新开一台仅主机网卡的Linux#######
网咯地址:192.168.100.188
网关指向:192.168.100.88

打开火狐访问20.0.0.22:WEB服务器,然后去WEB主机查看日志,看一下访问地址
[root@web httpd]# tail -3 access_log 
20.0.0.21 - - [30/Oct/2020:22:50:31 +0800] "GET / HTTP/1.1" 403 4897 "-" "Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Firefox/52.0"
20.0.0.21 - - [30/Oct/2020:22:50:31 +0800] "GET /noindex/css/bootstrap.min.css HTTP/1.1" 304 - "http://20.0.0.22/" "Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Firefox/52.0"
20.0.0.21 - - [30/Oct/2020:22:50:31 +0800] "GET /noindex/css/open-sans.css HTTP/1.1" 304 - "http://20.0.0.22/" "Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Firefox/52.0"
###可以看到,是代理服务器访问的,透明代理完成#########

ACL访问控制

vim /etc/squid.conf

acl localhost src 20.0.0.21/32                               ###可以对固定的IP设置,具体IP/32
acl MYLAN src 20.0.0.0/24                                   ###可以对网段设置
acl destionhost dst 20.0.0.22/32                             ###可以对目标地址设置
acl MC20 maxconn 20                                          ###访问数量最大上限值
acl BURL url_regex -i ^http://  ^https://                    ###可以使用正则表达式的结构,一般是以开头是什么什么协议,http/https
acl PURL urlpath_regex -i \.mp$ \.mp4$ \.rmvb$          ###或者基于访问的资源的末尾,比如。MP3.mp4
acl work time MTWHF 08:30-17.30                             ###基于访问的时间,比如:星期一到星期五的 8.30-17.30                                     ###MTWHF这个是星期1-星期5的缩写
下面可以加允许或者拒绝

####我们可以定义到文件当中,对文件中内容整体进行控制

mkdir /etc/squid             ##启用对象列表管理
cd //etc/squid/

vim dest.list

         192.168.175.140
         192.168.188.140
         192.168.195.140

vim /etc/squid.conf

          acl destionhost dst "/etc/squid/dest.list"       ###定义这个文件的列表名称destionhost,dst表示以文件类型+绝对路径
               

          http_access deny destionhost                        ###拒绝列表(注意配置),也可以允许
                                                                                 ###注意要加上关键词http_access
service squid restart 



###下面进行操作#####
vim /etc/squid.conf
7行下面插入
acl localzeng src 192.168.100.0/24         


31行下面添加一条
http_access deny localzeng           ###拒绝访问

service squid reload      

###192.168.100.88的主机打开火狐访问20.0.0.22,访问不了了###

去除上面的两条规则后
###再用192.168.100.88的主机打开火狐访问20.0.0.22,可以访问###
########################################################################################

sarg日志分析

##将sarg-2.3.7.tar.gz拖进opt
[root@squid opt]# yum -y install gcc gcc-c++

[root@squid opt]# tar zxvf sarg-2.3.7.tar.gz
[root@squid opt]# cd sarg-2.3.7/

##在装sarg之前一定要gd gd-devel,它是处理图片,或者生成图片的####
[root@squid sarg-2.3.7]# yum -y install gd gd-devel

##创建一个工作目录###
[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

############注释######################
--prefix=/usr/local/sarg \               ###安装路径
--sysconfdir=/etc/sarg \                 ###配置文件路径
--enable-extraprotection                ###开启安全防护

###make安装######
[root@squid sarg-2.3.7]# make && make install

###修改配置文件######
[root@squid sarg-2.3.7]# cd /etc/sarg/
[root@squid sarg]# ls
css.tpl  exclude_codes  sarg.conf  user_limit_block

[root@squid sarg]# vim sarg.conf          ###可以看到里面全被注释了

###你怎么知道全被注释了,过滤出是以#号开头的行######
[root@squid sarg]# grep -v "^#" sarg.conf

#####接下来去开启功能就行了####
[root@squid sarg]# vim sarg.conf 
————和下面相同的话注释#号就可以了,不同的复制————

7         access_log /usr/local/squid/var/logs/access.log              ###指定sarg的访问日志文件!!

25       title "Squid User Access Reports"                                     ###网页标题:因为以网站形式显示,所以要有个标题

120     output_dir /var/www/html/squid-reports                        ###报告输出目录

178     user_ip no                                                                         ###使用用户名显示

206     exclude_hosts /usr/local/sarg/noreport                            ###不计入排序的站点列表文件,等会你要去创建这个文件,不会自动产生

184     topuser_sort_field connect reverse                                    ###top排序中有连接次数,访问字节,降序排序   升序的话是normal

190##如果184注释了这行就不用了     user_sort_field reverse        ###用户访问记录 连接次数,访问字节按降序

257     overwrite_report no                                                           ### 同名日志是否覆盖,no不覆盖

289     mail_utility mailq.postfix                                                     ###发送邮件报告命令,用系统自带的mailq.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]# touch /usr/local/sarg/noreport

##为了方便操作,为了系统所能识别#####
[root@squid sarg]# ln -s /usr/local/sarg/bin/sarg /usr/local/bin/

###########生成报告###########
[root@squid sarg]# sarg
SARG: Unknown sort criterion "reverse" for parameter "user_sort_field"      这里提示错误

[root@squid sarg]# !vim
190行注释掉
#user_sort_field reverse

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

###生成的报告在哪呢?################
[root@squid squid-reports]# cd /var/www/html/squid-reports/
[root@squid squid-reports]# ls
2020Oct31-2020Oct31  images  index.html

可以看到页面以及这些服务

#####安装httpd##########
[root@squid squid-reports]# yum -y install httpd
[root@squid squid-reports]# systemctl restart httpd

##再看下站点还在不在#######
[root@squid squid-reports]# ls
2020Oct31-2020Oct31  images  index.html

################下面开始访问试试##################

###下面这两个地址都可以访问到,因为这两个地址都是squid的IP地址#########
http://192.168.100.88/squid-reports/    
http://20.0.0.21/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后面跟的是小写的L

##结合crontab -e
0 0 * * * /bin/bsah /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)

###解释#####
###当天的日期###
[root@squid ~]# date +%d/%m/%Y,如果不加,看的是当天的
31/10/2020

##前一天的日期
[root@squid ~]# date -d "1 day ago" +%d/%m/%Y         ##如果不加,看的只有当天的
30/10/2020

 
#####先去命令行敲一下
[root@squid ~]# 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: TAG: access_log /usr/local/squid/var/logs/access.log
SARG: TAG: title "Squid User Access Reports"
SARG: TAG: output_dir /var/www/html/squid-reports
SARG: TAG: user_ip no
SARG: TAG: topuser_sort_field connect reverse
SARG: TAG: exclude_hosts /usr/local/sarg/noreport
SARG: TAG: overwrite_report no
SARG: TAG: mail_utility mailq.postfix
SARG: TAG: charset UTF-8
SARG: TAG: weekdays 0-6
SARG: TAG: hours 0-23
SARG: TAG: www_document_root /var/www/html
SARG: 纪录在文件: 31, reading: 100.00%
SARG: 期间被日志文件覆盖: 31/10/2020 - 31/10/2020
SARG: (info) date=31/10/2020
SARG: (info) period=2020 10 30-2020 10 31
SARG: (info) outdirname=/var/www/html/squid-reports//2020Oct30-2020Oct31
SARG: (info) Dansguardian report not produced because no dansguardian configuration file was provided
SARG: (info) No redirector logs provided to produce that kind of report
SARG: (info) No downloaded files to report
SARG: (info) Authentication failures report not produced because it is empty
SARG: (info) Redirector report not generated because it is empty
SARG: 成功的生成报告在 /var/www/html/squid-reports//2020Oct30-2020Oct31


##########再去访问,发现变成两个了###########
http://192.168.100.88/squid-reports/    
http://20.0.0.21/squid-reports/

在这里插入图片描述

反向代理

############反向代理############################
####去WEB服务器:20.0.0.22下:
[root@localhost ~]# cd /var/www/html/
[root@localhost html]# ls
[root@localhost html]# echo "11111111111111111" > index.html
[root@localhost html]# ls
index.html

###用浏览器访问一下试试有没有问题########


####再开一台WEB服务器:20.0.0.23##########
[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# setenforce 0

[root@localhost ~]# yum -y install httpd
[root@localhost ~]# cd /var/www/html/
[root@localhost html]# ls
[root@localhost html]# echo "2222222222222222" > index.html
[root@localhost html]# ls
index.html
[root@localhost html]# systemctl start httpd

###用浏览器访问一下试试有没有问题########

#####到squid服务器去设置反向代理
[root@squid ~]# vim /etc/squid.conf

##修改60行,并在下面添加三行####
60 http_port 20.0.0.21:80 accel vhost vport                            ####你的监听的地址和端口,
61 cache_peer 20.0.0.22 parent 80 0 no-query originserver round-robin max_conn=30 wcight=1 name=web1
62 cache_peer 20.0.0.27 parent 80 0 no-query originserver round-robin max_conn=30 wcight=1 name=web2
63 cache_peer_domain web1 web2 www.zeng.com 20.0.0.21

###注释#########
cache_peer_domain web1 web2 www.zeng.com 20.0.0.2




####打开客户机######
[root@localhost ~]# ping 20.0.0.22
PING 20.0.0.22 (20.0.0.22) 56(84) bytes of data.
64 bytes from 20.0.0.22: icmp_seq=1 ttl=63 time=0.514 ms

[root@localhost ~]# ping 20.0.0.27
PING 20.0.0.27 (20.0.0.27) 56(84) bytes of data.
64 bytes from 20.0.0.27: icmp_seq=1 ttl=63 time=0.875 ms


###两个网段都能同,没问题,打开浏览器发现两个都不让访问#########
是因为我们之前只放通3128的端口,这个时候我们需要去所有服务器节点去放通80端口

###web1:20.0.0.22的操作####因为这个时候不需要透明代理,我们改用传统代理,所以你的防火墙规则可以清空了####
###为了确保能够访问,所有节点都刷新这三条####
iptables -F
iptables -t nat -F
iptables -I INPUT -p tcp --dport 80 -j ACCEPT

###用浏览器访问可以看到全部能够成功访问了####
###因为我们之前做日志,导致squid:20.0.0.2180端口被占用了,所以你访问20.0.0.21时,访问的是20.0.0.21的本机而不是它后端的两台WEB服务器
###所以需要去关闭squid的80端口
[root@squid ~]# systemctl stop httpd
[root@squid ~]# netstat -anutp | grep 80
[root@squid ~]# service squid restart

####这个时候修改客户机hosts文件,然后访问####
20.0.0.21 www.zeng.com

###发现轮询了,查看访问地址是否为squid:20.0.0.21####
###查看20.0.0.22WEB
[root@localhost html]# tail -f /var/log/httpd/access_log 
20.0.0.21 - - [01/Nov/2020:00:15:19 +0800] "GET / HTTP/1.1" 304 - "-" "Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Firefox/52.0"
20.0.0.21 - - [01/Nov/2020:00:15:19 +0800] "GET /favicon.ico HTTP/1.1" 404 209 "-" "Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Firefox/52.0"
20.0.0.21 - - [01/Nov/2020:00:15:19 +0800] "GET /favicon.ico HTTP/1.1" 404 209 "-" "Mozilla/5.0 (X11


###查看20.0.0.23WEB
20.0.0.21 - - [01/Nov/2020:00:22:09 +0800] "GET /success.txt HTTP/1.1" 404 209 "-" "Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Firefox/52.0"
20.0.0.21 - - [01/Nov/2020:00:22:09 +0800] "GET /success.txt HTTP/1.1" 404 209 "-" "Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Firefox/52.0"
20.0.0.21 - - [01/Nov/2020:00:22:09 +0800] "GET /success.txt HTTP/1.1" 404 209 "-" "Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Firefox/52.0"

#####OK 实验完成#######

在这里插入图片描述
在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值