对Squid使用的一些总结

本文详细总结了Squid缓存服务器的配置优化策略,包括cache_dir、cache_mem、透明代理、访问控制、更新周期设置等关键点,并介绍了如何在Apache服务器中前置Squid以减少内存消耗,实现内容过滤、连接数限制等功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

对Squid使用的几点总结

1.cache_dir

在Linux环境下用aufs类型的cache_dir比较稳定,速度也比较快。cache_dir所在文件系统如果用noatime选项来mount,可以避

免访问cache文件时经常要更新access time的操作。

一般cache容量和被加速内容的比值在1:10到1:20比较合适。

cache_dir aufs /var/spool/squid 10240 64 64
这是一个典型网站web caching server的cache_dir配置,加速100GB到200GB的内容

cache_dir aufs /var/spool/squid 1024 16 64
这是一个典型办公室透明代理的cache_dir配置,加速10GB到20GB的内容


2.cache_mem

很多文章说cache_mem要设置成物理内存数量的一半,比如物理内存2GB的cache_mem要设置成1GB。但是经过实际使用检验,这往往

是造成服务器内存超负荷的元凶。《Squid: The Definitive Guide》关于cache_mem这样说:“This directive doesn‘t 

entirely control the size of the Squid process”。实际使用当中内存占用往往超过cache_mem限定值很多,也就是说,

squid占用内存量似乎不受这个参数的控制。所以,此参数不必设得太大,适量就行,我的设置是:

服务器:
cache_mem 100 MB

局域网:
cache_mem 10 MB

另外,设置较小的maximum_object_size_in_memory值有助于有效控制squid过度占用内存,例如:
maximum_object_size 12288 KB
maximum_object_size_in_memory 24 KB

memory_pools off,也是一个不错的设置

题外话,个人觉得squid这个软件设计的比较失败的地方就是内存管理这一块...


3.透明代理

防火墙设置好端口拦截,然后这样设置可以实现透明代理:

httpd_accel_port 80 (0: 加速任何端口)
httpd_accel_host virtual
httpd_accel_uses_host_header on
httpd_accel_with_proxy on


这是使两种工作模式:代理模式和加速模式,同时工作。要想关闭代理模式,只要把httpd_accel_with_proxy设成off


4.http_port

http_port可以重复设置,使得squid在多个端口侦听

http_port 80
http_port 3128
http_port localhost:8080


5.访问控制

acl it_nets src 192.168.0.0/19 (限制访问ip)
acl over_conn_limit maxconn 20 (限制连接数)
acl illegal_access url_regex -i what_ever_you_want (限制访问内容)

http_access deny over_conn_limit
http_access deny illegal_access
http_access allow it_nets
http_access deny all


6.cache文件的更新周期

refresh_pattern ^/$ 0 66% 240 reload-into-ims
refresh_pattern ^.+/(<^.>*)$ 0 66% 720 reload-into-ims
(首页,更新得比较频繁,cache更新周期短一些)

refresh_pattern -i /.(htm|html|txt|js)$ 0 66% 1440 reload-into-ims
(一般页面,更新得没那么频繁,cache更新周期长一些)

refresh_pattern -i /.(bmp|gif|jpg|png|swf)$ 0 66% 10080 reload-into-ims
(图片、flash,更新得最不频繁,cache更新周期最长)

refresh_pattern . 0 66% 10080 reload-into-ims
(缺省,长一些)


reload-into-ims

When set, this option makes Squid transform a request with a no-cache directive into a validation 

(If-Modified-Since) request. In other words, Squid adds an If-Modified-Since header to the request 

before forwarding it on. Note that this only works for objects that have a Last-Modified timestamp. 

The outbound request retains the no-cache directive, so that it reaches the origin server.

大概意思是:只在源服务器上文件变动的情况下才实际更新cache当中的内容。

小技巧:Ctrl+F5可以强制浏览器和squid刷新它的缓存。

题外话:内容管理系统(CMS)在更新发布页面的时候,应该与cache服务器之间有一种沟通机制,通知cache刷新更新了的内容


7.杂项

negative_ttl 0 minutes

pipeline_prefetch on (对提升squid处理客户端请求的速度有帮助)

forwarded_for on

dns_nameservers 202.96.128.86 202.96.128.166


8.Squid+Apache

在apache服务器中前置squid,可以有效减少apache衍生进程的数量,从而减少内存消耗。

利用Squid的访问控制功能,可以实现内容过滤、连接数限制等apache力所不能及的功能。

利用Squid的I/O驱动的请求处理模式,更高效地实现静态内容请求处理,至于动态内容还是交给apache来处理。

9.max_filedesc 16384

这个不用多说了...

10.待续..
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值