|NO.Z.00019|——————————|^^ 构建 ^^|——|squid缓存/代理服务.V4|——|4台server|

一、搭建squid反向代理模式:实验拓扑
### --- 搭建squid反向代理模式:实验拓扑

~~~     10.10.10.11    10.10.10.12两台部署在内网的Apache服务器,
~~~     给公网用户缓存内网服务器里面的数据。加速公网用户的访问。
~~~     CDN也是这个原理;一部分由智能DNS(DNSport),
~~~     第二部分:缓存服务器(有很多,squid是一种方式)
~~~     squid的反向代理模式,可以很好的做到CDN的缓存功能
~~~     SQUID有两块网卡10.10.10.13和内网相连,20.20.20.13和公网相连
~~~     20.20.20.13:公网的客户端
~~~     若是Apache1和Apache2提供服务的话,若是这两个web服务文件不一致的情况下,
~~~     在客户端可以看到文件刷新,若是把两台服务都停止,它会保存最后一个获取的作为缓存。
~~~     主要是给公网用户缓存代理数据;
~~~     这里的SQUID依然没有后端健康状态监测的功能
~~~     并且它的算法是RR,也就说明后端代理的节点是高可用的(因为他没有后端健康状态监测的)
二、硬件环境准备
### --- 硬件环境准备

~~~     HA-server1:10.10.10.11:Apache:内网
~~~     HA-server2:10.10.10.12:Apache:内网
~~~     HA-server3:10.10.10.13:SQUID:内网
~~~     HA-server3:20.20.20.13:SQUID:内网
~~~     HA-server4:20.20.20.13:client:公网
三、实验专题:搭建squid反向代理模式
1、10.10.10.11    10.10.10.12配置web服务
### --- 10.10.10.11    10.10.10.12配置web服务
### --- 配置10.10.10.11
### --- 启动Apache服务器

[root@server11 ~]# service httpd start
[root@server11 ~]# chkconfig httpd on
[root@server11 ~]# echo "this is server 1" >> /var/www/html/index.html
[root@server11 ~]# curl localhost
this is server 1
### --- 把网关指向10.10.10.13

[root@server11 ~]# echo "GATEWAY=10.10.10.13" >> /etc/sysconfig/network-scripts/ifcfg-eth0
[root@server11 ~]# service network restart
[root@server11 ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
10.10.10.0      0.0.0.0         255.255.255.0   U     1      0        0 eth0
0.0.0.0         10.10.10.13     0.0.0.0         UG    0      0        0 eth0
### --- 配置10.10.10.12
### --- 启动Apache服务器

[root@server12 ~]# service httpd start
[root@server11 ~]# chkconfig httpd on
[root@server12 ~]# echo "this is server 2" >> /var/www/html/index.html
[root@server12 ~]# curl localhost
this is server 2
### --- 把网关指向10.10.10.13

[root@server12 ~]# echo "GATEWAY=10.10.10.13" >> /etc/sysconfig/network-scripts/ifcfg-eth0
[root@server12 ~]# service network restart
[root@server12 ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
10.10.10.0      0.0.0.0         255.255.255.0   U     1      0        0 eth0
0.0.0.0         10.10.10.13     0.0.0.0         UG    0      0        0 eth0
2、配置SQUID服务
### --- 在10.10.10.13下配置
### --- 配置ifcfg-eth1网卡

[root@server13 ~]# vim /etc/sysconfig/network-scripts/ifcfg-eth1 
DEVICE=eth1
ONBOOT=yes
BOOTPROTO=static
IPADDR=20.20.20.13
NETMASK=255.255.255.0 
[root@server13 ~]# service network restart
[root@server13 ~]# ip addr
eth0: 10.10.10.13/24 brd 10.10.10.255
eth1: 20.20.20.13/24 brd 20.20.20.255
### --- 部署squid服务
 
[root@server13 ~]# yum install -y squid
[root@server13 ~]# vim /etc/squid/squid.conf
# Squid normally listens to port 3128
http_port 20.20.20.13:80 vhost                                              // 添加公网地址   端口号:80 vhost:表示反向代理模式
cache_peer 10.10.10.11 parent 80 0 originserver round-robin                 // cache_peer 10.10.10.11指定真实服务器或者缓存池 parent:表示上游模式 80:端口   0:表示当前环境中还有没有squid服务器,没有了 originserver:表示它是它的字节点 round-robin:调度算法
cache_peer 10.10.10.12 parent 80 0 originserver round-robin 
[root@server13 ~]# service squid start
### --- 打开路由转发功能

[root@server13 ~]# vim /etc/sysctl.conf 
net.ipv4.ip_forward = 1
[root@server13 ~]# sysctl -p
net.ipv4.ip_forward = 1
3、配置client客户端
### --- 在10.10.10.14下配置
### --- 更改客户端的IP地址

[root@server14 ~]# vim /etc/sysconfig/network-scripts/ifcfg-eth0 
DEVICE=eth0
ONBOOT=yes
BOOTPROTO=static
IPADDR=20.20.20.14
NETMASK=255.255.25.0
[root@server14 ~]# service network restart
4、验证:
### --- 在HA-server4虚拟主机下通过Firefox访问http://20.20.20.13地址:显示访问被拒绝
~~~     访问被拒绝说明:客户端访问到web服务器了,但是web服务器没有搭理客户端。
~~~     加入ACL方式设置验证
~~~     原因分析

[root@server13 ~]# vim /etc/squid/squid.conf
acl localnet src 10.0.0.0/8                                                 // acl定义范围。名称为localnet。类型为src,源地址类,10.0.0.0/8这个地址刚好匹配容纳到了10.10.10.1这个网段,
http_access allow localhost                                                 // 说明放行了刚才定义的localhost这么一个src范围,凑巧的是10在这里,所以10被放行了
### --- 若是想让放行的话,把20网段加到放行列表里

acl localnet src 20.20.20.0/24  # RFC1918 possible internal network 

四、配置ACL
### --- 在HA-server3下配置

[root@server13 ~]# vim /etc/squid/squid.conf
acl localnet src 10.0.0.0/8     # RFC1918 possible internal network         // 在改行下面添加如下内容
acl localnet src 20.20.20.0/24  # RFC1918 possible internal network
[root@server13 ~]# service squid reload                                     // 重启若是提示“visible_hostname:在配置文件中给他定义一个域名即可”不写也没有关系
### --- 在HA-server4下虚拟主机下通过Firefox访问http://20.20.20.13地址:显示this is server 1或者this is server 2,说明可以正常访问了。
### --- 并且内网两台服务器服务全部中断的情况下

[root@server11 ~]# service httpd stop
[root@server12 ~]# service httpd stop
//在HA-server4虚拟主机下通过Firefox访问http://20.20.20.13地址:只能显示this is server 2  //它的缓存中缓存的是最后一次访问的数据//所以它会认为是两个不同的数据文件,但在生产环境中必须是一直的。
### --- 配置squid.conf下的ACL和http_access
### --- 还原httpd服务

[root@server11 ~]# service httpd start
[root@server12 ~]# service httpd start 
### --- 配置squid的配置文件
~~~     在HA-server4下虚拟主机下通过Firefox访问http://20.20.20.13地址:
~~~     显示this is server 1或者this is server 2        //说明正常

[root@server12 ~]# service httpd start
[root@server13 ~]#  vim /etc/squid/squid.conf
acl CONNECT method CONNECT                                                  // 该行下添加如下内容
acl wannet src 20.20.20.0/24
http_access allow localhost                                                 // 该行下添加如下内容
http_access allow wannet
[root@server13 ~]# service squid reload
### --- 若是只定义了acl,没有定义http_access,默认是拒绝
~~~     在HA-server4下虚拟主机下通过Firefox访问http://20.20.20.13地址:显示连接被拒绝

[root@server13 ~]#  vim /etc/squid/squid.conf
acl wannet src 20.20.20.0/24
#http_access allow wannet
### --- 注销deny all查看效果//还是没有任何变化
 
[root@server13 ~]#  vim /etc/squid/squid.conf
# http_access deny all                                                      // 注销deny all
[root@server13 ~]# service squid reload
### --- 再次修改配置文件
 
[root@server13 ~]#  vim /etc/squid/squid.conf
http_access allow localhost                                                 // 把这一行改为如下内容
http_access deny localhost                                                  // deny localhost与之前设置的wannet区域是无关的,当deny all 被关闭以后,
                                                                            // 我们如果定义了scl,并且这个scl没有定义http_access的情况下;它会根据最后一条的http_access反向进行操作。也就说明;此处的deny是allow,若是allow的话就是deny。
~~~     在HA-server4下虚拟主机下通过Firefox访问http://20.20.20.13地址:
~~~     显示this is server 1或者this is server 2                            // 说明正常
~~~     该案例不推荐配置,最为后期排查故障的一种方案。
五、ACL设置配置说明
### --- ACL配置格式

~~~     ACL(Access Control List,访问控制列表)
~~~     根据源地址、目标URL、文件类型等定义列表
~~~     acl  列表名称   列表类型    列表内容......
~~~     针对已定义的acl列表进行限制
~~~     http_access allow或deny  列表名称......
~~~     如果出现ACL,但是没有出现http_access,默认相当于没写,
~~~     正确定义ACL和http_access,那就是对应的获取设置权限。
### --- 支持类型:
~~~     src                         源地址
~~~     dst                         目标地址:写的是一个绝对路径,匹配的是绝对路径,必须一致
~~~     port                        目标端口
~~~     dstdomain                   目标域
~~~     time                        访问时间
~~~     maxconn                     最大并发连接:超过就会被拒绝。
~~~     url_regex                   目标URL地址:匹配的是一个包含路径,匹配的是一个范围类型。
~~~     Urlpath_regex               整个目标URL路径:URL类型

 
 
 
 
 
 
 
 
 

Walter Savage Landor:strove with none,for none was worth my strife.Nature I loved and, next to Nature, Art:I warm'd both hands before the fire of life.It sinks, and I am ready to depart
                                                                                                                                                   ——W.S.Landor

 

 

posted on   yanqi_vip  阅读(37)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

导航

统计

点击右上角即可分享
微信分享提示