部署squid服務及配置傳統代理參考博文:Centos 7安裝Squid代理服務及構建傳統代理
配置squid透明代理參考博文:Centos7安裝Squid代理服務及配置透明代理html
squid提供了強大的代理控制機制,經過合理設置ACL並進行限制,能夠針對源地址、目標地址、訪問的URL路徑、訪問的時間等各類條件進行過濾。在配置文件squid.conf中,ACL訪問控制經過兩個步驟來實現:其1、使用ACL配置項定義須要控制的條件;其2、經過http_access配置項對已定義的列表作「容許」或「拒絕」訪問的控制。node
每行acl配置能夠定義一條訪問控制列表,格式以下:linux
acl 列表名稱 列表類型 列表內容
其中,」列表名稱」是由管理員自行指定,用來識別控制條件;;「列表類型」必須使用squid預約義的值,對應不一樣類別的控制條件;「列表內容」是要控制的具體對象,不一樣類型的列表所對應的內容也不同,能夠有多個值(以空格分隔,爲「或」的關係)。vim
經過上述格式能夠發現,定義訪問控制列表時,關鍵在於選擇「列表類型」並設置具體的條件對象。Squid預約義的列表類型有不少種,經常使用的包括源地址、目標地址、訪問時間、訪問端口等,以下表:
windows
ACL定義示例:centos
[root@centos02~ ]# vim /etc/squid.conf .......................... acl localnet src 10.0.0.0/8 # RFC1918 possible internal network <!--squid默認的一些ACL--> acl localnet src 172.16.0.0/12 # RFC1918 possible internal network <!--默認存在--> acl localnet src 192.168.0.0/16 # RFC1918 possible internal network <!--默認存在--> acl localhost src 127.0.0.1/255.255.255.255 <!--源地址爲127.0.0.1--> acl mylan src 192.168.1.0/24 192.168.4.0/24 <!--客戶機網段--> acl to_localhost dst 127.0.0.0/8 <!--目標地址爲127.0.0.0/8網段--> acl mc20 maxconn 20 <!--最大併發鏈接量爲20--> acl blackurl url_regex -i ^rtsp:// ^emule:// <!--以rtsp://等開頭的URL--> acl mediafile urlpath_regex -i \.mp3$ \.mp4$ \.rmvb$ <!--以.mp3等結尾的URL路徑--> acl worktime time MTWHF 9:00-18:00 <!--時間爲週一至週五的 9:00-18:00--> ........................
當須要限制的同一類對象較多時,可使用獨立的文件來存放,在acl配置行的內容處指定對應的文件位置便可。以下:瀏覽器
[root@centos02~ ]# mkdir /etc/squid [root@centos02~ ]# cd /etc/squid [root@centos02 squid]# vim ipblock.list <!--創建目標IP地址名單--> 61.135.167.36 125.39.127.25 60.28.14.0 [root@centos02 squid]# vim dmblock.list <!--創建目標域地址名單--> .qq.com .msn.com .live.com .verycd.com [root@centos02 squid]# vim /etc/squid.conf acl ipblock dst "/etc/squid/ipblock.list" <!--調用指定文件中的列表內容--> acl dmblock dstdomain "/etc/squid/dmblock.list"
當ACL設置好後,還須要經過http_access配置項來進行控制。必須注意的是,http_access配置行必須放在對應的acl配置行以後。每行http_access配置肯定一條訪問規則,格式以下:bash
http_access allow或deny 列表名服務器
將剛纔定義的acl應用到規則中,以下:併發
[root@centos02 squid]# vim /etc/squid.conf ...................... http_access deny !Safe_ports <!--squid默認存在的訪問權限--> http_access deny mediafile <!--禁止客戶機下載mp3等文件--> http_access deny ipblock <!--禁止客戶機訪問黑名單中的IP地址--> http_access deny dmblock <!--禁止客戶機訪問黑名單中網站域--> http_access deny mc20 <!--客戶機的併發鏈接量超過20時將被阻止--> http_access allow worktime <!--容許客戶機在工做時間內上網--> reply_body_max_size 10 MB <!--容許下載的最大文件大小(10M)--> ....................... http_access deny all <!--默認禁止全部客戶機使用代理,squid默認存在的訪問權限-->
在配置訪問權限時,須要注意如下幾點 :
每條http_access規則中,能夠同時包含多個訪問控制列表名,各個列表之間以空格分隔,是「與」的關係,表示必須知足全部訪問控制列表對應的條件纔會進行限制。
須要使用取反條件時,能夠在訪問控制列表前添加「 !」符號。
執行訪問控制時,squid將按照各條規則的順序依次進行檢查,若是找到一條相匹配的規則就再也不向後搜索(這點和iptables的規則匹配相似)。所以,規則的順序很是重要。
沒有設置任何規則時,squid服務將拒絕客戶端的請求。這也就是爲何配置文件中默認存在三個內網網段的ACL規則,若想拒絕默認存在的三個網段中某個,還需將其註釋掉,再進行限制,以避免發生衝突,形成訪問規則不生效。
- 有規則但找不到相匹配的項:squid將採用與最後一條規則相反的動做,即若是最好一條規則是allow,就拒絕客戶端的請求,不然容許該請求,默認存在的最後一條規則爲「http_access deny all 」
一般狀況下,把最經常使用的控制規則放在最前面,以減小squid的負載。在訪問控制的整體策略上,建議採用「先拒絕後容許」或「先容許後拒絕」的方式。
爲了使咱們查看日誌更爲直觀一些,可使用SARG,它是一款squid日誌分析工具,採用HTML格式, 詳細列出每位用戶訪問Internet的站點信息,時間佔用信息、排名、鏈接次數、訪問量等。
[root@centos02 ~]# rz
[root@centos02 ~]# ls anaconda-ks.cfg gd-devel-2.0.35-11.el6.x86_64.rpm initial-setup-ks.cfg
[root@centos02 ~]# mount /dev/cdrom /mnt/ <!--掛載linux光盤--> mount: /dev/sr0 寫保護,將以只讀方式掛載 [root@centos02 ~]# tar zxvf /mnt/sarg-2.3.7.tar.gz -C /usr/src/ <!--解壓縮sarg軟件包--> [root@centos02 ~]# umount /mnt/ <!--卸載光盤--> [root@centos02 ~]# mount /dev/cdrom /mnt/ <!--掛載操做系統盤--> mount: /dev/sr0 寫保護,將以只讀方式掛載 [root@centos02 ~]# rm -rf /etc/yum.repos.d/CentOS-* [root@centos02 ~]# yum -y install gd gd-devel httpd <!--安裝sarg依賴軟件--> [root@centos02 ~]# rpm -ivh gd-devel-2.0.35-11.el6.x86_64.rpm <!--檢查sarg軟件所需的依賴--> [root@centos02 ~]# rpm -ivh gd-devel-2.0.35-11.el6.x86_64.rpm --nodeps <!--強制忽略依賴關係安裝gd-devel--> [root@centos02 ~]# rpm -qa | grep gd <!--檢查是否安裝成功--> [root@centos02 ~]# rpm -qa | grep gd-devel <!--檢查是否安裝成功--> [root@centos02 ~]# rpm -qa | grep httpd <!--檢查是否安裝成功--> [root@centos02 ~]# mkdir /usr/local/sarg <!--建立安裝sarg目錄--> [root@centos02 ~]# cd /usr/src/sarg-2.3.7/ <!--進入sarg目錄--> [root@centos02 sarg-2.3.7]# ./configure --prefix=/usr/local/sarg --sysconfdir=/etc/sarg --enable-extraprotection <!--配置sarg--> [root@centos02 sarg-2.3.7]# make && make install <!--安裝sarg--> [root@centos02 sarg-2.3.7]# ls -ld /etc/sarg/ <!--查看是否生成配置文件--> drwxr-xr-x 2 root root 83 11月 18 10:35 /etc/sarg/ [root@centos02 sarg-2.3.7]# ls -ld /usr/local/sarg/ <!--查看是否生成配置文件--> drwxr-xr-x 4 root root 30 11月 18 10:35 /usr/local/sarg/ [root@centos02 ~]# cp /etc/sarg/sarg.conf /etc/sarg/sarg.conf.bak <!--備份sarg主配置文件--> [root@centos02 ~]# vim /etc/sarg/sarg.conf <!--修改sarg主配置文件--> 8 access_log /usr/local/squid/var/logs/access.log <!--指定squid的訪問日誌文件--> 27 title "Squid User Access Reports" <!--網頁標題--> 122 output_dir /var/www/html/squid-reports <!--sarg報告的輸出目錄--> 182 user_ip no <!--使用用戶名顯示--> 189 topuser_sort_field BYTES reverse <!--在top排序中,指定鏈接次數、訪問次數, 採用降序排序,升序將reverse換成normal--> 196 user_sort_field BYTES reverse <!--對於用戶訪問記錄, 鏈接次數按降序排序--> 211 exclude_hosts /usr/local/sarg/noreport <!--指定不計入排序的站點列表文件--> 265 overwrite_report no <!--當那個日期報告已經存在,是否覆蓋報告--> 298 mail_utility mailx <!--發送郵件報告的命令--> 444 charset utf-8 <!--使用字符集--> 529 weekdays 0-6 <!--指定top排序時的星期週期,0爲週日--> 537 hours 0-23 <!--指定top排序時的時間週期--> 646 www_document_root /var/www/html <!--網頁根目錄--> [root@centos02 ~]# mkdir -p /usr/local/sarg/noreport <!--建立不計入排序的目錄--> [root@centos02 ~]# ln -s /usr/local/sarg/bin/sarg /usr/local/bin/ <!--優化執行命令--> [root@centos02 ~]# sarg <!--執行sarg啓動一次記錄--> SARG: 紀錄在文件: 27, reading: 100.00% SARG: 成功的生成報告在 /var/www/html/squid-reports/2019Nov18-2019Nov18 [root@centos02 ~]# systemctl start httpd <!--啓動httpd服務--> [root@centos02 ~]# systemctl enable httpd <!--設置開機自動啓動-->
http://192.168.100.20/squid-reports/
[root@centos02 ~]# vim log.sh <!--建立腳本--> #!/bin/bash TD=$(date -d '1 day ago' +%d/%M/%Y) /usr/local/sarg/bin/sarg -l /usr/local/squid/var/logs/access.log -o /var/www/html/sarg -z -d $YETD_$TD &> /dev/null exit 0 [root@centos02 ~]# chmod +x log.sh <!--腳本添加執行權限--> [root@centos02 ~]# ./log.sh & <!--執行腳本--> [1] 7798 [root@centos02 ~]# vim /etc/rc.d/rc.local <!--將腳本添加至開機自動啓動配置文件中--> /root/log.sh [root@centos02 ~]# chmod +x /etc/rc.d/rc.local <!--添加執行權限-->
—————— 本文至此結束,感謝閱讀 ——————