首页 > 代码库 > 多台web的Varnish配置实例及内置变量详解

多台web的Varnish配置实例及内置变量详解

   Receive—————— Pass  \  Pipe

      | |  

      | |  

      | Fetch —————  

      |    ↑ |

      |    |

   Lookup ——— Miss |

     |  |

     |          ↓

   Hit ———————————deliver

 



varnishd变量类型详解:

 

 请求到达varnish,可以使用的VCL内置公用变量

 

 req.backend   # 指定对应的后端主机

 req.request   # 指定请求的类型:GET HEAD  POST  等

 req.http.header  # 指定请求中的http头信息

 req.url   #指定请求的地址

 req.proto   #表示client请求的http协议版本

 req.restarts  #表示请求重启的次数,默认最大值为4

 

 

 向后端主机请求时,可以使用的VCL内置的公用变量

 

 beresp.request   #指定请求的类型,例如GET 和HEAD等

 beresp.url    #指定请求地址

 beresp.proto     #指定client请求的http协议版本

 beresp.http.header  #指定请求中的http头信息

 beresp.ttl  #缓存的生存周期,也就是cache保留多长时间,单位是S

 beresp.status    #指定内容的请求代码,例如502,503等

 

 

 从cache或后端主机获取内容后,可以使用的

 

 obj.status       #返回内容的请求状态代码,例如200,302,504等

 obj.cacheable  #返回内容是否可以缓存,也就是说,如果http返回的是200,203,300,301,302 404 或410等,并且有非0的生存周期,则可以缓存。

 obj.valid        #是否是有效的http应答

 obj.response     #返回内容的请求状态信息

 obj.proto  #返回内容的http协议版本

 obj.ttl  #返回内容的生存周期,也就是缓存时间,单位是s

 obj.lastuse      #返回上一次请求到现在的间隔时间,单位是s

 

 返回给client的,可以使用的

 

 resp.status      #返回给client的http状态代码

 resp.proto  #返回给client的http协议版本

 resp.http.header #返回给client的http头部信息

 resp.response    #返回给client的http状态信息

 


 

 unset req.http.cookie   #清除cookie缓存 

 


default.vcl:   #实例仅供参考


# 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 webserver1 {

     .host = "127.0.0.1";    #定义web1的ip 和nginx的端口

     .port = "8081";

 }

 backend webserver3 {

     .host = "10.2.16.253";   #定义web1的ip 和nginx的端口

     .port = "8081";

 }

 backend img {

.host = "10.2.16.253";    #定义图片服务器的ip和nginx端口

.port = "8090";

 }


#backend webserver4 {

#    .host = "10.2.16.254";

#    .port = "8082";

#}

# 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.

director webserver random {                  #定义一个director服务器组,由后端两台web来承担请求,用权值来分配请求几率;

{.backend = webserver1; .weight = 1;}

{.backend = webserver3; .weight = 1;}


acl purge { #定义清除缓存规则,允许127.0.0.1和10.2.16.0 网段的ip通过PURGE方法清除缓存;

  "127.0.0.1";

"10.2.16.0"/24;

}


sub vcl_recv { #Receive模块;

if (req.request == "PURGE") { #当发送PURGE请求的客户端不在acl中设定的地址时,将返回405代码,提示“Not allowed.”

    if (!client.ip ~ purge) {

    error 405 "Not allowed.";

}

elseif (req.url ~ "\. (php|cgi)($|\?)") {    #当请求的URL以php或cgi结尾时,则不缓存,交给后端服务器处理。

return (pass);

}

else {

return (lookup);

}

}

if (req.url ~ "^/img") {         #当URL中包括img时,将请求发送给img服务器,否则,发送给web1服务器。

set req.backend = img;

} else {

set req.backend = webserver1;

}


#if ((req.http.host ~"www.xxx.com")&&(req.restarts ==0)) {

# set req.backend = webserver;

#} elseif (req.restarts == 1) {

# set req.backend = webserver4;

#}


if (req.http.host ~"www.xxx.com") {     #如果请求的URL是 "www.xxx.com",则将请求分发给服务器组webserver。

set req.backend = webserver;

}


#if (req.url ~ "^/images") {     #清除/images目录下的所有缓存,如果访问的URL匹配这个规则,那么头信息中的cookie就会被删除。

#  unset req.http.cookie;  

# }  


if (req.request != "GET" && req.request != "HEAD")    #如果请求不是GET和HEAD,则缓存

{

return (pipe);

}

elseif (req.http.Authenticate || req.http.Authorization) {      

return (pass);

}

return (lookup);

}



sub vcl_hit                            # Hit模块

{

  if (req.request == "PURGE") {        # 如果请求类型是PURGE方法,varnishd会将此请求的缓存周期设置为0,也就是使这个URL的缓存失效,从而达到刷新varnish缓存的目的。

set obj.ttl = 0s;                  #设置缓存周期为0

error 200 "Purged.";

}


if (!obj.cacheable)

{

return (pass);

}


if (obj.http.Vary)

{

unset obj.http.Vary;

}

}


sub vcl_miss  #Miss模块

{

  if (req.request == "PURGE") {      

error 404 "Not in cache.";

}

}


sub vcl_hash {  #Hash模块

set req.hash += req.url;  #定义hash值,并且处理压缩内容

if (req.http.host) {

set req.hash += req.http.host;

} else {

set req.hash += server.ip;

}

if ( req.http.Accept-Encoding ){

if (req.url ~ "\.(jpg|jpeg|png|xsl|xml|pdf|ppt|doc|docx|chm|rar|zip|bmp|jpeg|swf|ico|mp3|mp4|rmvb|ogg|mov|avi|wmv|swf|txt|png|gif|jpg|css|js|html|htm)$") {

} else {

set req.hash += req.http.Accept-Encoding;

}

}

return (hash);

}


sub vcl_fetch     #Fetch模块

{

if (!beresp.cacheable) {

return (pass);

}

if (beresp.http.Set-Cookie) {

return (pass);

}


if (beresp.status == 500 || beresp.status == 501 || beresp.status == 502 || beresp.status == 503 || beresp.status == 504 || beresp.status == 404)

#定义在什么状态下进入restart模式

{

  return (restart);

}

if (beresp.http.Pragma ~ "no-cache" || beresp.http.Cache-Control ~ "no-cache" || beresp.http.Cache-Control ~ "private")   

#定义不缓存内容含有哪些http头的请求

{

return (pass);

}


if (req.request == "GET" && req.url ~ "\.(css|js|html|htm)$") {            #定义不同内容的缓存时间

set beresp.ttl = 300s;

}

if (req.request == "GET" && req.url ~ "\.(gif|jpg|jpeg|bmp|png|tiff|img)$") {

set beresp.ttl = 3600s;

}

if (req.request == "GET" && req.url ~ "\.(svg|swf|ico|mp3|mp4|wav|rmvb|avi|wmv)$") {

set beresp.ttl = 10d;

}

return (deliver);

}


sub vcl_deliver {    #Deliver模块

if (obj.hits > 0) {

set resp.http.X-Cache = "HIT from www.xxx.com";

} else {

set resp.http.X-Cache = "MISS from www.xxx.com";

}

return (deliver);

}




# }

     #if (beresp.http.Set-Cookie) {

     #    return (pass);

     #}

    # return (deliver);

 #}

 

# 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>

#     <address>

#        <a href="http://www.varnish-cache.org/">Varnish cache server</a>

#     </address>

#   </body>

# </html>

# "};

#     return (deliver);

# }


本文出自 “Fate” 博客,请务必保留此出处http://czybl.blog.51cto.com/4283444/1544837

多台web的Varnish配置实例及内置变量详解