Squid代理----ACL訪問控制+sarg日誌分析+反向代理

前言

前篇博客介紹了Squid基礎理論+傳統代理模式與透明模式,本篇博客基於重新部署了Squid環境(傳統模式),部署流程可見:squid----基礎理論+傳統、透明代理實驗

一、ACL訪問控制

1.1 ACL訪問控制概述
  • Squid通過了強大的代理控制機制,通過合理的設置ACL並行限制,可以針對源地址、目標地址、訪問URL的路徑、訪問的時間等各種條件進行過濾
1.2 ACL訪問控制步驟
  • 在配置文件Squid.conf中,ACL訪問控制通過兩個步驟來實現

    ① 使用ACL配置項定義需要控制的條件

    ② 通過http_accesss配置像對以定義的列表“允許”或“拒絕”訪問的控制

  • 每行ACL配置可以定義一條訪問控制列表,格式如下:

    acl 列表名稱 列表類型 列表內容

    列表名稱:名稱是自定義的,相當於給ACL起了個名字

    列表類型:必須使用Squid預定義的值,對應不同類別的控制條件

    列表內容:是要控制的具體對象,不同類型的列表所對應的內容也不一樣,可以有多個值(以空格分隔,爲“或”的關係)

1.3 常用的訪問控制列表類型

在這裏插入圖片描述

二、ACL配置的兩種方式

2.1 直接在Squid的配置文件種修改
[root@squid ~]# vim /etc/squid.conf
acl localhost src 192.168.200.10/24        	#針對固定的源ip地址,此IP地址命名爲“localhost”,下方配置中會針對此主機名進行權限設置
acl MYLAN src 192.168.200.0/24              #針對某一網段
acl destionhost dst 192.168.200.1/24   		#針對具體的目標ip地址
acl MC20 maxconn 20                         #訪問的最大併發連接數量
acl BURL url_regex -i ^rtsp:// ^emule://    #正則表達式的訪問協議
acl PURL urlpath_regex -i \.mp3$ \.mp4$ \.rmvb$   #訪問的文件資源末尾
acl work time MTWHF 08:30-17:30        		#訪問時間
MTWHF:週一到週五
MTWHFAS:週一到週日

http_access deny localhost     				##調用localhost,設置拒絕訪問
#配置文件中acl的讀取時自上而下的
2.2 創建新文件,在配置文件裏聲明文件位置
[root@squid ~]# cd /etc/
[root@squid etc]# vim dest.list
192.168.200.10
192.168.200.1
[root@squid etc]# vim squid.conf
#聲明文件位置,命名爲destionhost
acl destionhost dst "/etc/squid/dest.list"

http_access deny destionhost 				#調用destionhost名,拒絕(文件內的地址)訪問

三、sarg日誌分析

3.1 sarg日誌介紹

Sarg(Squid Analysis Report Generator),是一款Squid 日誌分析工具,採用HTML 格式,詳細列出每一位用戶訪問Internet 的站點信息、時間佔用信息、排名、連接次數、訪問量等。

3.2 sarg 安裝
  • 環境基於上篇博客
[root@squid ~]# yum -y install gd gd-devel httpd

[root@squid ~]# cd /mnt
[root@squid mnt]# tar zxvf sarg-2.3.7.tar.gz -C /opt/
[root@squid mnt]# cd /opt/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 && make install
  • 修改配置文件
[root@squid sarg-2.3.7]# vim /etc/sarg/sarg.conf 
#7行 取消註釋 squid的訪問日誌位置
access_log /usr/local/squid/var/logs/access.log

#25行 取消註釋 網頁標題
title "Squid User Access Reports"

#120行 取消註釋 分析報告的存放位置
output_dir /var/www/html/squid-reports 

#178行 取消註釋不使用IP代替用戶ID
user_ip no

#184行 取消註釋,刪除BYTES 添加connec (可以統計連接)
topuser_sort_field connect reverse  

#190行 取消註釋,刪除BYTES 取消字節設定,不取消可能導致報錯
user_sort_field reverse

#206行 取消註釋 不計入排序的站點列表文件,norepot需要手動創建
exclude_hosts /usr/local/sarg/norepot

#257行 取消註釋,不進行同名覆蓋

#289行 取消註釋,配置默認使用額是mailx的形式,這裏改成mailq.postfix(郵件傳輸協議)
mail_utility mailq.postfix

#434行 取消註釋,字符集默認爲latin1 這裏改成utf-8
charset utf-8

#513行、525行 取消註釋,功能爲top排行的星期週期與時間週期)

#633行 取消註釋 (網頁根目錄)
www_document_root /var/www/html
------>wq
  • 創建站點列表文件
[root@squid sarg-2.3.7]# touch /usr/local/sarg/norepot

#創建軟鏈接 方便操作
[root@squid sarg-2.3.7]# ln -s /usr/local/sarg/bin/sarg /usr/local/bin
    
#啓動sarg
[root@squid sarg-2.3.7]# sarg
SARG: Records in file: 109, reading: 100.00%
SARG: Successful report generated on /var/www/html/squid-reports/2020Mar24-2020Mar24
3.3 驗證
  • 創建週期性計劃任務
[root@squid sarg-2.3.7]# crontab -e
*/1 * * * * /usr/local/bin/sarg
#這裏爲了凸顯實現效果,所以將時間間隔設置爲了1分鐘
  • 使用客戶端訪問頁面

在這裏插入圖片描述

  • 添加週期性計劃任務,每天生成報告
[root@squid sarg-2.3.7]# crontab -e
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)

四、Squid反向代理

  • Squid反向代理在之前的博客中有詳細介紹,可見“前言”中的鏈接,這裏只簡單介紹配置步驟
  • ① DNS解析
  • ② Squid 配置
  • ③ 端口轉發
4.1 添加web服務器、設置主頁內容
  • 之前的環境中,我們已經部署了
    • squid服務器 IP爲 192.168.226.128
    • web服務器 IP爲:192.168.226.132
    • 一臺win10作爲客戶機
    • 現在添加一臺web2服務器 IP爲:192.168.226.133
  • 在web服務器上設置主頁內容
#web服務器
[root@web ~]# cd /var/www/html/
[root@web html]# ls
dog.jpg
[root@web html]# vim index.html
<h1>this is web
<img src="dog.jpg"/>
</h1>
----》wq
#這裏重啓以下httpd服務,確保爲開啓狀態
[root@web html]# systemctl restart httpd


#web2服務器
[root@web2 ~]# yum install httpd -y
[root@web2 ~]# cd /var/www/html
[root@web2 html]# ls
cat.jpg
[root@web2 html]# vim index.html
<h1>this is web2
<img src="cat.jpg"/>
</h1>
----》wq
[root@web2 html]# systemctl stop firewalld.service 
[root@web2 html]# setenforce 0
[root@web2 html]# systemctl start httpd
  • 使用客戶機訪問測試
    在這裏插入圖片描述
    在這裏插入圖片描述
4.2 Squid服務器設置反向代理
  • 修改配置文件

#60行 監控本地地址,當客戶訪問到80端口,定義虛擬主機和虛擬端口
http_port 192.168.226.128:80 accel vhost vport
#61-62行 設置兩個重定向,定位到2臺web服務器 80端口,最大訪問數量爲30 設置權重爲1 並設置別名爲web1和web2
cache_peer 192.168.226.132 parent 80 0 no-query originserver round-robin max_conn=30 weight=1 name=web1
cache_peer 192.168.226.133 parent 80 0 no-query originserver round-robin max_conn=30 weight=1 name=web2
#63行 功能爲,如果客戶訪問www.gongzuo.com這個域名則會匹配web1、web2站點
cache_peer_domain web1 web2 www.gongzuo.com
----》wq
[root@squid html]# service squid restart
  • 關閉Squid節點的httpd服務、設置防火牆規則
#關閉squid服務器的httpd服務,因爲要避免與設置的虛擬80端口衝突
[root@squid html]# systemctl stop httpd
[root@squid html]# systemctl start firewalld.service
[root@squid html]# iptables -F
[root@squid html]# iptables -t nat -F
[root@squid html]# iptables -I INPUT -p tcp --dport 3128 -j ACCEPT
#允許3128端口通過
4.3 使用客戶端訪問測試
  • ① 設置域名解析(使用超級管理員administrator用戶登錄)

  • ② 添加Squid代理

  • ③ 測試

  • 設置域名解析

    解禁用administrator並設置密碼
    在這裏插入圖片描述

在這裏插入圖片描述

  • 添加域名解析

    位置:C盤/Windows-system32-drivers-etc-hosts

在這裏插入圖片描述

  • 添加代理
    在這裏插入圖片描述

  • 訪問www.gongzuo.com 測試

    因爲有緩存的原因,可以多刷新幾次

在這裏插入圖片描述
在這裏插入圖片描述

  • 以上,反向代理模式設置完成
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章