Varnish是一款高性能的开源HTTP加速器,挪威最大的在线报纸 Verdens Gang 使用3台Varnish代替了原来的12台Squid,性能比以前更好。
Varnish 的作者Poul-Henning Kamp是FreeBSD的内核开发者之一,他认为现在的计算机比起1975年已经复杂许多。在1975年时,储存媒介只有两种:内存与硬盘。但现在计算机系统的内存除了主存外,还包括了CPU内的L1、L2,甚至有L3快取。硬盘上也有自己的快取装置,因此Squid Cache自行处理物件替换的架构不可能得知这些情况而做到最佳化,但操作系统可以得知这些情况,所以这部份的工作应该交给操作系统处理,这就是 Varnish cache设计架构。
varnish项目是2006年发布的第一个版本0.9.距今已经四年多了,此文档之前也提过varnish还不稳定,那是2007年时候编写的,经过varnish开发团队和网友们的辛苦耕耘,现在的varnish已经很健壮。很多门户网站已经部署了varnish,并且反应都很好,甚至反应比squid还稳定,且效率更高,资源占用更少。相信在反向代理,web加速方面,varnish已经有足够能力代替squid。
varnish比squid的优势
1、Varnish采用了“Visual Page Cache”技术,在内存的利用上,Varnish比Squid具有优势,它避免了Squid频繁在内存、磁盘中交换文件,性能要比Squid高。
2、Varnish的稳定性还不错,ryvius管理的一台图片服务器运行Varnish已经有一个月,没有发生过故障,而进行相同工作的Squid服务器就倒过几次。
3、通过Varnish管理端口,可以使用正则表达式快速、批量地清除部分缓存,这一点是Squid不能具备的。
varnish 中的对象
http://www.varnish-cache.org/docs/2.1/reference/vcl.html
server ,client
req == request ,bereq == backend request
resp == response,beresp == backend response
obj == cache object
varnish状态转移图
Work线程处理请求的过程是根据VCL的配置而定制的状态机,典型的处理流程如下
1.Receive,请求处理的入口状态,根据VCL判断该请求是Pass(跳过)还是进行Lookup(本地查询)
2.Lookup,在hash表中查找数据,若找到则进入hit状态,否则进入fetch状态。
3.Pass, 选择后台,进入fetch状态
4.Fetch,对请求进行后端的获取,发送请求,获得数据,并进行本地的存储
5.Deliver,,将数据发送给客户端,然后进入done
6.Done,处理结束事宜,对于一些请求需要做重新处理则可能重新进行状态转换或交给epoll
varnish 中的默认config
# This is a basic VCL configuration file for varnish. See the vcl(7) # man page for details on VCL syntax and semantics. # # Default backend definition. Set this to point to your content server. # 服务侦听地址和端口 backend default { .host = "127.0.0.1"; .port = "8080"; } # # Below is a commented-out copy of the default VCL logic. If you # redefine any of these subroutines, the built-in logic will be # appended to your code. sub vcl_recv { if (req.restarts == 0) { if (req.http.x-forwarded-for) { set req.http.X-Forwarded-For = req.http.X-Forwarded-For ", " client.ip; } else { set req.http.X-Forwarded-For = client.ip; } } #这里如果不是基本http操作就跳过 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. */ return (pipe); } #这里默认只cache GET 和 HEAD Method if (req.request != "GET" && req.request != "HEAD") { /* We only deal with GET and HEAD by default */ return (pass); } #是否需要认证 if (req.http.Authorization || req.http.Cookie) { /* Not cacheable by default */ return (pass); } #检查cache中有没有,相当于 cache.get(uri) return (lookup); } sub vcl_pipe { # 直接做管道通过,不做任何处理 # Note that only the first request to the backend will have # X-Forwarded-For set. If you use X-Forwarded-For and want to # have it set for all requests, make sure to have: # set bereq.http.connection = "close"; # here. It is not set by default as it might break some broken web # applications, like IIS with NTLM authentication. return (pipe); } sub vcl_pass { #去后台取数据,用于未命中的情况 return (pass); } sub vcl_hash { #生成hash key,对应一个缓存页面 url + ip set req.hash += req.url; if (req.http.host) { set req.hash += req.http.host; } else { set req.hash += server.ip; } return (hash); } sub vcl_hit { #cache.get 命中 if (!obj.cacheable) { return (pass); } #发送命中数据 return (deliver); } sub vcl_miss { #未命中,去服务器去取数据 return (fetch); } sub vcl_fetch { if (!beresp.cacheable) { return (pass); } if (beresp.http.Set-Cookie) { return (pass); } return (deliver); } sub vcl_deliver { return (deliver); } #varnish 错误情况 sub vcl_error { set obj.http.Content-Type = "text/html; charset=utf-8"; synthetic {" <?xml version="1.0" encoding="utf-8"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html> <head> <title>"} obj.status " " obj.response {"</title> </head> <body> <h1>Error "} obj.status " " obj.response {"</h1> <p>"} obj.response {"</p> <h3>Guru Meditation:</h3> <p>XID: "} req.xid {"</p> <hr> <p>Varnish cache server</p> </body> </html> "}; return (deliver); }
Varnish缓存的后端健康监测
backend fileserver{ .host = "127.0.0.1"; .port = "8080"; .probe = { .url = "/test.html"; .timeout = 3000 ms; .interval = 5 s; .window = 3; .threshold = 2; #3次检查里2次失败,就算不健康 } }
优化Linux内核参数
#主要是开大tcp 缓冲区和链接数量 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