Linux 系统 squid反向代理配置及优化

字体大小: 中小 标准 ->行高大小: 标准

http_port 80 accel vport vhost http11

accel 表示http加速器模式,需要至少一个vhost/vport/defaultsize参数
defaultsite=domainname 如果http头中没有host,则将defaultsize作为默认的host
vhost 加速器支持host header方式的虚拟主机
vport 加速器支持基于ip的虚拟主机

http11 让squid支持http1.1

memory_replacement_policy heap GDSF
cache_mem 1024 MB
cache_swap_low 90 \\根据网站流量自行调整
cache_swap_high 98

maximum_object_size 1024 KB
minimum_object_size 0 KB
maximum_object_size_in_memory 1024 KB

acl manager proto cache_object
acl localhost src 127.0.0.1/255.255.255.255
acl SSL_ports port 443
acl Safe_ports port 80 # http
acl Safe_ports port 21 # ftp
acl Safe_ports port 443 # https
acl Safe_ports port 70 # gopher
acl Safe_ports port 210 # wais
acl Safe_ports port 1025-65535 # unregistered ports
acl Safe_ports port 280 # http-mgmt
acl Safe_ports port 488 # gss-http
acl Safe_ports port 591 # filemaker
acl Safe_ports port 777 # multiling http

refresh_pattern ^ftp: 1440 20% 10080
refresh_pattern ^gopher: 1440 0% 1440
refresh_pattern (cgi-bin|\?) 0 0% 0
refresh_pattern -i .html$ 60 90% 1440 reload-into-ims ignore-no-cache refresh-ims
refresh_pattern -i .gif$ 60 90% 1440 reload-into-ims ignore-no-cache refresh-ims
refresh_pattern -i .js$ 60 90% 1440 reload-into-ims ignore-no-cache refresh-ims
refresh_pattern -i .css$ 60 90% 1440 reload-into-ims ignore-no-cache refresh-ims
refresh_pattern -i .jpg$ 90 90% 1440 reload-into-ims ignore-no-cache refresh-ims
refresh_pattern -i .png$ 90 90% 1440 reload-into-ims ignore-no-cache refresh-ims
refresh_pattern -i .bmp$ 90 90% 1440 reload-into-ims ignore-no-cache refresh-ims
refresh_pattern -i .swf$ 90 90% 1440 reload-into-ims ignore-no-cache refresh-ims
refresh_pattern . 0 20% 4320


acl CONNECT method CONNECT
acl PURGE method PURGE
http_access allow manager localhost
http_access allow PURGE localhost
http_access deny manager
http_access deny !Safe_ports
http_access deny CONNECT !SSL_ports
http_access allow all
always_direct allow all

cache_effective_user squid
cache_effective_group squid

emulate_httpd_log on
logformat combined %>a %ui %un [%tl] "%rm %ru HTTP/%rv" %Hs %<st "%{Referer}>h" "%{User-Agent}>h" %Ss:%Sh
access_log /usr/local/squid/var/logs/access.log combined
redirect_rewrites_host_header of

cache_replacement_policy heap GDSF
cache_dir aufs /data/squid/cache/cache01 5120 16 128
cache_dir aufs /data/squid/cache/cache02 5120 16 128

cache_log /usr/local/squid/var/logs/cache.log
cache_store_log none
cache_swap_log /usr/local/squid/var/logs/swap.log
log_ip_on_direct on
pid_filename /usr/local/squid/var/logs/squid.pid

visible_hostname test
logfile_rotate 4

snmp_port 3401
acl snmppublic snmp_community public
snmp_access allow snmppublic localhost

 

优化部分:http://bbs.linuxtone.org/thread-1107-1-1.html

主要是/opt/bokee/squid/etc/squid.conf文件参数的配置:

#缓存用户UID
cache_effective_user nobody

#缓存用户组 GID
cache_effective_group nobody

#代理服务器的高速缓存;系统默认8M;注意,如果您的机器有N兆内存,那么,推荐您在这里添的数字是N/3。
cache_mem 768 MB

#定义缓冲区
#type: 为存储内型(ufs和asyncufs以及diskd等,ufs使用较多);
#/opt/bokee/squid/cache: 缓冲目录的路径
#8196M: 目录空间大小
#64: 第一层缓冲目录数
#256: 第一层目录下的每一个目录所包含的目录数
cache_dir diskd /opt/bokee/squid/cache 10240 16 256 Q1=72 Q2=64

#将cache目录的所有者更改为nobody
#chown -R nobody:nobody /opt/bokee/squid/cache

关于Cache目录的建议:
由于cache目录是经常的读写,所以最好硬盘能用SCSI的,速度比较快而且稳定。
如果我们的cache大概需要40G的大小,那么我们尽量使用多硬盘,不要当纯用一个40G的硬盘,
可以使用4个10G的硬盘,这样,对于cache的速度更快。比如,当你有10M的东西要写到cache中,
如果是只是用一个硬盘的话,虽然可能你已经将4个cache目录分别放在4个分区,可是你只有一个硬盘,同时只有一个在写入,
可是当你有4个硬盘的时候,你每个硬盘就只要写入2.5M的东西,那样是不是更快呢?
还有,建议将每个cache目录单独存放在一个分区中,分区不要划分太大,一般2至4G就行,这样proxy搜索资料的时候不用耗费太多的时间。

#icp_port参数指定Squid从邻居服务器缓冲内发送和接收ICP请求的端口号。
#这里设置为0是因为这里配置Squid为内部Web服务器的加速器,所以不需要使用邻居服务器的缓冲。
icp_port 0

#swap 性能微调
#有时候由于用户的不正常操作,可能会使与squid的TCP连接处于半关闭状态,这时候,该TCP连接的发送端已经关闭,而接收端正常工作。
#默认时,该值设为on. squid将一直保持这种处于半关闭状态的TCP连接,直到返回套接字的读写错误才将其关闭。
#如果将该值设为off,则一旦从客户端 返回“no more data to read”的信息,squid就立即关闭该连接
half_closed_clients off

#squid使用大量的交换空间来存储对象。过了一定的时间以后,该交换空间就会用完,
#所以还必须定期的按照某种指标来将低于某个水平线的对象清除。 squid使用所谓的“最近最少使用算法”(LRU)来做这一工作。
#当已使用的交换空间达到cache_swap_high时,squid就根据LRU所计算的得到每个对象的值,将低于某个水平线的对象清除。
#这种清除工作一进行到已用空间达到cache_swap_low。这两个值用百分比表示,
#如果所使用的交换空间很大的话,建议减少这两个值得差距,因为这时一个百分点就可能是几百兆空间,这势必影响squid的性能。
#默认为:90-95(大于95会造成不稳定)
cache_swap_high 93
cache_swap_low 88

#maximum_object_size 大于该值的对象将不被存储。如果想要提高访问速度,就请降低该值;如果想最大限度地节约带宽,降低成本,请增加该值。
#默认值为:4096 KB
maximum_object_size 2048 KB

#以下是系统默认值
minimum_object_size 0 KB

#在内存中单个文件最大缓存大小,超过这个大小将不缓存到内存中
maximum_object_size_in_memory 256 KB

#reference_age 3 months
squid根据对象的LRU(最近最少使用算法)来清除对象,squid依据使用磁盘空间的总量动态地计算对象的LRU年龄。
我们用 reference_age定义对象的最大LRU年龄。如果一个对象在指定的reference_age内没有被访问,squid将删除该对象。默认值为一个月。
可以使用如下所示的时间表示方法。
1 week
3.5 days
4 months
2.2 hours

#定义all名为所有ip
acl all src 0.0.0.0/0.0.0.0

#dstdomain指代理服务器代理的主机. 定义bolg名为所有一下的域名的主机
acl blog dstdomain .blogchina.com
acl blog dstdomain localhost
acl blog dstdomain .bokee.com

#允许blog类使用代理服务器
http_access allow blog

#允许使用代理服务器(中间件被其它的squid调用)
#icp_access allow all

#acl denywords url_regex -i sex
#http_access deny badwords
#以上2句不允许使用该缓存服务器访问URL正则表达式中含sex字样的URL

hierarchy_stoplist .jsp Ctrl
acl QUERY urlpath_regex .jsp Ctrl
no_cache deny QUERY
#以上3句不缓存url中有.jsp或Ctrl的页面;直接访问主机.

#默认用户禁止使用代理服务器
http_access deny all

#客户端代理端口(本机的公网ip)
http_port ***.***.***.***:80 vhost vport

### httpd 透明代理设置

#squid2.5


#反向代理的WEB服务端口号。
httpd_accel_port 80

#此处设置反向代理的主机名,如果对后面多个域名进行缓冲,请使用虚拟主机模式(hosts文件中)。
httpd_accel_host virtual(127.0.0.1单个域名时;直接用ip而不用virtual;但须加httpd_accel_single_host on)
或用dns服务器: #dns_nameservers 60.191.254.49

#此处设置开反向代理的同时,是否开普通代理缓存服务。
#如果这行不注释掉,就没有高速缓存功能。不正向代理(不让外面用)就off;默认为on
httpd_accel_with_proxy off

#设定http1.1协议支持
httpd_accel_uses_host_header on

#squid2.6反向代理

cache_peer 127.0.0.1 parent 80 0 no-query originserver

#发生错误时,生成提示所显示的缓存服务器名
visible_hostname www.bokee.com

#发生错误时,生成提示所显示的缓存服务器管理员名
#cache_mgr jhzhou@bokee-inc.com

#打开“emulate_httpd_log”选项,将使Squid仿照Web服务器的格式创建访问记录。
#如果希望使用Web访问记录分析程序,就需要设置这个参数. 默认为off
emulate_httpd_log on

#不写日志
cache_access_log none
cache_log none
cache_store_log none

#写日志
#cache_store_log /var/log/squid/store.log
#cache_access_log /var/log/squid/access.log
#cache_log /var/log/squid/cache.log

#客户端不持续连接
client_persistent_connections off

#设置snmp监控的共同体有密码为123; 默认为public
acl snmppublic snmp_community 123

#设置snmp监控端口;默认为3401
snmp_port 3401

#允许所有的计算机访问snmppublic;默认为all
snmp_access allow snmppublic all

#时间设置
#消极存储对象的生存时间。所谓的消极存储对象,就是诸如“连接失败”及404 Not Found等一类错误信息。默认为:5 minutes。
negative_ttl 1 minutes

#缓存失败的D N S查询结果的生存时间。默认为5min。
negative_dns_ttl 1 minutes

#缓存成功的DNS查询结果的生存时间。默认为6小时。
positive_dns_ttl 1 hours

#在建立与客户的连接后, squid将花多长时间等待客户发出HTTP请求。默认值为30s。
request_timeout 1 minutes

#squid等待连接完成的超时值。默认值为2min。
connect_timeout 1 minutes

#持续连接时间。默认值为1min。
persistent_request_timeout 1 minutes

#squid在与其他服务器和代理建立连接后,该连接闲置多长时间后被关闭。默认值为120秒。
pconn_timeout 1 minutes

vi /etc/sysctl.conf


增加以下几行:

引用

net.ipv4.tcp_fin_timeout = 30


net.ipv4.tcp_keepalive_time = 1200


net.ipv4.tcp_syncookies = 1


net.ipv4.tcp_tw_reuse = 1


net.ipv4.tcp_tw_recycle = 1


net.ipv4.ip_local_port_range = 1024

65000
net.ipv4.tcp_max_syn_backlog = 8192
net.ipv4.tcp_max_tw_buckets = 5000
说明:
net.ipv4.tcp_syncookies = 1 表示开启SYN Cookies。当出现SYN等待队列溢出时,启用cookies来处理,可防范少量SYN攻击,默认为0,表示关闭;
net.ipv4.tcp_tw_reuse = 1 表示开启重用。允许将TIME-WAIT sockets重新用于新的TCP连接,默认为0,表示关闭;
net.ipv4.tcp_tw_recycle = 1 表示开启TCP连接中TIME-WAIT sockets的快速回收,默认为0,表示关闭。
net.ipv4.tcp_fin_timeout = 30 表示如果套接字由本端要求关闭,这个参数决定了它保持在FIN-WAIT-2状态的时间。
net.ipv4.tcp_keepalive_time = 1200 表示当keepalive起用的时候,TCP发送keepalive消息的频度。缺省是2小时,改为20分钟。
net.ipv4.ip_local_port_range = 1024 65000 表示用于向外连接的端口范围。缺省情况下很小:32768到61000,改为1024到65000。
net.ipv4.tcp_max_syn_backlog = 8192 表示SYN队列的长度,默认为1024,加大队列长度为8192,可以容纳更多等待连接的网络连接数。
net.ipv4.tcp_max_tw_buckets = 5000 表示系统同时保持TIME_WAIT套接字的最大数量,如果超过这个数字,TIME_WAIT套接字将立刻被清除并打印警告信息。默认为180000,改 为5000。对于Apache、Nginx等服务器,上几行的参数可以很好地减少TIME_WAIT套接字数量,但是对于Squid,效果却不大。此项参 数可以控制TIME_WAIT套接字的最大数量,避免Squid服务器被大量的TIME_WAIT套接字拖死。

此文章由 http://www.ositren.com 收集整理 ,地址为: http://www.ositren.com/htmls/67448.html