varnish状态引擎1

vcl:

  state engine:各引擎之间存一定程度上的相关性;前一个engine如果可以有多种下游engine,则上游engine需要用return指明

要转移的下游engine  

vcl_recv
vcl_hash
vcl_hit
vcl_miss
vcl_fetch
vcl_deliver
vcl_pipe
vcl_pass
vcl_error

  编程语言语法:

  (1)//, #, /* */ 用于注释;会被编译器忽略

  (2) sub $name: 用于定义子例程 

  例如:sub vcl_recv {

      }

  (3) 不支持循环操作

  (4) 有众多内置的变量,变量的可调用位置与state engine有密切相关性

  (5) 支持终止语句,return(action);但没有返回值

  (6) 整个语言只对一个“域”有效,叫做“域”专用

  (7) 操作符:=, ==, ~, !, &&, ||

条件判断语句:

  if (CONDITION) {

  } else {

  }

变量赋值:set name=value    unset name

req.http.HEADER:调用request报文中http协议的指定的HEADER首部

    例如:req.http.X-Forwarded-For     req.http.Auhtorization      req.http.cookie

req.request: 请求方法

state engine workflow(v3):

  vcl_recv --> vcl_hash --> vcl_hit --> vcl_deliver

  vcl_recv --> vcl_hash --> vcl_miss --> vcl_fetch --> vcl_deliver

  vcl_recv --> vcl_pass --> vcl_fetch --> vcl_deliver

  vcl_recv --> vcl_pipe

定义配置文件,先复制一份,cp /etc/varnish/default.vcl /etc/varnish/test.vcl

vim /etc/varnish/test.vcl  在sub vcl_deliver engine下添加如下配置

obj.hits(内置变量):此请求对象从缓存中命中的次数

上面的命令是如果命中了则添加X-Cache = "HIT"首部,没命中则添加X-Cahce = "MISS"首部

varnishadm -S /etc/varnish/secret -T 127.0.0.1:6082  登陆varnish

编译加载新的配置文件:

 在客户端对varnish访问

命中!

varnish中的内置变量:
  变量种类:
  client
  server
  req
  resp
  bereq
  beresp
  obj
  storage

bereq.http.HEADERS: 由varnish发往backend server的请求报文的指定首部

bereq.request:请求方法

bereq.backend:指明要调用的后端主机

beresp.status:后端服务器的响应的状态码

beresp.http.HEADER: 从backend server响应的报文的首部

beresp.ttl:后端服务器响应的内容的余下的生存时长

obj.ttl: 对象的ttl值

obj.hits:此对象从缓存中命中的次数

vim /etc/varnish/test.vcl    加一个变量

在浏览器调试界面中已经能看到varnish自己的ip地址添加上去了

在varnish4版本中sub vcl_recv engine中默认内置了如下规则

sub vcl_recv {
if (req.method == "PRI") {
/* We do not support SPDY or HTTP/2.0 */
return (synth(405));
}

if (req.method != "GET" &&
req.method != "HEAD" &&
req.method != "PUT" &&
req.method != "POST" &&
req.method != "TRACE" &&
req.method != "OPTIONS" &&
req.method != "DELETE") {
/* Non-RFC2616 or CONNECT which is weird. */
return (pipe);
}

if (req.method != "GET" && req.method != "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);
}
return (hash);
}

原文地址:https://www.cnblogs.com/linuxboke/p/5509832.html