天天看點

Squid

squid

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解析測試)

<b>常用配置項如:</b>

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系統初始化是否有錯誤)

<b>驗證代理伺服器功能</b><b></b>

在squid伺服器上:

[root@station5squid]# ls /usr/share/squid/errors/(檢視語言類型)

<b>[root@server5 ~]# vim /etc/squid/squid.conf</b>

reply_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(指定代理網關和端口)

<b>visible_hostname  proxy.benet.com</b><b>(指定代理主機名)</b>

error_directory /usr/share/squid/errors/simplify_chinese(修改通路出錯資訊為中文顯示)

<b>[root@server5 ~]# service squid restart(</b><b>重新開機生效)</b>

在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(<b>檢查代理伺服器的日志檔案應發現客戶機的</b><b>http</b><b>通路記錄</b>)

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用戶端上通路:

<b>http://192.168.0.254/course/</b>

<b>在浏覽器中下載下傳看到小于10m</b><b>大小的檔案時允許儲存</b>

<b></b>

<b>在浏覽器中下載下傳看到超過10m</b><b>大小的檔案時則被拒絕</b>

<b>基于</b><b>acl</b><b>的通路控制代理:</b><b></b>

可以從客戶機的ip位址、請求通路的url/域名/檔案類型、通路時間、并發請求數等各方面進行控制

<b>應用通路控制的方式:</b>

定義acl清單

        acl 清單名稱 清單類型 清單内容 …

針對acl清單進行限制

        http_access allow或deny 清單名……

常用的acl清單類型有:src、dst、port、srcdomain、dstdomain、time、maxconn、url_regex、urlpath_regex

例:禁止任何客戶機使用代理伺服器

<b>acl all src 0.0.0.0/0.0.0.0</b>

<b>http_access deny all</b>

<b>acl</b><b>清單定義示例</b>

<b>acl lan1 src 192.168.10.0/24</b>

<b>acl pc1 src 192.168.10.12/32</b>

<b>acl blk_domain dstdomain .qq.com</b>

<b>acl work_hours time mtwhf 08:30-17:30</b>

<b>acl max20_conn maxconn 20</b>

<b>acl blk_url url_regex -i ^rtsp:// ^mms://</b>

<b>acl blk_words urlpath_regex -i sex adult</b>

<b>acl realfile urlpath_regex -i \.rmvb$ \.rm$</b>

<b>根據已經定義的部分acl</b><b>清單進行通路控制</b>

<b>http_access deny lan1 blk_url</b>

<b>http_access deny lan1 blk_words</b>

<b>http_access deny pc1 realfile</b>

<b>http_access deny pc1 max20_conn</b>

<b>http_access allow lan1 work_hours</b>

<b>通路控制規則的比對順序</b>

<b>沒有設定任何規則時,将拒絕所有用戶端的通路請求</b>

<b>有規則但找不到相比對的項時将采用與最後一條規則相反的權限,即如果最後一條規則是allow</b><b>,那麼就拒絕用戶端的請求,否則允許該請求</b>

<b>如何實作透明代理:</b>

<b>前提:</b>

<b>客戶機的web</b><b>通路資料要能經過防火牆</b>

<b>代理服務建構在網關(防火牆)主機中</b>

<b>配置要求:</b>

<b>代理服務程式能夠支援透明代理</b>

<b>設定防火牆規則,将客戶機的web</b><b>通路資料自動重定向給代理服務程式處理</b>

<b>配置簡要步驟:</b>

<b>修改squid.conf</b><b>配置檔案,并重新加載該配置</b>

<b> http_port 192.168.10.1:8080 transparent</b>

<b>添加iptables</b><b>規則</b>

<b> iptables -t nat -i prerouting -i eth1 -s 192.168.10.0/24 -p tcp --dport 80 -j redirect --to-ports 8080</b>

<b>客戶機浏覽器</b>

<b> </b><b>不需要在浏覽器中指定代理伺服器的位址、端口</b>

<b>驗證透明代理的實施效果</b>

<b>反向代理實作步驟:</b>

<b>修改squid.conf</b><b>檔案,并重新加載該配置</b>

<b>http_port  218.29.30.31:80 vhost</b>

<b>cache_peer 192.168.10.7 parent 80 0 originserver weight=5 max-conn=30</b>

<b>cache_peer 192.168.10.8 parent 80 0 originserver weight=5 max-conn=30</b>

<b>cache_peer 192.168.10.9 parent 80 0 originserver weight=5 max-conn=30</b>

<b>cache_peer 192.168.10.6 parent 80 0 originserver weight=1 max-conn=8</b>

<b>cache_peer web</b><b>伺服器位址 </b><b>伺服器類型 http</b><b>端口 icp</b><b>端口 [</b><b>可選項]</b>

<b>驗證反向代理的實施效果</b>

<b>在上遊web</b><b>伺服器(192.168.10.6</b><b>~192.168.10.9</b><b>)中開啟httpd</b><b>服務</b>

<b>在internet</b><b>中的客戶機(218.29.30.29</b><b>)中通路反向代理伺服器主機(http://218.29.30.31</b><b>),應能夠看到實際由上遊web</b><b>伺服器提供的網頁内容</b>

<b>檢視反向代理伺服器的通路日志資訊</b>

<b>[root@localhost ~]# tail -1  /var/log/squid/access.log</b>

<b>1231256531.038     35 218.29.30.29</b><b>(客戶及位址) tcp_miss/200 2869 get http://218.29.30.31/</b><b>(反向代理伺服器位址)/index.php? - first_up_parent/192.168.10.6 </b><b>(第一台上有web</b><b>伺服器位址)p_w_picpath/gif</b>

<b>1) </b><b>src</b><b>是基于來源的控制</b>

<b>     </b><b>若允許</b>192.168.10.0/24網段可以被通路<b></b>

<b>http_access allow mynetwork</b>

<b>acl mynetwork src 192.168.10.0/24</b>

<b>[root@server5 ~]# service squid restart</b>

     在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基于目标的通路控制:

       假設<b>不允許通路</b><b>192.168.0.254</b><b>的伺服器</b>

  <b> [root@server5 ~]# vim /etc/squid/squid.conf</b>

<b>   acl mynetwork src 192.168.10.0/24</b>

   acl myserver dst 192.168.0.254/32

   http_access deny mynetwork !myserver

  <b> [root@server5 ~]# service squid restart</b>

<b>   </b><b>在squid</b><b>用戶端通路http://192.168.0.254/course,</b><b>看到通路被拒絕</b>

<b>   </b><b>在squid</b><b>伺服器上檢視日志:</b>

<b>   [root@server5 ~]# tail /var/log/squid/access.log</b>

<b>   1267977109.278      7 192.168.10.11 tcp_denied/403 1520 get http://192.168.0.254/course/ - none/- text/html</b>

3) url_regex針對完整的url 做正規表達式比對的,注意必須是完整的url

若可以下載下傳包含pdf格式的文

   <b>[root@server5 ~]# vim /etc/squid/squid.conf</b>

       acl notpdf url_regex -i ^http.*\.pdf(其中-i表示忽略大小寫)

http_access allow mynetwork .notpdf

[root@server5 ~]# service squid restart

在squid上測試:http://192.168.0.254/yum/books/

看到pdf格式的可以被下載下傳

4) urlpath_regex表示忽略了協定、主機、端口,則隻對它們後邊的路徑做比對的

       若chm形式的檔案不可以被下載下傳

       <b>[root@server5 ~]# vim /etc/squid/squid.conf</b>

acl mynetwork src 192.168.10.0/24

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點的效果

        <b>[root@server5 ~]# vim /etc/squid/squid.conf </b>

<b>        acl worktime time 8:00-23:59</b><b>(這樣會取時間的并集)</b>

<b>acl worktime time 00:00-5:59</b>

<b>http_access allow mynetwork !worktime</b>

<b>在squid</b><b>用戶端上,在這個時間範圍内嘗試上外網結果被拒絕掉了</b>

<b>7</b><b>)</b><b>配置透明代理(透明代理表示不用指定代理伺服器就可以上外網)</b>

<b>       [root@server5 ~]# vim /etc/squid/squid.conf</b>

<b>http_port 192.168.10.10:8080 transparent</b>

<b>[root@server5 ~]# iptables -t nat -a prerouting -i eth1 -s 192.168.10.0/24 -p tcp --dport 80 -j redirect --to-ports 8080</b>

<b>[root@server5 ~]# service iptables save</b>

<b>[root@server5 ~]# iptables -t nat -l -n</b>

      [root@server5 ~]# service iptables restart

去掉firefox上的代理端口照樣可以通路

[root@station19 ~]# unset http_proxy

[root@station19 ~]# echo http_proxy

http_proxy

去掉elinks代理也可以通路了

8)maxconn表示最多允許連接配接的個數

<b>       </b>acl mynetwork src 192.168.10.0/24

       acl twoconn maxconn 2

       http_access allow mynetwork !twoconn

 [root@server5 ~]# service squid restart

       在squid客戶機上用firefox打開浏覽器,再用elinks也去連接配接浏覽器,檢視日志此時已經不允許再有其他連接配接了

注意:正向代理、反向代理、透明代理均不可同時

使用任意兩者。

繼續閱讀