用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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值