歡迎您光臨本站 註冊首頁

[操作實例] ACL控制cache_peer做反向WEB加速時虛擬主機對應到不同後端伺服器

[操作實例] ACL控制cache_peer做反向WEB加速時虛擬主機對應到不同後端伺服器

  首先,我們看看squid.conf.default之中的以下基礎配置:
  第一處:通過ACL之中有關於dstdomain的規則,也就是檢查請求網址的域名

#       acl aclname dstdomain   .foo.com ...    # Destination server from URL

  第二處:cache_peer_access用來設置對應的cache_peer的訪問許可權

#  TAG: cache_peer_access
#       Similar to 'cache_peer_domain' but provides more flexibility by
#       using ACL elements.
#        
#       cache_peer_access cache-host allow|deny [!]aclname ...
#         
#       The syntax is identical to 'http_access' and the other lists of
#       ACL elements.  See the comments for 'http_access' below, or
#       the Squid FAQ (http://www.squid-cache.org/FAQ/FAQ-10.html).      

  第三處:cache_peer_domain設置對應的cache_peer接受的域名

#  TAG: cache_peer_domain
#       Use to limit the domains for which a neighbor cache will be
#       queried.  Usage:
#  
#       cache_peer_domain cache-host domain
#       cache_peer_domain cache-host !domain
#   
#       For example, specifying
#               
#               cache_peer_domain parent.foo.net        .edu
#        
#       has the effect such that UDP query packets are sent to      
#       'bigserver' only when the requested object exists on a
#       server in the .edu domain.  Prefixing the domain name     
#       with '!' means the cache will be queried for objects   
#       NOT in that domain.
#        
#       NOTE:   * Any number of domains may be given for a cache-host,
#                 either on the same or separate lines.
#               * When multiple domains are given for a particular
#                 cache-host, the first matched domain is applied.
#               * Cache hosts with no domain restrictions are queried
#                 for all requests.
#               * There are no defaults.
#               * There is also a 'cache_peer_access' tag in the ACL
#                 section.
#


  下面,我們就測試幾個例子,看看結果如何。
  測試之前,需要做一點點的設置:

  把www.baidu.com和www.sohu.com域名都指向測試squid伺服器的ip。
  *nix設置/etc/hosts,Windows設置系統目錄/system32/drivers/etc/hosts
  我的測試伺服器ip是192.168.1.103,squid埠1080


  squid基本配置如下:

http_port 192.168.1.103:1080 vhost vport defaultsite=www.test.com
acl baidu dstdomain www.baidu.com
acl sohu  dstdomain www.sohu.com
cache_peer www.baidu.com parent 80 0 no-query originserver no-digest
cache_peer www.sohu.com parent 80 0 no-query originserver no-digest


  第一次測試規則配置:

cache_peer_access www.baidu.com deny !baidu
cache_peer_access www.sohu.com deny !sohu

  測試網址:http://www.baidu.com:1080 http://www.sohu.com:1080
  測試結果:兩者均可正確訪問

  第二次測試規則配置:

cache_peer_domain www.baidu.com .baidu.com
cache_peer_domain www.sohu.com .sohu.com

  測試網址:http://www.baidu.com:1080 http://www.sohu.com:1080
  測試結果:兩者均可正確訪問

  第三次測試規則配置:

cache_peer_access www.baidu.com deny baidu
cache_peer_access www.sohu.com deny sohu

  測試網址:http://www.baidu.com:1080 http://www.sohu.com:1080
  測試結果:訪問baidu的是sohu的squid提示頁面,不允許盜用;而訪問sohu實際上被我們反向到了baidu的伺服器,baidu的可能沒有使用squid,所以可以直接訪問。

  第四次測試規則配置:

cache_peer_access www.baidu.com deny baidu
cache_peer_access www.sohu.com deny sohu

  測試網址:http://www.baidu.com:1080 http://www.sohu.com:1080
  測試結果:與第三次測試。

  總結,使用cache_peer_access和cache_peer_domain,都可以用來設置cache_peer的存取許可權,使得不同的虛擬主機對應不同的後端伺服器。

  注意:
  1. 測試過程之中,配置正確且適合需要時,可能網頁上部分圖片不可顯示,因為試用了絕對網址,而我們的測試埠為1080。所以域名指向我們的伺服器的ip,但是80埠沒有監聽。如果你把測試埠修改為80,則不會出現這樣子的事情。
  2. 以上只是貼出了測是部分的配置,其他配置請自行設置。
《解決方案》

cache_peer_domain www.baidu.com .baidu.com
cache_peer_domain www.sohu.com .sohu.com

個人感覺這個方式比較好用。可以少建一條acl。可以偷懶:em02::m01:,配置看起來也清晰

[ 本帖最後由 zxxz79 於 2007-1-15 17:01 編輯 ]
《解決方案》

原帖由 zxxz79 於 2007-1-15 16:59 發表
cache_peer_domain www.baidu.com .baidu.com
cache_peer_domain www.sohu.com .sohu.com

個人感覺這個方式比較好用。可以少建一條acl。可以偷懶:em02::m01:,配置看起來也清晰

  也可以,不過acl相對來說更加強大一些。
《解決方案》

謝謝,正需要這樣的知識,收下慢慢研究了~
《解決方案》

真是太好了,謝謝樓主!
如果在hosts文件里一個域名指向多個伺服器IP,squid能自動做負載均衡就好了。
《解決方案》

原帖由 runco.luo 於 2007-1-18 12:00 發表
真是太好了,謝謝樓主!
如果在hosts文件里一個域名指向多個伺服器IP,squid能自動做負載均衡就好了。

:em06::em06::em06:

這個想法太牛了。

但是不現實。

但是你可以這麼做:

ip1 s1.test.com
ip2 s2.test.com
ip3 s3.test.com
ip4 s4.test.com

然後squid用這些。
《解決方案》

斑竹,您配置里的
cache_peer www.baidu.com parent 80 0 no-query originserver no-digest
可以改成
cache_peer 202.108.22.43 parent 80 0 no-query originserver no-digest
嗎?
《解決方案》

你PING百度出來的IP ,全部都可以使用IP訪問百度的網站
《解決方案》

CODE:
  把www.baidu.com和www.sohu.com域名都指向測試squid伺服器的ip。
  *nix設置/etc/hosts,Windows設置系統目錄/system32/drivers/etc/hosts
  我的測試伺服器ip是192.168.1.103,squid埠1080


斑竹,這裡的hosts文件應該如何修改?
我想windows的hosts應改成
192.168.1.103     www.baidu.com
192.168.1.103     www.sohu.com

linux的hosts不用這樣改吧?

請賜教!謝謝
《解決方案》

使用hosts不能負載均衡。換DNS可以。

[火星人 ] [操作實例] ACL控制cache_peer做反向WEB加速時虛擬主機對應到不同後端伺服器已經有550次圍觀

http://coctec.com/docs/service/show-post-34574.html