squid代理服務器介紹與配置(理論+實踐)二

ACL訪問控制

ACL訪問控制方式

  • 根據源地址、目標URL、文件類型等定義列表
    • acl 列表名稱 列表類型 列表內容 ...
  • 針對已定義的acl列表進行限制
    • http_access allow或deny 列表名稱 ...

ACL規則優先級

  • 當一個用戶訪問代理服務器時, Squid會順序匹配Squid中定義的全部規則列表,一旦匹配成功,當即中止匹配。當全部規則都不匹配時,Squid會使用與最後一條相反的規則

經常使用的ACL列表類型

  • src → 源地址
  • dst → 目標地址
  • port → 目標地址
  • dstdomain → 目標域
  • time → 訪問時間
  • maxconn → 最大併發鏈接
  • url_regex → 目標URL地址
  • Urlpath_regex → 整個目標URL路徑

實驗搭建

實驗環境

squid服務器IP地址:ens33:192.168.80.184
                  ens36:192.168.10.1 (僅主機模式)
web服務器IP地址:192.168.80.151
client客戶端IP地址:192.168.10.10  (僅主機模式)

在squid服務器上修改配置文件

root@squid ~]# vim /etc/squid.conf      //修改配置文件
# should be allowed
acl hostlocal src 192.168.10.10/32        //控制hostlocal10.10的主機
# Deny requests to certain unsafe ports
http_access deny hostlocal                //拒絕訪問
[root@squid ~]# service squid reload       //重啓squid服務

在測試機上訪問web網頁

squid代理服務器介紹與配置(理論+實踐)二

sarg日誌配置

在squid服務器上安裝sarg

[root@squid ~]# mount.cifs //192.168.80.2/LNMP-C7 /mnt/      //掛載
Password for root@//192.168.80.2/LNMP-C7:  
[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]# yum install gd gd-devel -y       //安裝gd庫
[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       //編譯安裝

修改sarg配置文件

[root@squid sarg-2.3.7]# vim /etc/sarg/sarg.conf      //修改sarg配置文件
access_log /usr/local/squid/var/logs/access.log    //開啓指定訪問日誌文件
title "Squid User Access Reports"                  //網頁標題
output_dir /var/www/html/squid-reports             //報告輸出目錄
user_ip no                                        //使用用戶名顯示
exclude_hosts /usr/local/sarg/noreport            //不計入排序的站點列表文件
topuser_sort_field connect reverse               //top排序中有鏈接次數,訪問字節,降序排列,升序是normal
overwrite_report no                        //同名日誌是否覆蓋
mail_utility mailq.postfix                //發送郵件報告命令
charset UTF-8                             //使用字符集
weekdays 0-6                              //top排行的時間週期
hours 0-23                               //top排行的時間週期
www_document_root /var/www/html           //網頁根目錄
[root@squid ~]# sarg                       //生成報告
SARG: 紀錄在文件: 91, reading: 100.00%
SARG: 成功的生成報告在 /var/www/html/squid-reports/2019Dec11-2019Dec12
[root@squid sarg-2.3.7]# cd /var/www/html/squid-reports/        //切換到html目錄下
[root@squid squid-reports]# ls
2019Dec11-2019Dec12   images  index.html
[root@squid squid-reports]# yum install httpd -y               //安裝httpd服務
[root@squid squid-reports]# systemctl start httpd.service     //開啓服務
[root@squid squid-reports]# systemctl stop firewalld.service      //關閉防火牆
[root@squid squid-reports]# setenforce 0                       //關閉selinux

在client客戶機訪問網頁查看訪問記錄

squid代理服務器介紹與配置(理論+實踐)二

在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)

再次在client客戶機訪問網頁查看訪問記錄

squid代理服務器介紹與配置(理論+實踐)二

squid反向代理配置

實驗環境

squid服務器IP地址:ens33:192.168.80.184
                  ens36:192.168.10.1 (僅主機模式)
web1服務器IP地址:192.168.80.151
web2服務器IP地址: 192.168.80.185
client客戶端IP地址:192.168.10.10  (僅主機模式)

在web1服務器上編輯網頁內容

root@web ~]# cd /var/www/html/
[root@web html]# echo "<h1>this is test web!</h1>" > index.html

client客戶機上訪問網頁

squid代理服務器介紹與配置(理論+實踐)二

在web2服務器上編輯網頁內容

[root@web2 ~]# systemctl stop firewalld.service       //關閉防火牆
[root@web2 ~]# setenforce 0
[root@web2 ~]# yum install httpd -y                  //安裝httpd服務
[root@web2 ~]# cd /var/www/html/                     //建立網頁內容
[root@web2 html]# echo "<h1>this is test2 web!</h1>" > index.html 
[root@web2 html]# systemctl start httpd.service

在squid服務器上配置反向代理

[root@localhost squid]# vim /etc/squid.conf
# Squid normally listens to port 3128
http_port 192.168.80.184:80 accel vhost vport        //監控本機80端口
cache_peer 192.168.80.151 parent 80 0 no-query originserver round-robin max_conn=30 weight=1 name=web1        //節點服務器1最大訪問30,權重1,別名web1
cache_peer 192.168.80.185 parent 80 0 no-query originserver round-robin max_conn=30 weight=1 name=web1
cache_peer_domain web1 web2 www.yun.com     //訪問yun.com匹配web1,web2節點
[root@localhost squid]# service squid restart      //重啓squid服務

在client客戶機的admin用戶下配置解析域名地址,並設置代理

squid代理服務器介紹與配置(理論+實踐)二
squid代理服務器介紹與配置(理論+實踐)二
squid代理服務器介紹與配置(理論+實踐)二squid代理服務器介紹與配置(理論+實踐)二

相關文章
相關標籤/搜索