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: \r\n 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  相应的HTTPheader

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.responsevarnish服务器返回的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 cache2分钟,这样就可以不用使用户一直等待了,虽然数据不是最新,但是总比用户无法访问强。

sub vcl_recv {

  set req.grace = 2m;

}

sub vcl_fetch {

  set beresp.grace = 2m;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值