原贴:http://longrujun.name/index.php/2008/11/08/scaling-apps-with-varnish/
Scaling Apps with Varnish
Varnish是一款高性能的反向代理和HTTP加速器,并非传说中Http Cache Server。这是Varnish作者的 Varnish http accelerator文档,少不了PK Squid。
不过本人认为其在一定程度上还无法达到完全替换Squid的功效。保留意见于此:
1、没有cache检测机制,当内存满了无有效处理机制;
2、一但进程Hang、Crash或者重启,内存完全释放出cache内容,所有requests都回origin servers;
3、高并发状态下CPU、IO、内存等开销均高于Squid
针对
1、确实Squid在内存控制着实不尽如人意。cache_mem设定理想的内存总量用于:In-Transit OBJECTs、HotOBJECTs、Negative-CachedOBJECTs。这些对象所对应的数据都存储在4KB的块里面。此参数指定理想中分配4KB块总数的最大限制,其中In-TransitOBJECTs具有最高的优先级。当输入数据需要额外空间 来存储的时候,negative-cached和hotOBJECTs将被释放。配合上disk和ram之间的交互cache_swap。在Cache达设置的峰值时会有按优先动作处理cacheobject。这点姑且看作略胜于Varnish
2、 Varnish基于Kernel和Ram的处理机制上比Squid成熟很多。进程挂了或reboot后,所有Cache内容将释放,Requests将全部回到Originalservers。这点上Squid还有Disk Cache即通俗的filesystem cache,尽管如Varnish作者表述的”It’svery slowly”,但总比直接去源端访问来的高效。估计Varnish设计中的2进程– 是想表达Automatic restarts oncrash的意思吧。
3、 不用多说了,在高并发情况下Varnish处理的请求远大于Squid,系统Load会高于Squid。这也是Varnish相比Squid高效便捷的一点。当然有利肯定需要承担相应的系统开销
总之Squid固然经典,但不妨碍Varnish作为一款性能卓越的HTTP Accelerator
还是交给应用来选择吧。本人管理的10台Squid中应用相对稳定,暂不打算用Varnish来替换
其中一台Cache info如下
Cache information for squid:
Request Hit Ratios: 5min: 88.8%, 60min: 88.1%
Byte Hit Ratios: 5min: 66.7%, 60min: 65.5%
Request Memory Hit Ratios: 5min: 25.6%, 60min: 25.8%
Request Disk Hit Ratios: 5min: 1.8%, 60min: 1.8%
Storage Swap size: 75470884 KB
Storage Mem size: 524016 KB
Mean Object Size: 31.30 KB
Requests given to unlinkd: 6197478
Varnish成功应用在挪威最大的在线报纸http://www.vg.no/:
使用3台Varnish代替了原来的12台squid。参见Here
有关Varnish架构手记请参见Here
A>.基本安装配置(Linux)
1、下载源码编译
#wget http://downloads.sourceforge.net/varnish/varnish-2.0.1.tar.gz?modtime=1224271223&big_mirror=0
# tar zxvf varnish-2.0.1.tar.gz
# cd varnish-2.0.1
# ./autogen.sh
# ./configure –prefix=/Data/app_1/varnish
#make
#make install
2、建立Varnish Cache目录
#mkdir –p /Data/vcache
3、创建Varnish配置文件:
vi /Data/app_1/varnish/etc/vcl.conf
内容如下
backend default {
.host = “x.com”;
.port = “80″;
}
backend B1 {
.host = “x.com”;
.port = “80″;
}
backend B2 {
.host = “y.com”;
.port = “80″;
}
backend B3 {
.host = “z.com”;
.port = “80″;
}
acl purge {
“localhost”;
“127.0.0.1″;
“192.168.1.0″/24;
“124.42.78.0″/32;
}
sub vcl_recv {
if (req.request == “PURGE”) {
if (!client.ip ~ purge) {
error 405 “Not allowed.”;
}
lookup;
}
if (req.http.host ~ “^(www.)?x.com$”) {
set req.http.host = “www.x.com”;
set req.backend = B1;
} elsif (req.http.host ~ “^y.com$”) {
set req.http.host = “y.com”;
set req.backend = B2;
} elsif (req.http.host ~ “^z.com$”) {
set req.http.host = “z.com”;
set req.backend = B3;
} else {
error 404 “NOT be cached”;
}
if (req.request != “GET” &&
req.request != “HEAD” &&
req.request != “PUT” &&
req.request != “POST” &&
req.request != “TRACE” &&
req.request != “OPTIONS” &&
req.request != “DELETE”) {
/* Non-RFC2616 or CONNECT which is weird. */
pipe;
}
if (req.request != “GET” && req.request != “HEAD”) {
/* We only deal with GET and HEAD by default */
pass;
}
if (req.http.Authorization || req.http.Cookie) {
/* Not cacheable by default */
pass;
}
if (req.request == “GET” && req.url ~ “/.(php)($|/?)”) {
pass;
}
lookup;
}
sub vcl_pipe {
pipe;
}
sub vcl_pass {
pass;
}
#sub vcl_hash {
# set req.hash += req.url;
# if (req.http.host) {
# set req.hash += req.http.host;
# } else {
# set req.hash += server.ip;
# }
# hash;
#}
sub vcl_hit {
if (req.request == “PURGE”) {
set obj.ttl = 0s;
error 200 “Purged.”;
}
}
sub vcl_miss {
if (req.request == “PURGE”) {
error 404 “Not in cache.”;
}
}
sub vcl_fetch {
# if (!obj.cacheable) {
# pass;
# }
# if (obj.http.Set-Cookie) {
#remove obj.http.Set-Cookie;
# pass;
# }
# if (obj.http.Cache-Control ~ “no-cache” || obj.http.Cache-Control ~ “private”) {
# pass;
# }
if (req.request == “GET” && req.url ~ “/.(txt|js)$”) {
set obj.ttl = 3600s;
}
else {
set obj.ttl = 30d;
}
}
Note
>1. 如果不提供命令行选项 (-b hostname:port ),则 backend default 部分将指定要连接的服务器。
>2. 当守护进程收到一个客户机请求时,将调用 vcl_recv() 函数。反过来,当从实际的 Web服务器检索到请求对象后或者对 Web 服务器的请求失败后,将调用 vcl_fetch()。如前述,如果 Cache-Control 或Pragma 报头被设为 no-cache,则 vcl_fetch() 也拒绝缓存。
>3. 在代码中,pass 操作暗示 “通过”,或者对于这次单独的请求/响应交换不执行任何操作。pipe还把数据不加改变地从客户机传递到服务器,但是对客户机与服务器之间的所有后续事务都这样做(pipe 是连续不间断的pass,直至任何一端关闭连接)。lookup 将尝试在缓存中查找响应,将把响应添加到缓存中。
这段配置文件解释一下:
A、Varnish通过反向代理请求后端B1/B2/B3,端口为80的web服务器;
B、Varnish允许localhost、127.0.0.1、1124.42.78.0/32三个来源IP通过PURGE方法清除缓存;
C、Varnish对域名为x.com/y.com/z.com的请求进行处理,非x/y/z域名的请求则返回“NOT be cached”;
D、Varnish对HTTP协议中的GET、HEAD请求进行缓存,对POST请求透过,让其直接访问后端Web服务器。之所以这样配置,是因为POST请求一般是发送数据给服务器的,需要服务器接收、处理,所以不缓存;
E、Varnish对以.txt和.js结尾的URL缓存时间设置1小时,对其他的URL缓存时间设置为30天。
4、建立Varnish启动、停止和reload脚本
#cd /Data/scripts
#vi start.sh
内容如下
#!/bin/sh
# file: start.sh
rm -f *.core
date -u
/Data/app_1/varnish/sbin/varnishd /
-a 0.0.0.0:80 /
-s file,/Data/vcache/varnish_cache.data,2g /
-f /Data/app_1/varnish/etc/vcl.conf /
-p thread_pool_max=1500 /
-p thread_pools=5 /
-p listen_depth=512 /
-T 127.0.0.1:5000 /
-p client_http11=on /
-P /Data/app_1/varnish/sbin/varnish.pid
#set log
/Data/app_1/varnish/bin/varnishncsa -a -w /Data/logs/varnish.log &
#vi stop.sh
内容如下
killall varnishd
killall varnishncsa
#reload.sh
内容如下
kill -HUP `cat /Data/app_1/varnish/sbin/varnish.pid`
5、启动Varnish
#ulimit -SHn 65536
#./start.sh
B>.管理Varnish:
1、查看Varnish服务器连接数与命中率:
/Data/app_1/varnish/bin/varnishstat
2、通过Varnish管理端口进行管理:
用help看看可以使用哪些Varnish命令:
/Data/app_1/varnish/bin/varnishadm -T 127.0.0.1:5000 help
# /Data/app_1/varnish/bin/varnishadm -T 127.0.0.1:5000 help
help [command]
ping [timestamp]
status
start
stop
stats
vcl.load <configname> <filename>
vcl.inline <configname> <quoted_VCLstring>
vcl.use <configname>
vcl.discard <configname>
vcl.list
vcl.show <configname>
param.show [-l] [<param>]
param.set <param> <value>
quit
purge.url <regexp>
purge.hash <regexp>
purge.list
3、Varnish中最常用的Url及Referer
#/Data/app_1/varnish/bin/varnishtop -i rxurl
# /Data/app_1/varnish/bin/varnishtop -i rxheader -I Referer
4、通过Varnish管理端口,使用正则表达式批量清除缓存:
(1)、例:清除类似http://x.com/a/index.html的URL地址):
/Data/app_1/varnish/bin/varnishadm -T 127.0.0.1:5000 url.purge /a/
(2)、例:清除类似http://x.com/tech的URL地址:
/Data/app_1/varnish/bin/varnishadm -T 127.0.0.1:5000 url.purge w*$
(3)、例:清除所有缓存:
/Data/app_1/varnish/bin/varnishadm -T 127.0.0.1:5000 url.purge *$
6、配置开机自动启动Varnish
vi /etc/rc.local
增加以下内容:
ulimit -SHn 65536
8、优化Linux内核参数
vi /etc/sysctl.conf
net.ipv4.ip_local_port_range = 1024 65536
net.core.rmem_max=16777216
net.core.wmem_max=16777216
net.ipv4.tcp_rmem=4096 87380 16777216
net.ipv4.tcp_wmem=4096 65536 16777216
net.ipv4.tcp_fin_timeout = 30
net.ipv4.tcp_keepalive_time = 300
net.ipv4.tcp_tw_recycle = 1
net.core.netdev_max_backlog = 30000
net.ipv4.tcp_no_metrics_save=1
net.core.somaxconn = 262144
net.ipv4.tcp_syncookies = 0
net.ipv4.tcp_max_orphans = 262144
net.ipv4.tcp_max_syn_backlog = 262144
net.ipv4.tcp_synack_retries = 2
net.ipv4.tcp_syn_retries = 2
#sysctl -p
附
1、Varnish官方网站: http://varnish.projects.linpro.no/
2、常见FAQ参考http://varnish.projects.linpro.no/wiki/FAQ
再分享一下我老师大神的人工智能教程吧。零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!https://blog.csdn.net/jiangjunshow