|NO.Z.00019|——————————|^^ 構建 ^^|——|squid快取/代理服務.V4|-------------------------------|4臺server|
阿新 • • 發佈:2022-03-22
[Applications:squid快取/代理服務.V4] [Applications.LBC&HAC$HPC] [叢集|squid快取/代理服務|squid傳統模式|squid透明模式|squid反向代理模式|ACL|HTTP_ACCESS|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型別
===============================END===============================
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
來自為知筆記(Wiz)