Jquery中文网 www.jquerycn.cn
Jquery中文网 >  服务器技术  >  应用加速  >  正文 varnish的VCL的配置详解

varnish的VCL的配置详解

发布时间:2014-08-14   编辑:www.jquerycn.cn
varnish的VCL的配置详解, Varnish是一款高性能的开源HTTP加速器...
    Varnish是一款高性能的开源HTTP加速器,挪威最大的在线报纸 Verdens Gang 使用3台Varnish代替了原来的12台Squid,性能比以前更好。
    Varnish 的作者Poul-Henning Kamp是FreeBSD的内核开发者之一,他认为现在的计算机比起1975年已经复杂许多。在1975年时,储存媒介只有两种:内存与硬盘。但现在计算
机系统的内存除了主存外,还包括了CPU内的L1、L2,甚至有L3快取。硬盘上也有自己的快取装置,因此Squid Cache自行处理物件替换的架构不可能得知这些情况而做到最佳化,
但操作系统可以得知这些情况,所以这部份的工作应该交给操作系统处理,这就是 Varnish cache设计架构。
varnish的VCL的配置详解
主机配置
复制代码 代码如下:
backend www {
  .host = "www.example.com";
  .port = "http";
  .connect_timeout = 1s;     连接时间
  .first_byte_timeout = 5s;    连接上后等待接受第一个字节的时间
  .between_bytes_timeout = 2s; 在字节与字节之间的等待时间
}
多个主机实现负载均衡。
复制代码 代码如下:
director b2 random {
  .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
复制代码 代码如下:
director directorname dns {
        .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";   主机名后缀
}

健康检查
复制代码 代码如下:
backend www {
  .host = "www.example.com";
  .port = "http";
  .probe = {
    .url = "/test.jpg";
    .timeout = 0.3 s;
    .window = 8;
    .threshold = 3;
    .initial = 3;
  }
}
 
Or it can be defined separately and then referenced::
复制代码 代码如下:
probe healthcheck {
   .url = "/status.cgi";
   .interval = 60s;
   .timeout = 0.3 s;
   .window = 8;
   .threshold = 3;
   .initial = 3;
}
backend www {
  .host = "www.example.com";
  .port = "http";
  .probe = healthcheck;
}
 
If you have many backends this can simplify the config a lot.
It is also possible to specify the raw HTTP request:
复制代码 代码如下:
probe rawprobe {
    # NB: rn automatically inserted after each string!
    .request =
      "GET / HTTP/1.1"
      "Host: www.foo.bar"
      "Connection: close";
}
 
An ACL declaration creates and initializes a named access control list which can later be used to match client addresses::
复制代码 代码如下:
acl local {
  "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);
}
函数形式
复制代码 代码如下:
sub pipe_if_local {
  if (client.ip ~ local) {
    return (pipe);
  }
}
 
Subroutines in VCL do not take arguments, nor do they return values.
函数调用:
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
vcl_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)是正则表达式中不区分大小写的意思。
复制代码 代码如下:
sub vcl_recv {
  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 源服务名或者服务端口号。
client.ip 客户端IP
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编码
下面参数是varnish访问源数据库时使用:
bereq.request  请求类型 head get post……
bereq.url
bereq.proto
bereq.http.header
bereq.connect_timeout 等待连接源数据库的时间(以秒计算)
bereq.first_byte_timeout 连接后,得到第一个字节的时间(以秒计算)
bereq.between_bytes_timeout 在两个字节之间的时间(以秒计算)
从源数据库中得到数据,在写入cache之前使用的变量,也就是说:在vcl_fetch中使用的变量:
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中存活时间
目标对象被加载到cache中后,下面的变量可以使用,也就是说在 vcl_hit and vcl_deliver.函数中使用的变量:
obj.proto 对象检索时使用的http协议。
obj.status  由varnish服务器返回的http协议状态码。
obj.response由varnish服务器返回的http协议状信息。
obj.ttl  对象还能存活的时间。
obj.lastuse 对象上次访问的大致时间
obj.hits  对象被访问的大致次数,如果是0,表示对象已经从cache中丢失。
下面的变量在确定一个对象的hash值的时候可用。
req.hash  指定对象的hash key 在读和写的时候会被访问。
下面的变量在准备反应回给客户端的时候会被使用:
resp.proto response给客户端的HTTP 协议版本
resp.status  将被返回的HTTP协议状态码
resp.response将被返回的HTTP协议状态信息
resp.http.header   相应的HTTP header.
变量实例:
复制代码 代码如下:
sub vcl_recv {
  # Normalize the Host: header
  if (req.http.host ~ "(?i)^(www.)?example.com$") {
    set req.http.host = "www.example.com";
  }
}
 
HTTP headers can be removed entirely using the remove keyword::
复制代码 代码如下:
sub vcl_fetch {
  # Don't cache cookies
  remove beresp.http.Set-Cookie;
}
有时候我们的源数据库组织数据比较慢我们可以设定函数如下,这样,每个对象都会被保存在varnish cache中2分钟,这样就可以不用使用户一直等待了,虽然数据不是最新,但
是总比用户无法访问强。
复制代码 代码如下:
sub vcl_recv {
  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的缓存技术应用-让你的网站飞起来

[关闭]