用squid作代理

作者: mmm

來自: www.linuxsir.org

(一)
近期,我看相當多的帖子,都提到了怎麼樣用squid作代理,大家討論的很多。但是好象沒有系統的實踐。兄弟我就自己幹起來啦!請兄弟們指教!

1.配置之我見。
a,服務,b,工具。c,配置文件。d,日誌。e,權限管理
squid按照上面的劃分:
/etc/init.d/squid
/usr/sbin/squid#當然還用其他的
/etc/squid.conf
/var/log/squid/
/etc/squid.conf
這五個部分配置好了,當然沒有問題啦:)
2。配置之循序漸進
@@@服務的用法:
/etc/init.d/squid start|restart|stop
這個是服務的標準用法,不用多說。
@@@工具的用法:常用的
/usr/sbin/squid -k reconfigure|shutdown|check|rotate
reconfigure 重讀/etc/squid.conf,用在修改了配置文件後想立即生效;
check 測試
rotate 輪換日誌文件
@@@基本配置-----目標:能夠代理上網
cp /etc/squid.conf /etc/squid.conf.self
vi /etc/squid.conf
# 注意:在默認的下面添加自己的配置就可以,建議不要直接去修改原來的行!!!
http_port 8000 #代理端口
cache_mem 32 MB #一般來說是你的內存的1/3到1/2
http_access allow all#這個使所有人可以代理上網---《基本的權限管理部分
在客戶端瀏覽器裏用上代理,應該可以了吧!恭喜
@@@進一步的配置-----目標:性能提高一點總是好的
cache_swap_low 90
cache_swap_high 95
緩衝存儲的閥值:高於95立即調整,實際上提高了磁盤性能
reference_age 3 weeks
緩衝對象存儲期限:當存儲的對象達到期限後,刪除。一般就3 weeks
maximum_object_size 2048 KB
存儲對象的大小閥值:大於她的不存儲
prefer_direct off
直接奧父代理獲取緩衝,不直接到網站獲取
@@@多重代理----目標:直接拿別人的代理來用,我又沒有什麼錯
cache_peer proxy.x.x.x sibling|parent port icp_port [options]
例子:cache_peer 61.139.106.2 parent 8000 3130 proxy-only
父代理<--------類型) 父端口 icp 通訊端口 參數
cache_peer 202.109.61.163 sibling 3128 3130 proxy-only
兄弟代理<-------類型)
這個的作用大家肯定知道。就是設置多重代理,比如,你自己的代理不能上外國的。就 找個能上外國的,加上就是了。這樣以你機子爲代理的客戶都能上外國的了。呵呵!! !還可以設置多個。速度快啊!。。
@@@日誌管理
/var/log/squid/access.log|store.log|cache.log#默認可以不改啦
分別在/etc/squid.conf
cache_access_log
cache_log
cache_store_log
日誌都是會無限增長的。squid提供輪換機制,設定基數,就可以輪換啦。比如基數爲3則access.log0 access.log1 access.log2 循環。其他一樣。
調整輪換基數:
logfile_rotate 3
實際上你可以用工具手工輪換/usr/sbin/squid -k rotate
@@@權限管理
最後就是權限管理啦。squid允許你管理很多權限。比如
什麼ip|hostname|domain可以用代理,可以使用那些port,那些網站|ip|domain直接訪問,不用代理.。。。。。。
注意一個原則:最後匹配。即只匹配最後的權限控 制
acl name type value
這個就是基本的語法。
@@@@@@@@@@@@@@@@@@@acl name src ip/network ip/network ...
比如:acl mynet src 192.168.1.0/255.255.255.0
acl myreject src 210.41.114.0/255.255.255.0 192.168.1.5/24
第一行,定義了一個網絡
第二行,定義了一個網絡,一個ip#即mask方式就是網絡,cidr方式就是單個ip
http_access allow mynet
http_access deny myreject
分配權限給定義的name
@@@@@@@@@@@@@@@@@@@@acl name dst ip/network ip/network ...
限制訪問(黃色。。。)或者其他網絡或網站主機
和上面的差不多。
@@@@@@@@@@@@@@@@@@@@acl name srcdomain|dstdomain domain1 domain2 ...
使用域名進行權限分配
如:
acl yellow dstdomian xxx168.com sicau.edu.cn
最後都要用http_access allow|deny 分配!
@@@@@@@@@@@@@@@@@@@@不通過代理訪問的目標
acl name dst|dstdomain ip|domian
alway_direct name
比如: acl sina dstdomian www.sina.com.cn
always_direct sina
這樣客戶端直接訪問sina,這個用在你自己內部地址服務器上,呵呵~!~~

(二)
通過上一節的設置,我想你已經能夠用代理上網,聊天,下軟件了這一節我們一起學習一些稍微深入的東西。
@@@@@多重代理(不知道這個說法對不對)
squid支持多重代理。
cache_peer 用法詳解

# TAG: cache_peer
# To specify other caches in a hierarchy, use the format:
#
# cache_peer hostname type http_port icp_port
#
# For example,
#
# # proxy icp
# # hostname type port port options
# # -------------------- -------- ----- ----- -----------
# cache_peer parent.foo.net parent 3128 3130 [proxy-only]
# cache_peer sib1.foo.net sibling 3128 3130 [proxy-only]
# cache_peer sib2.foo.net sibling 3128 3130 [proxy-only]
#
# type: either 'parent', 'sibling', or 'multicast'.
#
# proxy_port: The port number where the cache listens for proxy
# requests.
#
# icp_port: Used for querying neighbor caches about
# objects. To have a non-ICP neighbor
# specify '7' for the ICP port and make sure the
# neighbor machine has the UDP echo port
# enabled in its /etc/inetd.conf file.
#
# options: proxy-only
# weight=n
# ttl=n
# no-query
# default
# round-robin
# multicast-responder
# closest-only
# no-digest
# no-netdb-exchange
# no-delay
# login=userassword
# connect-timeout=nn
# digest-url=url
# allow-miss
#
# use 'proxy-only' to specify that objects fetched
# from this cache should not be saved locally.
#
# use 'weight=n' to specify a weighted parent.
# The weight must be an integer. The default weight
# is 1, larger weights are favored more.
#
# use 'ttl=n' to specify a IP multicast TTL to use
# when sending an ICP queries to this address.
# Only useful when sending to a multicast group.
# Because we don't accept ICP replies from random
# hosts, you must configure other group members as
# peers with the 'multicast-responder' option below.
#
# use 'no-query' to NOT send ICP queries to this
# neighbor.
#
# use 'default' if this is a parent cache which can
# be used as a "last-resort." You should probably
# only use 'default' in situations where you cannot
# use ICP with your parent cache(s).
#
# use 'round-robin' to define a set of parents which
# should be used in a round-robin fashion in the
# absence of any ICP queries.
#
# 'multicast-responder' indicates that the named peer
# is a member of a multicast group. ICP queries will
# not be sent directly to the peer, but ICP replies
# will be accepted from it.
#
# 'closest-only' indicates that, for ICP_OP_MISS
# replies, we'll only forward CLOSEST_PARENT_MISSes
# and never FIRST_PARENT_MISSes.
#
# use 'no-digest' to NOT request cache digests from
# this neighbor.
#
# 'no-netdb-exchange' disables requesting ICMP
# RTT database (NetDB) from the neighbor.
#
# use 'no-delay' to prevent access to this neighbor
# from influencing the delay pools.
#
# use 'login=userassword' if this is a personal/workgroup
# proxy and your parent requires proxy authentication.
#
# use 'connect-timeout=nn' to specify a peer
# specific connect timeout (also see the
# peer_connect_timeout directive)
#
# use 'digest-url=url' to tell Squid to fetch the cache
# digest (if digests are enabled) for this host from
# the specified URL rather than the Squid default
# location.
#
# use 'allow-miss' to disable Squid's use of only-if-cached
# when forwarding requests to siblings. This is primarily
# useful when icp_hit_stale is used by the sibling. To
# extensive use of this option may result in forwarding
# loops, and you should avoid having two-way peerings
# with this option. (for example to deny peer usage on
# requests from peer by denying cache_peer_access if the
# source is a peer)
#
# NOTE: non-ICP neighbors must be specified as 'parent'.
#
#Default:
# none
cache_peer 61.139.106.2 parent 8000 3130 no-query no-netdb-exchange proxy-only
cache_peer 202.109.78.22 parent 3128 3130 no-query no-netdb-exchange proxy-only
cache_peer 202.105.138.19 parent 8080 3130 no-query no-netdb-exchange proxy-only
cache_peer 61.129.70.224 parent 80 3130 no-query no-netdb-exchange proxy-only
cache_peer 61.145.230.14 parent 8080 3130 no-query no-netdb-exchange proxy-only
cache_peer 61.145.231.69 parent 80 3130 no-query no-netdb-exchange proxy-only
----------------------------------------------------------------------
我們最常用的就是parent 類型 參數多爲:no-query no-netdb-exchange proxy-only 即不需要向parent發出icp查詢,不和parent交換管理信息,直接從parent cache取數據送到客戶,而不在本地緩存~!~(提高速度)
還有一個參數不常用:round-robin,並行從個個parent取數據,如果你的parent都很近而且快,那麼可以用這個參數,我沒有用,因爲個個parent速度不一樣
反倒速度慢了。(最後不要濫用parent,因爲受管制的)
-----------------------------------------------------------------------
直接訪問某些站點
-----------------------------------------------------------------------
acl name dst value
acl name dstdomain value
always_direct allow name
比如校園服務器,離我們很近,但是用了parent代理後肯定慢,這個時候我們可以利用這個權限管理使客戶直接訪問她們,而不通過代理。
acl camp dstdomain xx.edu.cn
acl vod dst 192.168.2.5
always_direct allow camp
always_dirent allow vod
---------------------------------------------------------------------------
url過濾
------------------------------------------------------------------------
acl name url_regex url1 url2 ...
http_access deny name
這個主要是過濾不良信息。比如黃色,反動。。。。。
acl ho url_regex ^http://www.pku.edu.cn$ //這裏是正則表達式
http_access deny ho
---------------------------------------------------------------------
訪問時間限制
----------------------------------------------------------------------
這個要配合其他規則使用
acl name time day-abbrevs h1:m1-h2:m2
http_access allow name
acl outnet src 210.41.115.0/255.255.255.0
acl ok_outnet time MTWHF 00:00-24:00
http_access allow ok_outnet outnet
http_access deny ok_outnet
即:允許outnet這個域在星期一到星期五都能訪問
注意:http_access allow ok_outnet outnet
時間 域
時間是英語星期的第一個字母
------------------------------------------------------------------------
拒絕緩衝某些數據
-------------------------------------------------------------------
no_cache deny name
比如:
acl yellow url_regex ^http://xxx.168.com$
no_cache deny yellow
拒絕緩衝這個網站的數據!
------------------------------------------------------------------------
別人能使用我作爲parent》》??????
--------------------------------------------------------------------
icp_port 3130 //最好不要改
icp_access allow|deny aclName
比如不允許
acl allno src 0.0.0.0./0.0.0.0
icp_access deny allno
---------------------------------------------------------------------
注意:acl 定義的name是全局的
也就是說不能重複定義一個name
比如:
acl kkk src 210.41.112.0/24
acl kkk url_regex ^sex$
這樣就使kkk重複定義,不行的喲!
---------------------------------------------------------------------
小弟用到的差不多就這些~!~如有錯誤,請指教和交流~!~
最後寫上第三篇,squid規劃~!~

(三)
squid透明代理
-------------------------------------------------------------------------
理論上,透明代理需要nat router加squid
即:在nat router上將用戶的80端口請求重定向到squid代理端口,這就要求:所有內部機子網關爲nat router
修改/etc/squid。conf
httpd_accel_host virtual//虛擬http服務
httpd_accel_port 80//監聽端口
httpd_accel_with_proxy on//採用proxy加速http
httpd_accel_uses_host_header on//加速http交換

至於nat router的信息可以參考這裏!
http://www.linuxsir.com/bbs/showthread.php?s=&threadid=29579&highlight=nat

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章