Squid是基于應用層的網關防火牆,它的作用是:
####通過緩存的方式為使用者提供Web通路加速
####對使用者的Web通路進行過濾控制
但是squid隻支援如:http、ftp、https等少數協定
代理分為:正向代理、反向代理、透明代理
正向代理又分為:
####普通代理:即标準的、傳統的代理服務,需要客戶機在浏覽器中指定代理伺服器的位址、端口
####透明代理:适用于企業的網關主機(共享接入Internet)中,客戶機不需要指定代理伺服器位址、端口等資訊,需要設定防火牆政策将客戶機的Web通路資料轉交給代理服務程式處理
反向代理:為Internet使用者通路企業Web站點提供緩存加速
squid軟體包名:squid-2.6.STABLE6
服務名:squid
主程式:/usr/sbin/squid
配置目錄:/etc/squid/
主配置檔案:/etc/squid/squid.conf
預設監聽端口:TCP 3128
預設通路日志檔案:/var/log/squid/access.log
squid -z (初始化緩存目錄結構)
squid -D(也可啟動squid服務,但不做dns解析測試)
常用配置項如:http_port 3128 (定義監聽端口為3128)
cache_mem 64 MB (定義緩存大小為64MB)
cache_mgr [email protected](定義伺服器管理者)
maximum_object_size 4096 KB (定義最大的緩存對象為4096KB)
reply_body_max_size 10240000 allow all(定義通路響應對象不能大于10MB)
access_log /var/log/squid/access.log squid(定義通路日志位置)
visible_hostname proxy.test.com(定義可用主機名)
cache_dir ufs /var/spool/squid 100 16 256(其中ufs 為緩存資料的存儲格式;100表示為緩存目錄配置設定的磁盤空間,并以MB為機關;16表示緩存空間的一級子目錄個數;256表示緩存空間的二級子目錄個數)
###############################################################################
Squid代理時可以做的一些限制規則這裡我們用實驗來示範,實驗網絡環境:
Squid伺服器内網卡 | 192.168.10.10 |
Squid伺服器外網卡 | 192.168.0.103 |
Squid内網中的用戶端 | 192.168.10.11 |
外網位址 | 192.168.0.254 |
Squid伺服器上:
[root@server5 ~]# yum list all |grep squid(列出squid軟體包)
[root@server5 ~]# yum install squid(安裝squid軟體包)
[root@server5 ~]# service squid start(啟動squid服務)
[root@server5 ~]# chkconfig squid on(添加squid到開機自啟動清單)
[root@server5 ~]# netstat -tunlp |grep :3128(檢視端口是否開放)
tcp 0 0 0.0.0.0:3128 0.0.0.0:* LISTEN 9750/(squid)
[root@server5 ~]# cd /etc/squid/(切換到squid配置目錄)
[root@server5 squid]# ls
[root@server5 squid]# grep -v "^#" squid.conf | cat –s(顯示squid已啟用的選項,并若有多個空白行則隻顯示一個空白行)
acl all src 0.0.0.0/0.0.0.0
acl manager proto cache_object
acl localhost src 127.0.0.1/255.255.255.255
acl to_localhost dst 127.0.0.0/8
acl SSL_ports port 443
acl Safe_ports port 80
acl Safe_ports port 21
acl Safe_ports port 70
acl Safe_ports port 443
acl Safe_ports port 210
acl Safe_ports port 1025-65535
acl Safe_ports port 280
acl Safe_ports port 488
acl Safe_ports port 591
acl Safe_ports port 777
acl CONNECT method CONNECT
http_access allow manager localhost
http_access deny manager
http_access deny !Safe_ports
http_access deny CONNECT !SSL_ports
http_access allow localhost
http_access deny all
icp_access allow all
http_port 3128
hierarchy_stoplist cgi-bin ?
access_log /var/log/squid/access.log squid
acl QUERY urlpath_regex cgi-bin \?
cache deny QUERY
refresh_pattern ^ftp: 1440 20% 10080
refresh_pattern ^gopher: 1440 0% 1440
refresh_pattern . 0 20% 4320
acl apache rep_header Server ^Apache
broken_vary_encoding allow apache
coredump_dir /var/spool/squid
[root@server5 squid]# cp squid.conf squid.conf2(為了防止出錯嘿嘿咱先儲存一份)
[root@server5 squid]# squid -k parse(分析squid系統初始化是否有錯誤)
驗證代理伺服器功能在squid伺服器上:
[root@station5squid]# ls /usr/share/squid/errors/(檢視語言類型)
[root@server5 ~]# vim /etc/squid/squid.confreply_body_max_size 10240000 allow all(通路對象隻能小于等于10M才可以被通路)
#http_access allow localhost(預設隻允許為本機代理,其它均拒絕)
#http_access deny all
http_access allow all(把http_access deny all修改為所有的都允許)
http_port 192.168.10.10:8080(指定代理網關和端口)
visible_hostname proxy.benet.com (指定代理主機名)error_directory /usr/share/squid/errors/Simplify_Chinese(修改通路出錯資訊為中文顯示)
[root@server5 ~]# service squid restart( 重新開機生效)在squid内網中的用戶端上:
[root@station19 ~]# export HTTP_PROXY=192.168.10.10:8080(指定代理網關、代理端口)
[root@station19 ~]# echo $HTTP_PROXY
192.168.10.10:8080
[root@station19 ~]# elinks 192.168.0.254:80(squid用戶端通路并以圖形界面顯示)
在squid伺服器上驗證:
[root@server5~]# tail -f /var/log/squid/access.log(
檢查代理伺服器的日志檔案應發現客戶機的 HTTP 通路記錄)
1267970936.677 78 192.168.10.11 TCP_MISS/301 702 GET http://192.168.0.254/course - DIRECT/192.168.0.254 text/html(表明代理通路已成功)
在squid用戶端上通路:
http://192.168.0.254/course/ 在浏覽器中下載下傳看到小于10M 大小的檔案時允許儲存 在浏覽器中下載下傳看到超過10M 大小的檔案時則被拒絕 基于 ACL 的通路控制代理:可以從客戶機的IP位址、請求通路的URL/域名/檔案類型、通路時間、并發請求數等各方面進行控制
應用通路控制的方式:定義acl清單
acl 清單名稱 清單類型 清單内容 …
針對acl清單進行限制
http_access allow或deny 清單名……
常用的ACL清單類型有:src、dst、port、srcdomain、dstdomain、time、maxconn、url_regex、urlpath_regex
例:禁止任何客戶機使用代理伺服器
清單定義示例 acl LAN1 src 192.168.10.0/24 acl PC1 src 192.168.10.12/32 acl Blk_Domain dstdomain .qq.com acl Work_Hours time MTWHF 08:30-17:30 acl Max20_Conn maxconn 20 acl Blk_URL url_regex -i ^rtsp:// ^mms:// acl Blk_Words urlpath_regex -i sex adult acl RealFile urlpath_regex -i \.rmvb$ \.rm$ 根據已經定義的部分ACL 清單進行通路控制 http_access deny LAN1 Blk_URL http_access deny LAN1 Blk_Words http_access deny PC1 RealFile http_access deny PC1 Max20_Conn http_access allow LAN1 Work_Hours 通路控制規則的比對順序 沒有設定任何規則時,将拒絕所有用戶端的通路請求 有規則但找不到相比對的項時将采用與最後一條規則相反的權限,即如果最後一條規則是allow ,那麼就拒絕用戶端的請求,否則允許該請求 如何實作透明代理: 前提: 客戶機的Web 通路資料要能經過防火牆 代理服務建構在網關(防火牆)主機中 配置要求: 代理服務程式能夠支援透明代理 設定防火牆規則,将客戶機的Web 通路資料自動重定向給代理服務程式處理 配置簡要步驟: 修改squid.conf 配置檔案,并重新加載該配置 http_port 192.168.10.1:8080 transparent 添加iptables 規則 iptables -t nat -I PREROUTING -i eth1 -s 192.168.10.0/24 -p tcp --dport 80 -j REDIRECT --to-ports 8080 客戶機浏覽器 不需要在浏覽器中指定代理伺服器的位址、端口 驗證透明代理的實施效果 反向代理實作步驟: 檔案,并重新加載該配置 http_port 218.29.30.31:80 vhost cache_peer 192.168.10.7 parent 80 0 originserver weight=5 max-conn=30 cache_peer 192.168.10.8 parent 80 0 originserver weight=5 max-conn=30 cache_peer 192.168.10.9 parent 80 0 originserver weight=5 max-conn=30 cache_peer 192.168.10.6 parent 80 0 originserver weight=1 max-conn=8 cache_peer Web 伺服器位址 伺服器類型 http 端口 icp 端口 [ 可選項] 驗證反向代理的實施效果 在上遊Web 伺服器(192.168.10.6 ~192.168.10.9 )中開啟httpd 服務 在Internet 中的客戶機(218.29.30.29 )中通路反向代理伺服器主機(http://218.29.30.31 ),應能夠看到實際由上遊Web 伺服器提供的網頁内容 檢視反向代理伺服器的通路日志資訊 [root@localhost ~]# tail -1 /var/log/squid/access.log 1231256531.038 35 218.29.30.29 (客戶及位址) TCP_MISS/200 2869 GET http://218.29.30.31/ (反向代理伺服器位址)/index.php? - FIRST_UP_PARENT/192.168.10.6 (第一台上有web 伺服器位址)p_w_picpath/gif 1) src 是基于來源的控制 若允許192.168.10.0/24網段可以被通路
http_access allow mynetwork acl mynetwork src 192.168.10.0/24 [root@server5 ~]# service squid restart在squid客戶機上通路http://192.168.0.254/course
在squid代理伺服器上檢視客戶機的http通路記錄日志資訊:
[root@server5 ~]# tail /var/log/squid/access.log
1267975971.422 739 192.168.10.11 TCP_MISS/404 616 GET http://192.168.0.254/favicon.ico - DIRECT/192.168.0.254 text/html
(看到192.168.10.0/24網段可以被通路)
2) dst基于目标的通路控制:
假設
不允許通路 的伺服器 [root@server5 ~]# vim /etc/squid/squid.conf acl mynetwork src 192.168.10.0/24acl myserver dst 192.168.0.254/32
http_access deny mynetwork !myserver
[root@server5 ~]# service squid restart 在squid 用戶端通路http://192.168.0.254/course, 看到通路被拒絕 伺服器上檢視日志: [root@server5 ~]# tail /var/log/squid/access.log 1267977109.278 7 192.168.10.11 TCP_DENIED/403 1520 GET http://192.168.0.254/course/ - NONE/- text/html3) url_regex針對完整的URL 做正規表達式比對的,注意必須是完整的url
若可以下載下傳包含pdf格式的文
acl notpdf url_regex -i ^http.*\.pdf(其中-i表示忽略大小寫)
http_access allow mynetwork .notpdf
在squid上測試:http://192.168.0.254/yum/books/
看到pdf格式的可以被下載下傳
4) urlpath_regex表示忽略了協定、主機、端口,則隻對它們後邊的路徑做比對的
若chm形式的檔案不可以被下載下傳
acl notchm urlpath_regex -i .chm \.chm$
http_access allow mynetwork !notchm
在squid用戶端上測試:
http://192.168.0.254/yum/books/
看到下載下傳被拒絕掉了
5)browser用來定義浏覽類型的
若我們定義不用firefox浏覽起來浏覽
acl mynetwork src 192.168.10.0/24
acl notfirefox browser MOZILLA
http_access allow mynetwork !notfirefox
在squid用戶端上用Firefox浏覽器測試結果已經浏覽不到頁面了
而打開一個終端用elinks測試則仍可以浏覽到頁面
6)time 基于時間的控制
若我們打算從早上8:00到第二天6:00這期間為上班時間,我們不允許上網,其他時間仍可上網,其實這裡隻是為了示範跨越00:00點的效果
acl worktime time 8:00-23:59 (這樣會取時間的并集) acl worktime time 00:00-5:59 http_access allow mynetwork !worktime 用戶端上,在這個時間範圍内嘗試上外網結果被拒絕掉了 7 配置透明代理(透明代理表示不用指定代理伺服器就可以上外網) [root@server5 ~]# vim /etc/squid/squid.conf http_port 192.168.10.10:8080 transparent [root@server5 ~]# iptables -t nat -A PREROUTING -i eth1 -s 192.168.10.0/24 -p tcp --dport 80 -j REDIRECT --to-ports 8080 [root@server5 ~]# service iptables save [root@server5 ~]# iptables -t nat -L -n[root@server5 ~]# service iptables restart
去掉Firefox上的代理端口照樣可以通路
[root@station19 ~]# unset HTTP_PROXY
[root@station19 ~]# echo HTTP_PROXY
HTTP_PROXY
去掉elinks代理也可以通路了
8)maxconn表示最多允許連接配接的個數
acl twoconn maxconn 2
http_access allow mynetwork !twoconn
在squid客戶機上用Firefox打開浏覽器,再用elinks也去連接配接浏覽器,檢視日志此時已經不允許再有其他連接配接了
注意:正向代理、反向代理、透明代理均不可同時
使用任意兩者。