最新消息:

squid 详解

未分类 admin 6374浏览 0评论

一、squid安装

http://www.squid-cache.org/Versions/v2/2.6/cfgman/index.html

tar zxvf squid-2.6.STABLE22.tar.gz
cd squid-2.6.STABLE22
./configure –prefix=/home/squid –enable-dlmalloc
–enable-gnuregex
–enable-async-io
–enable-storeio=aufs,diskd,ufs
–enable-snmp
–enable-cachemgr-hostname
–enable-arp-acl
–enable-large-cache-files
–disable-internal-dns
–disable-ident-lookups
–enable-truncate
–with-maxfd=655360
–enable-forw-via-db
–enable-cache-digests
–enable-referer-log
–enable-useragent-log
–enable-delay-pools
–enable-kill-parent-hack
–enable-carp
–enable-icmp
–enable-poll
–with-pthreads
–enable-ssl
make
make install

二、配置文件详解

1.验证选项
------------------------------------------
auth_param
authenticate_cache_garbage_interval
authenticate_ttl
authenticate_ip_ttl
authenticate_ip_shortcircuit_ttl

2.访问控制
------------------------------------------
external_acl_type ip_user_helper 用户IP 用户名 file
file: ip_addr[/mask] user|@group|all|NONE
192.168.1.0/24 NONE 禁止这个网段的访问
10.10.10.10 apparitor 允许这个用户访问
acl ALL src 0.0.0.0/0.0.0.0 #设置访问控制列表这个在squid里经常用。
http_access allow ALL #允许所有,ALL就是刚才acl定义的。 http_access # 顺序很重要。
http_access2 #在允许和拒绝的基础上在定义访问控制列表。
http_reply_access #判断原始服务器 HTTP响应里的 Content-Type头部,
例: acl MP3 rep_mime_type audio/mpeg #定义一个头部类型。
http_reply_access deny MP3 #拒绝audio/mpeg的头部信息。
icp_access #根据IP段的访问控制
例: acl IP src 10.10.10.10
icp_access deny IP #拒绝这个IP的访问。
htcp_access deny IP #拒绝这个IP清除缓存的内容。
htcp_clr_access deny IP #
miss_access #
ident_lookup_access #是否对客户端的TCP连接发起RFC 1413身份查询。
reply_body_max_size #限制HTTP响应主体的最大可接受的大小。如果超出范围将收到错误信息。集群时最好禁用它。

3.X-Forwarded-For
-------------------------------------------
follow_x_forwarded_for allow all #让后端的web服务器获取的是客户的IP不是cache 的。需要修该一下web的log格式。
acl_uses_indirect_client
delay_pool_uses_indirect_client
log_uses_indirect_client

4.网络选项
---------------------------------------------
http_port 80 3180 #指定squid在哪个端口监听
https_port 8080 cert=file key=file #用来处理加密的HTTP(SSL、TLS),file是openssl证书和私钥的路径。
tcp_outgoing_tos 0×00 IP #该访问列表能设置到原始服务器和邻居 cache的 TCP 连接的不同 TOS/Diffserv 值

5.SSL 选项
-------------------------------------------
ssl_unclean_shutdown on #解决某些浏览器不能正确关闭ssl连接。消除了断开的客户端的错误消息。
ssl_engine
sslproxy_client_certificate
sslproxy_clent_key
sslproxy_version
sslproxy_options
sslproxy_cipher
sslproxy_cafile
sslproxy_capath
sslproxy_flags
sslproxy_program

6.邻居间的cache访问
----------------------------------------
cache_peer #邻居间的访问
例:cache_peer hostname type http-port icp-port [options]
cache_peer 10.10.11.128 parent 80 3130 proxy-only default
cache_peer_domain
cache_peer_access #对邻居间的访问控制列表
neighbor_type_domain #改变基于原始服务器的邻居cache关系。
dead_peer_timeout #不接受来自死亡邻居的 ICP/HTCP 响应
hierarchy_stoplist ? cgi-bin #将 ? cgi-bin标记为不可层叠。
1)从不对不可层叠的请求发送 ICP/HTCP 查询,除非该请求匹配 never_direct 规则;
2)从不对不可层叠的请求发送 ICP/HTCP 查询到姐妹 cache;
3)从不对不可层叠的请求查询邻居的 cache digest。

7.内存cache选项
----------------------------------------
cache_mem 1024 MB #指定内存总cache的大小。
maximum_object_size_in_memory #单个对象在内存中最大的大小。
memory_replacement_policy heap LRU #内存的替换算法。默认是LRU
例: heap GDSF : Greedy-Dual Size Frequency
heap LFUDA: Least Frequently Used with Dynamic Aging
heap LRU : LRU policy implemented using a heap

8.硬盘cache 选项。
----------------------------------------
cache_replacement_policy #物理硬盘的替换算法
例: lru : Squid’s original list based LRU policy
heap GDSF : Greedy-Dual Size Frequency
heap LFUDA: Least Frequently Used with Dynamic Aging
heap LRU : LRU policy implemented using a heap

cache_dir aufs /cache0 4096 16 256 #物理cache设置。
store_dir_select_algorithm #设置轮询,一种替换算法。
max_open_disk_fds 0 #允许最大打开的文件数量。0无限制。
minimum_object_size #缓存最小文件的大小(default:0)
maximum_object_size #缓存最大文件的大小(4096 KB)
cache_swap_low #cache 使用率最小值
cache_swap_high #cache 使用率的最大值

9.log文件的选项。
----------------------------------------
logformat #定义squid log格式。
access_log #客户端使用代理服务器的记录文件
log_access
cache_log #CACHE的启动以及各类状态的记录文件
cache_store_log #存储缓冲对象的状态记录文件
cache_swap_state #swap.state 的
logfile_rotate 5 #设置日志替换策略的squid -k rotate 替换日志
emulate_httpd_log [off|on] #激活日志模式[原始模式|普通格式]
log_ip_on_direct [on|off] #设定squid在转发时第9段记录[IP|主机名]
mime_table $path #mime列表 mime 一种2进制的表
log_mime_hdrs [off|on] #[不记录|记录]HTTP 请求和响应的头部
useragent_log $path #记录User-Agent头部信息 [–enable-useragent-log]
referrer_log $path #
pid_filename $path #squid 进程号记录文件
debug_options ALL,1 #设定日志级别默认是1级。
log_fqdn [off|on] #设定指令[取|不取]唯一的值
client_netmask 255.255.255.255 #设定access.log里的客户IP地址显示的位数。 例:client_netmask 255.255.255.0 再看access的日志里客户IP最后一位为0
forward_log $path #[–enable-forward-log]
strip_query_terms [on|off] #[显示|不显示]?号后边的内容。
buffered_logs [off|on] #[禁止|允许]写cache.log的buffer

10、FTP 网关选项
---------------------------------------
ftp_user Squid@域名 #squid ftp认证模式 用户名认证
ftp_list_width 32 #设置ftp列表的宽度如果设得太小,将不能浏览到长 文件名
ftp_passive [on|off] #[启用|关闭]ftp被动模式
ftp_sanitycheck [on|off] #
ftp_telnet_protocol [on|off] #

11、外部支持选项
---------------------------------------
diskd_program $path/diskd-daemon #磁盘管理程序
unlinkd_program $path/libexec/unlinkd #删除文件程序[–enable-unlinkd]
pinger_program $path/libexec/pinger #ping 程序[–enable-icmp]

12、RUL从写选项
---------------------------------------
url_rewrite_program #URL重定向程序
url_rewrite_children 5 #重定向进程数
url_rewrite_concurrency 0 #定义重定向的并发数。
url_rewrite_host_header [on/off] #[开启|关闭]重定向时重写包头。()
url_rewrite_access $acl #对指定的URL改写
redirector_bypass[off|on] #设置为on时,当所有URL改写程序处于高负载,不能再提供改写时,多出的请求不再通过URL改写。直接由squid处理
location_rewrite_program #
location_rewrite_children 5 #
location_rewrite_concurrency 0 #
location_rewrite_access #

13、设置cache的选项
---------------------------------------
cache deny $acl #不缓存某些URL
refresh_pattern -i .*.gif 1440 20% 10080 #设定缓存类型时间
quick_abort_min 16 KB #断点续传的最小值
quick_abort_max 16 KB #断点续传的最大值
quick_abort_pct 95 #断点续传的百分比
read_ahead_gap 16 KB
negative_ttl 5 minutes #错误页面的缓存时间5分钟
positive_dns_ttl 6 hours #正确的DNS缓存时间6小时。
negative_dns_ttl 1 minute #错误的DNS缓存时间安1分钟
range_offset_limit 0 KB #主要是对各种流媒体和要断点续传的文件的缓存的。缺省是0,也就是说只要client发过来的http header里包含了“Range:” ,squid会把这个请求转到后端http server,最致命的是,http server返回的内容并不会进入squid的cache store。把它的值设置为-1;然后squid会把Range头去掉,而从后端http server把内容全部抓下来,放到cache store里,随后的对该cache的访问就不再转发到后端http server,可以大大提高命中率。也可以给这个参数设置一个值,会提前下载多少内容.但要注意,这个参数不要大过 maximum_object_size ,不然下载完了,maximum_object_size 这个参数不能缓存这么多,又删除这个文件.白白点用你的流量.
minimum_expiry_time 60 seconds #设置最的小新鲜值
store_avg_object_size 13 KB #平均每个存储块的大小。
store_objects_per_bucket 20 #希望每个存储块可存储多少对象

14、HTTP选项
----------------------------------------
request_header_max_size 20 KB #HTTP请求包头的大小
reply_header_max_size 20 KB #HTTP答复包头的大小
request_body_max_size 0 KB #HTTP数据请求的大小。0为没有限制。
broken_posts allwo $ACL
via [on|off] #检测HTTP Via头的主机名,防止发生死循环。–enable-http-violations
cache_vary [on|off] #使squid缓存有 Vary HTTP头 的文件。
broken_vary_encoding #
collapsed_forwarding [off|on] #[不允许|允许]多个请求相同的URL处理一个请求。如果是静态内容可以加快速度。如果是动态内容会影响速度。
refresh_stale_hit 0 seconds #
ie_refresh [off|on] #解决IE 5.5以前的一个刷新BUG。
vary_ignore_expire[off|on] #
extension_methods $ACL #squid除了了解HTTP 方法:GET, POST, PUT, HEAD, CONNECT, TRACE,OPTIONS 和 DELETE。还了解其他方法。需要在这里声明。
request_entities [off|on] #[禁止|允许]非HTTP头的请求。防止攻击。
header_access $ACl #squid 从它转发的请求里删除某些 HTTP 头部[–enable-http-violations]
例: header_access header allow all 允许转发所有的HTTP头。
header_replace $ACL #该指令允许你替换,而不是删除,HTTP 头部的内容。[–enable-http-violations]
relaxed_header_parser [on|off] #不严格分析http头

15、超时选项
----------------------------------------
forward_timeout 4 minutes #
connect_timeout 1 minute #与邻居建立TCP连接超时的时间。
peer_connect_timeout 30 seconds #连接到上层代理的超时时间
read_timeout 15 minutes #用于和服务器连接的超时时间。
request_timeout 5 minutes #等待HTTP建立的时间
persistent_request_timeout 2 minutes #这个是KeepAlive连接超时时间
client_lifetime 1 day #对单个 HTTP 请求的存活期设置一个上限。
half_closed_clients [on|off] #使squid在当read(2)不再返回数据时[关闭|不关闭]客户端的连接。
pconn_timeout 1 minute #设置闲置连接的超时时间。
ident_timeout 10 seconds #设置squid等待用户认证请求的时间。缺省值为10秒。[–enable-ident-lookups]
shutdown_lifetime 30 seconds #设置squid 接受关闭信号后生存的时间。
16、管理参数
----------------------------------------
cache_mgr gjw_apparitor@hotmail.com #设置遇到问题发送的邮箱地址
mail_from
mail_program mail #指定squid用什么发送邮件
cache_effective_user nobody #运行squid的用户
cache_effective_group #运行squid的组
httpd_suppress_version_string [off|on] #[显示|不显示]有squid版本信息的错误页面
visible_hostname mysquid #指定squid的主机名多用于集群
unique_hostname #如果你有一个代理服务器阵列,并且你为每个代理服务器指定了同样的“visible_hostname”,同时你必须为它们指定不同的“unique_hostname”来避免“forwarding loops ”(传输循环)发生。
hostname_aliases #指定一个主机别名用于DNS查询
umask 027 # squdi默认的权限umask

17、声明其他服务 OPTIONS FOR THE CACHE REGISTRATION SERVICE
----------------------------------------
announce_period 0 #声明多常时间发送邮件0是禁用。
announce_host $name #声明主机明
announce_file #通告的信息
announce_port #声明端口

18、 http加速选项 HTTPD-ACCELERATOR OPTIONS
------------------------------------------------
httpd_accel_no_pmtu_disc off #

19、限速参数 DELAY POOL PARAMETERS
------------------------------------------------
delay_pools 0 #表示有多少个限制池 [–enable-delay-pools]
delay_class 1 #声明池的类型[–enable-delay-pools]
delay_access #定义池规则[–enable-delay-pools]
delay_parameters #[–enable-delay-pools]
delay_initial_bucket_level 50 #[–enable-delay-pools]

20、WCCPv1和WCCPv2 配置选项 WCCPv1 AND WCCPv2 CONFIGURATION OPTIONS
------------------------------------------------
wccp_router 0.0.0.0 #路由器不发送任何会话到 squid,直到 squid 宣称它自己是路由器[–enable-wccp]
wccp2_router #[–enable-wccpv2]
wccp_version 4 #[–enable-wccp]
wccp2_rebuild_wait on #[–enable-wccpv2]
wccp2_forwarding_method 1 #[–enable-wccpv2]
wccp2_return_method 1 #[–enable-wccpv2]
wccp2_assignment_method 1 #[–enable-wccpv2]
wccp2_service #[–enable-wccpv2]
wccp2_service_info #[–enable-wccpv2]
wccp2_weight 10000 #[–enable-wccpv2]
wccp_address 0.0.0.0 #[–enable-wccp]
wccp2_address 0.0.0.0 #[–enable-wccpv2]

21、长久的连接处理 PERSISTENT CONNECTION HANDLING
----------------------------------------
client_persistent_connections [on|off] #对客户端长连接KeepAlive.这个参数的支持,默认是打开的.现在的客户端浏览器基本都支持的.
server_persistent_connections [on|off] #[允许 |禁止]服务器持久连接
persistent_connection_after_error [off|on] #
detect_broken_pconn [off|on] #
22、CACHE DIGEST OPTIONS
------------------------------------------------
digest_generation [on|off] #控制 squid 是否产生自己的 cache 摘要。假如你的 cache 是一个子 cache,而不是其他任何 cache 的父或姐妹 cache,那么你也许想禁止产生摘要。[–enable-cache-digests]
digest_bits_per_entry 5 #决定摘要的大小。默认值是 5。增加该值导致更大的摘要(浪费更多内存和带宽)和更低的假命中可能性。[–enable-cache-digests]
digest_rebuild_period 1 hour #控制 squid 重建摘要数据结构(并非 HTTP 响应)的频率。默认是每个小时 1 次。[–enable-cache-digests]
digest_rewrite_period 1 hour # 控制 squid 从摘要数据结构创建 HTTP 响应的频率。大部分情形下,这个频率应该匹配 digest_rebuild_period 值。[–enable-cache-digests]
digest_swapout_chunk_size 4096 bytes #添加多大数量的摘要数据结构到 cache 条目里[–enable-cache-digests]
digest_rebuild_chunk_percentage 10 #指令控制每次调用重建函数时,多少 cache 增加到摘要里.默认是 10%。[–enable-cache-digests]
23、 SNMP 选项 SNMP OPTIONS
----------------------------------------
snmp_port 3401 #snmp的端口 0是关闭[–enable-snmp]
snmp_access allow $ACL #[–enable-snmp]
snmp_incoming_address 0.0.0.0 #[–enable-snmp]
snmp_outgoing_address 255.255.255.255 #[–enable-snmp]

24、ICP配置
----------------------------------------
icp_port 3130 #ICP通讯端口,squid 自动成为 ICP 服务器,它在你指定的端口侦听ICP 消息。
htcp_port 4827 #HTCP端口,squid 默认变成HTCP 服务器。[–enable-htcp]
例:cache_peer neighbor.host.name parent 3128 4827 htcp
log_icp_queries [on|off] #[打开|关闭]access.log日志记录
udp_incoming_address 0.0.0.0 #squid将所有的UDP绑定到指定的网络接口,ip-address必须对应于系统的网络接口。它将影响DNS(当使用内部DNS时)、ICP、HTCP、SNMP。如果系统只有一个IP地址,该选项不能工作。如果设置udp_outgoing_address为其他的网络接口地址,squid将在那个网络接口上接收UDP数据报。
udp_outgoing_address 255.255.255.255 #
icp_hit_stale off #如果是on忽略父cache的陈旧的但没过期的文件返回ICP_HIT 否则返回ICP_MISS
minimum_direct_hops 4 #squid 直接连接到低于一定数量路由跳计数 , 匹配该标准的请求在access.log里以CLOSEST_DIRECT 形式记载。
minimum_direct_rtt 400 #squid 直接连接到低于一定毫秒的原始服务器。匹配该标准的请求在access.log里以CLOSEST_DIRECT形式记载。
netdb_low 900 #netdb_low 和 netdb_high 指令控制度量数据库的大小。当存储子网的数量抵达 netdb_high 时,squid 删除最少近来使用的条目,直到数量低netdb_low
netdb_high 1000
netdb_ping_period 5 minutes # 默认的,squid 在发送另一个 ping 到同一主机,或同一 24 位子网中主机时,会至少等待 5 分钟。可以使用 netdb_ping_period 指令来调整这个时间间隙。
query_icmp off #测试一个或多个邻居 cache。
test_reachability off #激活了 test_reachability指令,在原始服务器不响应 ICMP ping 时,squid 返回 ICP_MISS_NOFETCH,而不是ICP_MISS。这点有助于减少假 HTTP 请求的数量,并增加终端用户迅速接受数据的机会。然而, 一定比率的原始服务器站点过滤掉了 ICMP 传输。 对这些站点,即使 HTTP 连接成功,
squid 也会返回 ICP_MISS_NOFETCH。
icp_query_timeout 0 #设置ICP超时时间。
maximum_icp_query_timeout 2000 #设置ICP超时的上限
minimum_icp_query_timeout 5 #设置ICP超时的最小值

多点通信ICP选项 MULTICAST ICP OPTIONS
----------------------------------------
mcast_groups #设置广播地址
mcast_miss_addr 255.255.255.255 # [–enable-multicast-miss]
mcast_miss_ttl 16 #[–enable-multicast-miss]
mcast_miss_port 3135 #[–enable-multicast-miss]
mcast_miss_encode_key #设定网络组的key[–enable-multicast-miss]
mcast_icp_query_timeout 2000 #

内部图标选项 INTERNAL ICON OPTIONS
----------------------------------------
icon_directory /usr/local/squid/share/icons #指明向用户传送错误信息时所用到的图标文件的目录
global_internal_static on #
short_icon_urls off #

错误页面选项ERROR PAGE OPTIONS
------------------------------------------------
error_directory /usr/local/squid/share/errors/English #设定错误页面的语言
error_map $URL 404 #指定返回的错误信息
err_html_text #
deny_info #你可以定制自定义的拒绝访问信息文件,并且可以和不同的用户列表相关联。当用户被http_access相关规则拒绝时,squid可以向用户显示你自定义的相应的拒绝访问信息文件。
例: deny_info http://www.baidu.com/logs.gif tianya

转发请求选项 OPTIONS INFLUENCING REQUEST FORWARDING
----------------------------------------
nonhierarchical_direct on #该指令控制 squid 转发不可层叠的请求的方法。squid 默认直接发送不可层叠的请求到原始服务器。这是因为这些请求不会导致 cache 命中。
prefer_direct off #该指令控制 squid 转发层叠请求的方法。默认的,squid 首先发送这样的请求到邻居 cache,然后再到原始服务器
always_direct #该访问列表影响 squid 怎样处理与邻居 cache 转发 cache 丢失。通常 squid 试图转发 cache
never_direct # never_direct 与 always_direct 相反。匹配该列表的 cache 丢失请求必须发送到邻居 cache。
这点对在防火墙之后的代理特别有用。

高级的网络选项ADVANCED NETWORKING OPTIONS
----------------------------------------
incoming_icp_average 6 #
incoming_http_average 4 #
incoming_dns_average 4 #
min_icp_poll_cnt 8 #
min_dns_poll_cnt 8 #
min_http_poll_cnt 8 #
tcp_recv_bufsize 0 bytes #

DNS选项DNS OPTIONS
----------------------------------------
check_hostnames on #
allow_underscore on #
cache_dns_program /usr/local/squid/libexec/dnsserver #指定DNS查询程序的完整路径(包括文件的名称及所在的目录)。[–disable-internal-dns]
dns_children 5 #DNS解析的进程数[–disable-internal-dns]
dns_retransmit_interval 5 seconds #[–enable-internal-dns]
dns_timeout 2 minutes #dns解析超时时间[–enable-internal-dns]
dns_defnames off #
dns_nameservers $IP #指定DNS内部服务器
hosts_file /etc/hosts #读取hosts的位置
dns_testnames #指令来指定你自己的主机名。只要接受到响应,squid 就会认为 DNS 测试成功。
append_domain #
ignore_unknown_nameservers on #
ipcache_size 1024 #IP缓存的大小
ipcache_low 90 #IP缓存使用的最低百分比
ipcache_high 95 #IP缓存使用的最高百分比
fqdncache_size 1024 #FQDN 高速缓存大小 FQDN 全域名(FQDN,Fully Qualified Domain Name)是指主机名加上全路径,全路径中列出了序列中所有域成员。

其他选项MISCELLANEOUS
----------------------------------------
memory_pools [on|off] #关闭打开内存池
memory_pools_limit 5 MB #设置内存池的大小
forwarded_for on #squid的IP地址会在HTTP请求中转反
cachemgr_passwd #设定管理端口密码
client_db [on|off] #[打开|关闭]客户端信息统计
reload_into_ims off #设置on这个参数违反 HTTP 协议,但是对大部分网站来说是可以设置为 on 的,只要后端服务器对If-Modified-Since 头的判断正确即可。[–enable-http-violations]
maximum_single_addr_tries 1 # 同一个IP的最大连接数
retry_on_error off #
as_whois_server whois.ra.net #
offline_mode off #当 offline_mode on时,Squid 对任何 cache 响应返回 cache 命中,而不用考虑它的新鲜程度。
uri_whitespace strip #
coredump_dir #
chroot #
balance_on_multiple_ip on #
pipeline_prefetch off #
high_response_time_warning 0

转载请注明:爱开源 » squid 详解

您必须 登录 才能发表评论!