Squid訪問控制方法的實例
《Squid訪問控制:ACL元素以及訪問列表》這篇文章為大家詳細講述了ACL元素以及http_access訪問控制列表的語法以及使用過程中需要注意的問題,下面給出使用這些訪問控制方法的實例:
(1)允許網段61.0.3.188/24以及172.190.96.33/24內的所有客戶機訪問代理服務器,并且允許在文件/etc/squid/guest列出的客戶機訪問代理服務器,除此之外的客戶機將拒絕訪問本地代理服務器:
acl clients src 61.0.3.188/24 172.190.96.33/24
acl guests src “/etc/squid/guest”
acl all src 0.0.0.0/0.0.0.0
http_access allow clients
http_access allow guests
http_access deny all
其中,文件“/etc/squid/guest”中的內容為:
172.168.10.3/24
210.113.24.8/16
10.0.1.24/25
(2)允許域名為job.net、gdfq.edu.cn的兩個域訪問本地代理服務器,其他的域都將拒絕訪問本地代理服務器:
acl permitted_domain src job.net gdfq.edu.cn
acl all src 0.0.0.0/0.0.0.0
http_access allow permitted_domain
http_access deny all
(3)使用正則表達式,拒絕客戶機通過代理服務器訪問包含有諸如“sexy”等關鍵字的網站:
acl deny_url url_regex –i sexy
http_access deny deny_url
(4)拒絕客戶機通過代理服務器訪問文件中指定IP或者域名的網站,其中文件/etc/squid/ deny_ip中存放有拒絕訪問的IP地址,文件/etc/squid/deny_dns中存放有拒絕訪問的域名:
acl deny_ip dst “etc/squid/deny_ip”
acl deny_dns dst “etc/squid/deny_dns”
http_access deny deny_ip
http_access deny deny_dns
(5)允許和拒絕指定的用戶訪問指定的網站,其中,允許客戶1訪問網站http://www.sina. com.cn,而拒絕客戶2訪問網站http://www.163.com:
acl client1 src 192.168.0.118
acl client1_url url_regex ^http://www.sina.com.cn
acl client2 src 192.168.0.119
acl client2_url url_regex ^http://www.163.com
http_access allow client1 client1_url
http_access deny client2 client2_url
(6)允許所有的用戶在規定的時間內(周一至周四的8:30到20:30)訪問代理服務器,只允許特定的用戶(系統管理員,其網段為: 192.168.10.0/24)在周五下午訪問代理服務器,其他的在周五下午一點至六點一律拒絕訪問代理服務器:
acl allclient src 0.0.0.0/0.0.0.0
acl administrator 192.168.10.0/24
acl common_time time MTWH 8:30-20:30
acl manage_time time F 13:00-18:00
http_access allow allclient common_time
http_access allow administrator manage_time
http_access deny manage_time
(7)/etc/squid.conf,系統軟件包提供、推薦的最小化配置如下,用戶可以根據實際情況來進行定制
acl all src 0.0.0.0/0.0.0.0
acl manager proto cache_object
acl localhost src 192.168.10.3/255.255.255.255
acl SSL_ports port 443 563
acl Safe_ports port 80 # http
acl Safe_ports port 21 # ftp
acl Safe_ports port 443 563 # https, snews
acl Safe_ports port 70 # gopher
acl Safe_ports port 210 # wais
acl Safe_ports port 1025-65535 # unregistered ports
acl Safe_ports port 280 # http-mgmt
acl Safe_ports port 488 # gss-http
acl Safe_ports port 591 # filemaker
acl Safe_ports port 777 # multiling http
acl Safe_ports port 901 # SWAT
acl purge method PURGE
acl CONNECT method CONNECT
(...)
# Only allow cachemgr access from localhost
http_access allow manager localhost
http_access deny manager
# Only allow purge requests from localhost
http_access allow purge localhost
http_access deny purge
# Deny requests to unknown ports
http_access deny !Safe_ports
# Deny CONNECT to other than SSL ports
http_access deny CONNECT !SSL_ports
#
# INSERT YOUR OWN RULE(S)HERE TO ALLOW ACCESS FROM YOUR CLIENTS
#
http_access allow localhost
# And finally deny all other access to this proxy
http_access deny all
#Default:
# icp_access deny all
#
#Allow ICP queries from eveyone
icp_access allow all
配置帶認證的代理服務抑制非法用戶使用代理服務
默認時,Squid本身不帶任何認證程序,但是可以通過外部認證程序來實現用戶認證。一般有以下的認證程序:LDAP認證、SMB認證、基于mysql的認證、基于sock5的密碼認證和基于Radius的認證。
下面介紹常用的ncsa實現的認證,ncsa是Squid源代碼包自帶的認證程序之一,從squid 2.5開始都包含了ncsa的模塊。在Red Hat Enterprise Linux 發行套件的/usr/lib/squid目錄下可以找到ncsa_auth文件。
要使用該認證服務,首先需要創建認證用戶和密碼:
#htpasswd -c /usr/local/squid/etc/ps_file guest
如果是以后添加用戶的話就把-c的參數去掉。
然后,再更改/etc/squid/squid.conf主配置文件,添加如下:
//配置認證文件和用戶文件
auth_param basic program /usr/lib/squid/ncsa_auth /usr/local/squid/etc/ ps_file
//指定認證程序的進程數
auth_param basic children 5
//代理服務器的名稱
auth_param basic realm Squid proxy-caching web server
//認證有效時間為2小時
auth_param basic credentialsttl 2 hours
//只有認證用戶才能訪問
acl normal proxy_auth REQUIRED
http_Access allow normal
最后,重啟squid服務即可。在瀏覽器里配上這個代理,打開任意網站,如果彈出了輸入用戶名和密碼的對話框,就證明配置成功了。