varnish的VCL的配置详解
Varnish 的作者Poul-Henning Kamp是FreeBSD的内核开发者之一,他认为现在的计算机比起1975年已经复杂许多。在1975年时,储存媒介只有两种:内存与硬盘。但现在计算
.host = "www.example.com";
.port = "http";
.connect_timeout = 1s; 连接时间
.first_byte_timeout = 5s; 连接上后等待接受第一个字节的时间
.between_bytes_timeout = 2s; 在字节与字节之间的等待时间
}
.retries = 5;
{
.backend = b1;
.weight = 7;
}
{
.backend = {
.host = "fs2";
}
.weight = 3;
}
}
The random director(随即)
The round-robin director(轮询)
The client director 根据客户端(session cookie等)确认访问哪台服务器
The hash director(基于hash表来决定访问哪台服务器)
The DNS director
.list = {
.host_header = "www.example.com";
.port = "80";
.connect_timeout = 0.4;
"192.168.15.0"/24;
"192.168.16.128"/25;
}
.ttl = 5m; 查找缓存时间
.suffix = "internal.example.net"; 主机名后缀
}
健康检查
.host = "www.example.com";
.port = "http";
.probe = {
.url = "/test.jpg";
.timeout = 0.3 s;
.window = 8;
.threshold = 3;
.initial = 3;
}
}
.url = "/status.cgi";
.interval = 60s;
.timeout = 0.3 s;
.window = 8;
.threshold = 3;
.initial = 3;
}
.host = "www.example.com";
.port = "http";
.probe = healthcheck;
}
# NB: rn automatically inserted after each string!
.request =
"GET / HTTP/1.1"
"Host: www.foo.bar"
"Connection: close";
}
"localhost"; // myself
"192.0.2.0"/24; // and everyone on the local network
! "192.0.2.23"; // except for the dialin router
}
if (client.ip ~ local) {
return (pipe);
}
if (client.ip ~ local) {
return (pipe);
}
}
函数调用:
call pipe_if_local;
内置函数:
vcl_recv在请求开始的时候调用,判断是否处理该请求,怎样处理以及访问后台哪个服务器。
返回值可以为:
error code [reason]
返回指定错误代码,终止请求。
pass
切换到pass模式,调用vcl_pass函数。
pipe
切换到pipe模式,调用vcl_ pipe函数。
lookup
在缓存中查找请求对象,根据是否在缓存中调用vcl_hit or vcl_miss函数
vcl_pipe
在pipe模式下被调用,请求被传递到后台服务器,前台与后台直接多次交互,直到结束不写日志。
vcl_pass
在pass模式下被调用,请求被传递到后台服务器,前台与后台直接一次交互,不写日志。
The vcl_recv 会调用一下参数会停止。
error code [reason]
返回错误信息
pass
进入到pass模式
restart
重启事物。
vcl_hash
可以调用hash_data() 把数据添加到hash表中
vcl_hit
在cache中成功查找到数据会调用这个函数。
可以调用一下参数终止该函数
deliver
error code [reason]
pass
restart
vcl_miss
没有在内存中找到数据的时候调用这个函数,他的目的是是否从后台服务器上取数据,并且是从哪台服务器上取数据。
可以调用一下参数终止该函数
error code [reason]
pass
fetch
当后台服务器上的数据成功被加载到缓存中时调用。
可以调用一下参数终止该函数
deliver
数据被加载到cache中并传递给客户端。调用vcl_deliver
error code [reason]
pass
restart
vcl_deliver
缓存数据被传递给了客户端,调用这个函数。
可以调用一下参数终止该函数
deliver
error code [reason]
restart
vcl_error
当遇到后台或内部错误的时候会被调用。
可以调用一下参数终止该函数
deliver
Deliver the error object to the client.
restart
如果没有被定义会调用默认的。
函数样式:其中(?i)是正则表达式中不区分大小写的意思。
if (req.http.host ~ "(?i)example.com") {
set req.backend = foo;
} elsif (req.http.host ~ "(?i)example.org") {
set req.backend = bar;
}
}
now 当前时间
.host 源主机名或后台服务器IP地址
.port 源服务名或者服务端口号。
server.hostname varnish服务器主机名
server.identity varnish服务器标识
server.ip varnish服务器的IP
server.port varnish服务器的端口号
req.request 请求类型 get post head ……
req.url 请求的URL
req.proto client使用的HTTP协议版本
req.backend 提供这个请求的原服务器名
req.backend.healthy 提供这个请求的原服务是否正常提供服务
req.http.header 相应的HTTP的header。
req.hash_always_miss 强迫varnish忽略这一请求,直接从元数据库取数据。
req.hash_ignore_busy 在cache lookup时,忽略忙的对象。
req.can_gzip 客户端是否接受gzip编码
bereq.request 请求类型 head get post……
bereq.url
bereq.proto
bereq.http.header
bereq.connect_timeout 等待连接源数据库的时间(以秒计算)
bereq.first_byte_timeout 连接后,得到第一个字节的时间(以秒计算)
bereq.between_bytes_timeout 在两个字节之间的时间(以秒计算)
beresp.do_esi 解析ESI对象
beresp.do_gzip 存储前是否压缩
beresp.do_gunzip 存储前是否解压缩
beresp.proto 源数据库使用的HTTP协议。
beresp.status 由varnish返回的HTTP状态码
beresp.response 由varnish返回的 HTTP status信息
beresp.ttl 目标在cache中存活时间
obj.status 由varnish服务器返回的http协议状态码。
obj.response由varnish服务器返回的http协议状信息。
obj.ttl 对象还能存活的时间。
obj.lastuse 对象上次访问的大致时间
obj.hits 对象被访问的大致次数,如果是0,表示对象已经从cache中丢失。
req.hash 指定对象的hash key 在读和写的时候会被访问。
resp.proto response给客户端的HTTP 协议版本
resp.status 将被返回的HTTP协议状态码
resp.response将被返回的HTTP协议状态信息
resp.http.header 相应的HTTP header.
# Normalize the Host: header
if (req.http.host ~ "(?i)^(www.)?example.com$") {
set req.http.host = "www.example.com";
}
}
# Don't cache cookies
remove beresp.http.Set-Cookie;
}
set req.grace = 2m;
}
sub vcl_fetch {
set beresp.grace = 2m;
}
您可能感兴趣的文章:
varnish 503和varnish 400错误的解决方法
Drupal7配合Varnish使用及整合drupal模块的详细教程
varnish反向代理缓存配置详解
varnish的VCL的配置详解
Ubuntu下varnish的安装配置文档
varnish缓存策略及缓存时间计算方法
Varnish2.1.4配置缓存服务器
使用Xcache加速你的PHP网站
工作中的几个Drupal小问题记录总结
Drupal的缓存技术应用-让你的网站飞起来