目錄
-
- 一、緩存代理概述
-
-
- 1.1 緩存代理的概述-1
- 1.2 緩存代理的概述-2
-
- 二、實驗操作
一、緩存代理概述
1.1 緩存代理的概述-1
■ Web代理的工作機制
- 緩存網頁對象,減少重複請求
理論+實驗:Squid代理伺服器應用(傳統代理、透明代理、squid的ACL使用、squid的日志分析工具sarg、squid的反向代理) -
客戶機通路過程:
用戶端要設定一個代理IP,代理IP指向代理伺服器。客戶通路163網站代理ip會提前通路連接配接代理伺服器,代理伺服器會檢查有沒有緩存,有的話就直接給用戶端,沒有的話代理伺服器會去163拿資源在給客戶機,不會讓客戶機自己去拿資源(隐藏用戶端)。
1.2 緩存代理的概述-2
■ 代理的基本類型
- 傳統代理:适用于Internet,需明确指定服務端
- 透明代理:客戶機不需指定代理伺服器的位址和端口,而是通過預設路由、防火牆政策将Web通路重定向給代理伺服器處理
■ 使用代理的好處
- 提高Web通路速度
- 隐藏客戶機的真實IP位址
二、實驗操作
- 案例環境
主機: ip位址:
squid 20.0.0.20
web1 20.0.0.21
web2 20.0.0.22
案例要求:用戶端想通路web伺服器,通路的是squid代理伺服器,不直接通路web伺服器
一、安裝squid服務
--------------------------------------------安裝squid服務--------------------------------------------------------------------------------------------------------------------------------------------------
squid伺服器:
[[email protected] ~]# hostnamectl set-hostname squid #設定主機名squid
[[email protected] ~]# su ###重新整理
web伺服器:
[[email protected] ~]# hostnamectl set-hostname web #設定主機名web
[[email protected] ~]# su
squid伺服器配置:
将軟體包上傳到伺服器中
[[email protected] ~]# tar zxvf squid-3.4.6.tar.gz -C /opt/ ###将包解壓到/opt目錄下
[[email protected] ~]# cd /opt/squid-3.4.6/
[[email protected] squid-3.4.6]# yum -y install gcc gcc-c++ ###安裝編譯工具gcc、gcc-c++
[[email protected] 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
--------------------------------------------上面配置的解釋---------------------------------------------------------
./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" \ ###支援err語言(也就是報錯是簡體中文形式)
--enable-underscore \ ###url支援下劃線
--enable-poll \ ###字元裝置驅動函數(核心函數)
--enable-gnuregex \ ###支援正規表達式
----------------------------------------------------------------------------------------------------------
[[email protected] squid-3.4.6]# make -j4 && make install ###編譯并安裝
[[email protected] squid-3.4.6]# ln -s /usr/local/squid/sbin/* /usr/local/sbin/ ###讓可用指令讓系統所能識别
[[email protected] squid-3.4.6]# useradd -M -s /sbin/nologin squid ###建立一個程式性使用者
[[email protected] squid-3.4.6]# chown -R squid.squid /usr/local/squid/var/ ###給使用者一個權限
[[email protected] squid-3.4.6]# vim /etc/squid.conf ###改主配置檔案
#http_access deny all ###預設拒絕所有,登出掉,或者放下面,從上往下讀,不會拒絕掉
http_access allow all ###加上這條allow,允許所有終端來通路,并且可用通路其他的源端伺服器
http_port 3128
cache_effective_user squid ###在端口下面加上緩存管理使用者
cache_effective_group squid ###在端口下面加上緩存管理組
[[email protected] squid-3.4.6]# squid -k parse ###檢查配置檔案文法
[[email protected] squid-3.4.6]# squid -z ###初始化緩存目錄
[[email protected] squid-3.4.6]# squid ###啟動服務
[[email protected] squid-3.4.6]# netstat -anpt | grep 3128 ###可用檢視一下啟動了沒
tcp6 0 0 :::3128 :::* LISTEN 40026/(squid-1)
[[email protected] squid-3.4.6]# cd /etc/init.d/
[[email protected] init.d]# vim squid
#!/bin/bash
#chkconfig: 2345 90 25
PID="/usr/local/squid/var/run/squid.pid" ##定義PID檔案程序号
CONF="/etc/squid.conf" ##定義主配置檔案
CMD="/usr/local/squid/sbin/squid" ##定義啟動指令
case "$1" in
start)
netstat -ntap | grep squid &> /dev/null
if [ $? -eq 0 ]
then
echo "squid is running"
else
echo "正在啟動 squid...."
$CMD
fi
;;
stop)
$CMD -k kill &> /dev/null ##關閉squid
rm -rf $PID &> /dev/null ##删除PID檔案
;;
status)
[ -f $PID ] &> /dev/null
if [ $? -eq 0 ]
then
netstat -ntap | 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
[[email protected] init.d]# chmod +x squid ###給個執行權限
[[email protected] init.d]# chkconfig --add squid ###service添加一個清單名稱
[[email protected] init.d]# service squid stop ###關閉squid服務網
[[email protected] init.d]# netstat -anpt | grep 3128 ###檢視一下是否關閉掉了
[[email protected] init.d]# service squid start ###在啟動
正在啟動 squid....
二、傳統代理模式(正向)
------------------------------------------配置傳統代理伺服器(傳統模式),還在squid伺服器上配置---------------------------------------------------------------------------------------------------------------
[[email protected] init.d]# vim /etc/squid.conf ###配置主配置檔案,在端口下面添加這三條!
cache_mem 64 MB ###指定緩存功能所使用的記憶體空間大小
reply_body_max_size 10 MB ###允許使用者下載下傳的單個檔案最大檔案大小,以位元組為機關。設定0表示不限制
maximum_object_size 4096 KB ###允許儲存到緩存空間的最大對象大小,以KB為機關,超過大小限制的檔案将不被緩存,優化指令(大檔案儲存在使用者浏覽器緩存裡,不用來找伺服器了,不然會占用伺服器的資源)
[[email protected] init.d]# iptables -F ###清空防火牆規則
[[email protected] init.d]# iptables -t nat -F ###清空防火牆表規則
[[email protected] init.d]# setenforce 0 ###關閉核心防護(臨時的)
[[email protected] init.d]# iptables -I INPUT -p tcp --dport 3128 -j ACCEPT ###-I在開頭插入input鍊(一個鍊包含多個規則),-p定義一個tcp協定,-dport 端口,-j 允許操作
[[email protected] init.d]# service squid reload ###重載配置檔案 ```
- 配置web1端
[[email protected] ~]# systemctl stop firewalld ###關閉防火牆
[[email protected] ~]# setenforce 0 ###關閉核心防護
[[email protected] ~]# yum -y install httpd ###安裝Apache
[[email protected] ~]# systemctl start httpd ###啟動Apache服務
- 在主控端上面測試
1、打開谷歌浏覽器
2、打開設定
3、點開進階
4、點選左下角系統
5、點選頁面上的 打開您計算機的代理設定
6、手動設定代理下面設定,使用代理,位址是你的代理伺服器的ip位址,端口是3128,點選儲存
7、然後浏覽器輸入20.0.0.21,去通路web伺服器
- web1伺服器上檢視
[[email protected] httpd]# cd /var/log/httpd/ ###到httpd目錄下
[[email protected] httpd]# cat access_log ###檢視access日志
20.0.0.20 - - [30/Oct/2020:00:28:16 -0400] "GET /noindex/css/fonts/Bold/OpenSans-Bold.ttf HTTP/1.1" 404 238 "http://20.0.0.21/noindex/css/open-sans.css" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.61 Safari/537.36"
20.0.0.20 - - [30/Oct/2020:00:28:16 -0400] "GET /noindex/css/fonts/Light/OpenSans-Light.ttf HTTP/1.1" 404 240 "http://20.0.0.21/noindex/css/open-sans.css" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.61 Safari/537.36"
-------從上面的就能看出,通路web伺服器的是代理伺服器的ip,并不是真機的ip位址
三、透明模式(用戶端隻需要填一個代理網關ip就可以解析到)
------------------------------------------配置透明代理伺服器,還在squid伺服器配置---------------------------------------------------------------------------------------------------------------
在代理伺服器上添加一塊網卡,設定成主機模式
[[email protected] ~]# cd /etc/sysconfig/network-scripts/
[[email protected] network-scripts]# cp -p ifcfg-ens33 ifcfg-ens36
[[email protected] network-scripts]# vim ifcfg-ens36
NAME=ens36 ###名稱改了
DEVICE=ens36 ###名稱改了
ONBOOT=yes
IPADDR=192.168.10.3 ###ip位址改了
NETMASKE=255.255.255.0 ###子網路遮罩
UUID、DNS都不用設定,可以删掉
[[email protected] network-scripts]# systemctl restart network ###重新開機一下網卡
[[email protected] network-scripts]# vim /etc/sysctl.conf
net.ipv4.ip_forward=1 ###開啟路由轉發功能,1是開啟,0是不開啟
[[email protected] network-scripts]# sysctl -p ###加載一下
-------------------------------------web1端上也要配置一個回去的路由-----------------------------------------------------------------------------------------------------------------------
[[email protected] ~]# route add -net 192.168.10.0/24 gw 20.0.0.20
----------------------------------------squid伺服器配置---------------------------------------------------------------------------------------------------------------------------------------------------
[[email protected] network-scripts]# vim /etc/squid.conf
http_port 192.168.10.3:3128 transparent ###把3182端口的地方改成透明模式
[[email protected] network-scripts]# service squid start ###重新開機服務
[[email protected] network-scripts]# iptables -F ###清除一下防火牆規則
[[email protected] network-scripts]# iptables -t nat -F ###清除一下表緩存
[[email protected] network-scripts]# iptables -t nat -I PREROUTING -i ens36 -s 192.168.10.0/24 -p tcp --dport 80 -j REDIRECT --to 3128 ###添加一個80端口通路的規則
[[email protected] network-scripts]# iptables -t nat -I PREROUTING -i ens36 -s 192.168.10.0/24 -p tcp --dport 443 -j REDIRECT --to 3128 ###通路的是https的就要改成443端口
[[email protected] network-scripts]# iptables -I INPUT -p tcp --dport 3128 -j ACCEPT ###可以進行一個重定向轉發
------------------------------------測試-----------------------------------------------------------------------
用戶端的網關填寫剛剛配置的代理伺服器ens36網卡的ip位址,192.168.10.3
然後浏覽器輸入20.0.0.21通路apache網站
在Apache伺服器檢視
[[email protected] httpd]# cd /var/log/httpd/ ###到httpd目錄下
[[email protected] httpd]# cat access_log ###檢視access日志
20.0.0.20 - - [30/Oct/2020:03:03:55 -0400] "GET /favicon.ico HTTP/1.1" 404 209 "http://20.0.0.21/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.61 Safari/537.36"
隻要通路的ip位址是代理伺服器的就可以了!!!
四、ACL通路控制
--------------------------------------squid代理伺服器操作,在透明模式之後做--------------------------------------------------------------------------------------
[[email protected] ~]# vim /etc/squid.conf ###編輯主配置
acl hostlocal src 192.168.10.99/32 ###寫在acl第一個就行,
http_access deny hostlocal ###調用上面的ip位址和名稱拒絕通路,寫在http格式的第一個就行
[[email protected] ~]# service squid reload ###重新開機
測試:用戶端浏覽器輸入20.0.0.21就顯示通路被拒絕了!
五、sarg日志分析
--------------------------------------squid代理伺服器操作,在透明模式之後做--------------------------------------------------------------------------------------
将sarg包上傳到伺服器
[[email protected] ~]# tar zxvf sarg-2.3.7.tar.gz -C /opt/ ###解壓到opt目錄
[[email protected] ~]# cd /opt/sarg-2.3.7/
[[email protected] sarg-2.3.7]# yum -y install gd gs-devel ###gd圖像化處理工具
[[email protected] sarg-2.3.7]# mkdir /usr/local/sarg ###在 /usr/local/建立一個sarg工作目錄
[[email protected] sarg-2.3.7]# ./configure --prefix=/usr/local/sarg \ ###指定安裝路徑
--sysconfdir=/etc/sarg \ ###指定配置檔案所存在位置
--enable-extraprotection ###開啟安全防護
[[email protected] sarg-2.3.7]# make && make install ###編譯安裝
[[email protected] sarg-2.3.7]# cd /etc/sarg/
[[email protected] sarg]# vim sarg.conf ###裡面全是注釋的,需要什麼就找到修改和開啟
shift+: 輸入set nu,開啟行數顯示,這樣更快定位
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 ###實驗使用者名顯示
206行 exclude_hosts /usr/local/sarg/noreport ###要修改。不計入排序的站點清單檔案
184行 topuser_sort_field connect reverse ###要修改。top排序中有連接配接次數、通路位元組、降序排列 升序是normal
257行 overwrite_report no ###同名日志是否覆寫
289行 mail_utility mailq.postfix ###要修改。發送郵件報告指令
434行 charset UTF-8 ###要修改。使用字元集
518行 weekdays 0-6 ###top排行的星期周期
525行 hours 0-23 ###top排行的時間周期
633行 www_document_root /var/www/html ###網頁根目錄
添加不計入站點檔案,添加的域名将不被顯示在排序中
[[email protected] sarg]# touch /usr/local/sarg/noreport ###剛剛配置裡面我們開啟了不計入站點功能,現在要建立一個檔案
[[email protected] sarg]# ln -s /usr/local/sarg/bin/sarg /usr/local/bin/ ###讓系統識别sarg的指令
[[email protected] sarg]# sarg ###開啟生成
SARG: Records in file: 1796, reading: 100.00%
SARG: Successful report generated on /var/www/html/squid-reports/2020Oct30-2020Oct31
[[email protected] ]# yum -y install httpd ###安裝Apache
[[email protected] squid-reports]# systemctl start httpd ###開啟apache
測試:用戶端浏覽器輸入 http://192.168.10.3/squid-reports/index.html 就能看見sarg的日志統計頁面了
- 周期性計劃任務執行每天生成報告
-----------------------------------------周期性計劃任務執行每天生成報告----------------------------------
每敲下面一次代碼就生成一次報告,比較麻煩
[[email protected] ~]# 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)
-o:輸出/var/www/html/squid-reports/目錄當中
-d:指定時間和日期,這裡是一天的時間,但是後面加上了-$(date +%d/%m/%Y),就是前一天的
-------------------------------------想定時生成報告要用crontab -e計劃性周期任務--------------------------------------------
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)
用戶端浏覽器輸入192.168.10.3/squid-reports/index.html ###裡面就會多一天的日志檔案
六、反向代理(用戶端通路代理伺服器就能到web伺服器的網站上)
--------------------------------------------------反向代理---在web1伺服器-------------------------------------------------------------------------------------------------
[[email protected] httpd]# cd /var/www/html/
[[email protected] html]# echo "this is test1 web" > index.html ###建立一個網站,網頁要區分
測試:浏覽器輸入:20.0.0.21,就能看見 this is test1 web
-------------------------------------------------squid代理伺服器操作----------------------------------------------------------------------------------------------------------
[[email protected] html]# systemctl start firewalld ###開啟防火牆
[[email protected] html]# iptables -F ###清除一下防火牆規則
[[email protected] html]# iptables -t nat -F ###清除一下防火牆表緩存
[[email protected] html]# iptables -I INPUT -p tcp --dport 3128 -j ACCEPT ###定義一個重定向轉發
-----------------------------------------------web2伺服器操作------------------------------------------------------------------------------------------------------------
[[email protected] ~]# hostnamectl set-hostname web2 #設定主機名web2
[[email protected] ~]# su
[[email protected] ~]# systemctl stop firewalld ###關閉防火牆
[[email protected] ~]# setenforce 0 ###關閉核心防護
[[email protected] ~]# yum -y install httpd ###安裝apache
[[email protected] ~]# cd /var/www/html/ ###進入到apache網站目錄
[[email protected] html]# echo "this is test2 web" > index.html ###建立一個網站
[[email protected] html]# systemctl start httpd ###啟動apache
[[email protected] html]# route add -net 192.168.10.0/24 gw 20.0.0.20 ###配置靜态路由
-------------------------------------------------squid代理伺服器操作-----------------------------------------------------------------------------------------------------------------
[[email protected] ~]# vim /etc/squid.conf
http_port 20.0.0.20:80 accel vhost vport ###監聽自己ip位址80端口,虛拟主機、虛拟端口。有人通路的時候就執行下面的語句
cache_peer 20.0.0.21 parent 80 0 no-query originserver round-robin max_conn=30 weight=1 name=web1 ###重定向到web1伺服器80端口,禁止查詢、指定增值伺服器、輪詢機制、最大通路數量30、權重為1、别名web1
cache_peer 20.0.0.22 parent 80 0 no-query originserver round-robin max_conn=30 weight=1 name=web2 ######重定向到web2伺服器80端口,禁止查詢、指定增值伺服器、輪詢機制、最大通路數量30、權重為1、别名web2
cache_peer_domain web1 web2 www.yun.com ###如果通路www.yun.com就等于通路我兩個節點伺服器
[[email protected] ~]# service squid restart ###重新開機一下squid
測試:這個時候兩個web伺服器節點,通過浏覽器輸入20.0.0.21或20.0.0.22的網站都能通路各自的網站
----------------------------------------------------------用戶端操作配置----------------------------------------------------------------------------------------------------------------------
打開目錄:C:\Windows\System32\drivers\etc
編輯hosts檔案
20.0.0.20 www.yun.com ###添加一個映射
------------------------------------------------------------------------squid代理伺服器操作-----------------------------------------------------------------------------------------------------------------
[[email protected] ~]# systemctl stop httpd ###關閉apache
[[email protected] ~]# netstat -anpt | grep 80 ###檢視一下80端口還有沒有服務,還有的話可以kill -9 殺死
[[email protected] ~]# service squid start ###啟動squid
正在啟動 squid....
[[email protected] ~]# netstat -anpt | grep 80 ###檢視一下squid啟動了沒
tcp 0 0 20.0.0.20:80 0.0.0.0:* LISTEN 45728/(squid-1)
測試:用戶端浏覽器輸入www.yun.com就會顯示this is test1 web,在重新整理就是this is test2 web,輪詢顯示!!!