FreeBSD
Prev 设定 squid Next

安装完后,就要开始修改 squid 的设定档,以符合自己的需要。而 squid 有一个设定档,设定档放置於/home/squid/etc/squid.conf,设定档中预设将 options 都 # 起来,若是不把 # 取消,则 squid 直接以预设的设定值来执行,若是需要比较特殊的设定时,要先取消 #。以下是较为重要的 options:

    # TAG: http_port
    # http_port 3128

    squid 预设的 port 为 3128。

    # TAG: cache_peer
    #cache_peer cf.hcrc.edu.tw parent 3128 0 no-query no-netdb-exchange no-digest
    cache_peer proxy1.ntcu.net parent 3128 3130 no-query no-netdb-exchange no-digest
    cache_peer proxy2.ntcu.net parent 3128 3130 no-query no-netdb-exchange no-digest
    cache_peer proxy3.ntcu.net parent 3128 3130 no-query no-netdb-exchange no-digest default
    cache_peer proxy6.NTCU.net parent 3128 3130 no-query no-netdb-exchange no-digest

    这是连接 Parent Sroxy Server 的设定,请叁考交大网路快取服务。要随时注意其变动时的设定,因为它会作一些调整,故觉得速度变慢时,要来察看是否线路有所变动。

    因有时会发生上游无法指向,为避免服务中止,故实际在 squid.conf 中是所有的设定均加上 default 选项,

     

    # TAG: hierarchy_stoplist
    hierarchy_stoplist cgi-bin ? .php .php3 .asp .cgi

    URL中包含以上文字便由本机自行处理,问其他机器会越跑越多,因为这些有可能是动态档案。

    # TAG: no_cache
    acl QUERY urlpath_regex cgi-bin \? .php .php3 ,asp .cgi
    no_cache deny QUERY

    acl(access control list),不要 cache 此处设定的资料,因为这些有可能是动态档案。

    # TAG: cache_mem (bytes)
    cache_mem 40 MB

    配置给 squid 用的记忆体大小,约占记忆体1/3弱,过多或过少都不好。Ex:128MB的记忆体,设定35~40MB便可。通常 Proxy Server 会把记忆体几乎用之殆尽,所以不适合跟其他服务在一起,故有时候会把记忆体的二分之一拿来当作 cache_mem。注意一点:这并不是说 squid 只利用到这麽大的记忆体大小。

    # TAG: cache_swap_low (percent, 0-100)
    # TAG: cache_swap_high (percent, 0-100)
    cache_swap_low 80
    cache_swap_high 95

    cache容量高、低水标,当容量超过低水标时,cache object replacement 开始作用。

    # TAG: maximum_object_size (bytes)
    maximum_object_size 10 MB

    当单一资料超过此大小便不 cache 在硬碟中,限制单一资料的『档案的容量』。

    # TAG: ipcache_size (number of entries)
    # TAG: ipcache_low (percent)
    # TAG: ipcache_high (percent)
    ipcache_size 32768
    ipcache_low 80
    ipcache_high 95

    限制cache digests 可暂存 IP、Domain Name纪录的容量。

    # TAG: fqdncache_size (number of entries)
    fqdncache_size 32768

    完整主机名称(FQDN)纪录笔数。

    # TAG: cache_dir
    cache_dir diskd /cache 1000 48 64
    <档案格式> <名称> <容量> <子目录数量> <子子目录数量>

    指定 cache 目录的档案格式、名称、容量及子目录数量;每个 cache 目录容量,最好不要超过 3200MB;若一开始切割分割区时没有将 /cache 切割出来,在设定结束后,别忘了 mkdir /usr/home/squid/cache。

    # TAG: cache_access_log
    cache_access_log /home/squid/var/logs/access.log

    纪录 Client 端要求的动作。

    # TAG: cache_log
    cache_log /home/squid/var/logs/cache.log

    纪录cache运作情形。

    # TAG: cache_store_log
    cache_store_log none

    纪录哪些cache资料被移除,或存放多久,这个选项没有用处,可直接以none值disable。

    # TAG: cache_swap_log
    cache_swap_log /home/squid/var/logs/swap/swap.log

    swap.log 预设置於第一个 cache 目录下,将其置於 /home/squid/logs 下是为了与其他logs 统一管理;swap.log 功用在於 squid 启动时,重建cache。

    # TAG: log_fqdn on|off
    log_fqdn off

    在 access.log 中纪录完整 domain,但这也表示若 Client 端使用 IP 连线,也需做 DNS 查询,如此会延缓连线速度。

    # TAG: ftp_user
    ftp_user Squid@abc.ks.edu.tw

    填上 Domain 让对方 FTP 单位知道这是哪里来的连线,这是网际网路礼仪。

    # TAG: replacement_policy
    cache_replacement_policy heap LFUDA

    当硬碟空间不足时,哪些资料先被置换。预设是用 LRU,不过 The GDSF Policy(Greedy-Dual Size Frequency,先保留较小或较 popular 的资料)或 The LFUDA Policy(Least Frequently Used with Dynamic Aging,先保留较 popular的资料,不管资料大小),都比预设的形式好,若采用 The LFUDA Policy,建议调高 maximum_object_size,因为可提高 Byte Hit Rate。

    # TAG: memory_replacement_policy
    memory_replacement_policy heap LFUDA

    同 cache_replacement_polic y叁数。

    # TAG: dns_children
    dns_children 32

    当在 ./configure 时候若有加入 --disable-internal-dns 时,就可以配合使用。其最主要的作用在於可以产生 DNS 查询的子程序,也就是自己形成 DNS 服务。预设值是 5,但是在负荷重的 server 上至少需要 10,最多是 32。因为这也会消耗记忆体,故必须衡量机器本身的记忆体大小。若是程序太少,则会使的 Squid shutdown。

    # TAG: acl(access list)
    # 办公室 IP
    acl office src 163.16.1.0/255.255.255.0

    # 服务之 client 端
    acl ksnet src "/home/squid/etc/ksnet"

    # Proxy Server Local IP
    acl local-ip src 163.28.138.0/255.255.255.0

    # 拒绝网页
    acl porn url_regex "/home/squid/etc/porn"

    定义名称,定义许多条件供 access 做存取或阻挡,预设有蛮多 acl,可以再自行加入。

    # TAG: http_access
    http_access deny porn
    http_access allow ksnet
    http_access deny all

    与 http_access 相对应。

    # TAG: icp_access
    icp_access deny all

    因为 ICP 是 Server 之间的沟通,会消耗一些频宽与效能,为取得较多的效能,通常会把其 deny 掉。

    # TAG: miss_access
    miss_access allow manager
    miss_access allow ksnet
    miss_access deny !ksnet

    最后使用 deny all,可使本机只能为 sibling,不当 parent。

    # TAG: cache_peer_access
    acl twdn dstdomain .tw
    acl ksip dst "/home/squid/etc/ksnet"
    acl comdnao dstdom_regex .*\.[A-Oa-o][-a-z0-9]*\.com$ ^[A-Oa-o][-a-z0-9]*\.com$
    acl ksdomain dstdomain .ks.edu.tw
    acl comdn dstdomain .com
    #cache_peer_access cf.hcrc.edu.tw deny !twdn
    cache_peer_access proxy1.ntcu.net deny !comdnao
    cache_peer_access proxy2.ntcu.net deny comdnao
    cache_peer_access proxy2.ntcu.net deny !comdn
    cache_peer_access proxy3.ntcu.net deny comdn

    替 cache_peer 设定存取规则。

    acl tw-ip dst "/home/squid/etc/tw-ip"
    acl tw-edu-ip dst "/home/squid/etc/tw-edu-ip"
    always_direct allow tw-ip
    always_direct allow tw-edu-ip
    always_direct allow ksdomain
    never_direct allow !twdn

    对於国内网域的 request 直接由 Server 本身去连线,不必透过 parent。

    # TAG: cache_mgr
    cache_mgr squid

    cache管理者,当挂点时要寄信给谁,记得到 /etc/aliase 建立 squid 对应 E-Mail。

    # TAG: cache_effective_user
    # TAG: cache_effective_group
    cache_effective_user squid
    cache_effective_group squid

    指定存放 cache 资料目录的拥有者及所属群组。

    # TAG: visible_hostname
    visible_hostname abc.ks.edu.tw

    Proxy Server的 Domain Name。

    # TAG: logfile_rotate
    logfile_rotate 2

    保留几份 log 档。

    # TAG: deny_info

    当 Client 端欲连结禁止网站时所出现的画面,或未依规定分流时所出现的画面。

    # TAG: append_domain
    append_domain .ks.edu.tw

    传送 http requests 时加上系统的 IP 或 Domain Name。

    # TAG: log_icp_queries on|off
    log_icp_queries on

    纪录 ICP queries 到 access.log 档。

    # TAG: icp_hit_stale on|off
    icp_hit_stale on

    回应 ICP queries,在 cache 资料中找到,并向原网站 check 资料是否更新,若无则以 cache 中资料回应。

    # TAG: cachemgr_passwd
    cachemgr_passwd disable authenticator config shutdown
    cachemgr_passwd none info counters
    cachemgr_passwd **** all

    设定 cachemgr.cgi 密码。

    # TAG: store_avg_object_size (kbytes)
    store_avg_object_size 20 KB

    平均储存 object 大小,用以预估 cache 可掌握的 object。

    # TAG: store_objects_per_bucket
    store_objects_per_bucket 48

    object 储存表单中,每个 bucket 希望储存 object 数量,调小点可提高 buckets 和储存速率。

后续安装

设定完 /home/squid/etc/squid.conf 后,就要把 squid 启动,在启动前需要先下

$ /home/squid/sbin/squid -z

把 cache_dir 的目录结构建立,这是第一次才需要这麽做,或是有更动过 cache_dir 的设定。

注意:要注意一下 cache 的权限

再来就是要把 squid 执行,可以先下以下命令,来确定启动状况

$ /home/squid/sbin/squid -NCd1

(查看squid的叁数: $ /home/squid/sbin/squid -help)

可以按 ^C 停止,若过程讯息没有产生错误,就可以执行

$ /home/squid/bin/RunCache &

注意:RunCache 档案里的 squid 路径请修改成为 /home/squid/sbin/squid

叁考网站

http://squid.visolve.com/squid24s1/contents.htm


Prev Home Next
安装 squid UP 维护 Squid