Multiple Web varnish configuration examples and built-in variables

Source: Internet
Author: User
Tags varnish

Receive —————— Pass \ Pipe

| |

| |

| Fetch —————

| ↑ |

|    | |

Lookup ——— Miss |

|  |

| ↓

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



Varnishd Variable Type description:

Request arrives varnish, the VCL built-in common variables can be used

Req.backend # Specify the corresponding backend host

Req.request # Specifies the type of request: GET HEAD POST, etc.

Req.http.header # Specifies the HTTP header information in the request

Req.url #指定请求的地址

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

Req.restarts #表示请求重启的次数, the default maximum value is 4

Common variables built into the VCL that can be used when back-end host requests

Beresp.request #指定请求的类型, such as Get and head

Beresp.url #指定请求地址

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

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

Beresp.ttl #缓存的生存周期, that is, how long the cache is retained, in s

Beresp.status #指定内容的请求代码, such as 502,503

After you get the content from the cache or back-end host, you can use the

Obj.status #返回内容的请求状态代码, such as 200,302,504

Obj.cacheable #返回内容是否可以缓存, that is, if HTTP returns 200,203,300,301,302 404 or 410, and has a non-0 lifetime, it can be cached.

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

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

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

Obj.ttl #返回内容的生存周期, which is the cache time, is the unit s

Obj.lastuse #返回上一次请求到现在的间隔时间, Unit is s

Returned to the client, which can be used

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 was a basic VCL configuration file for varnish. See the VCL (7)

# Mans 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"; Ports for #定义web1的ip and Nginx

. Port = "8081";

}

Backend Webserver3 {

. Host = "10.2.16.253"; Ports for #定义web1的ip and 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 would be

# appended to your code.

#

Director webserver random {#定义一个director服务器组, by the backend two web to assume the request, uses the weight value to allocate the request probability;

{. backend = WebServer1;. Weight = 1;}

{. backend = Webserver3;. Weight = 1;}

}


ACL purge { #定义清除缓存规则, allows IP of 127.0.0.1 and 10.2.16.0 segments to clear the cache through purge method;

"127.0.0.1";

"10.2.16.0"/24;

}


Sub Vcl_recv {#Receive模块;

if (req.request = = "PURGE"){#当发送PURGE请求的客户端不在acl中设定的地址时, the 405 code is returned, prompting "not allowed."

if (!client.ip ~ purge) {

Error 405 "not allowed.";

}

ElseIf (req.url ~ "\. (php|cgi) ($|\?)") {#当请求的URL以php或cgi结尾时, it is not cached and is processed by the backend server.

return (pass);

}

else {

return (lookup);

}

}

if (req.url ~ "^/img") { #当URL中包括img时, send the request to the IMG server, otherwise, send it to the WEB1 server.

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", the request is distributed to the server group webserver.

Set req.backend = webserver;

}


#if (req.url ~ "^/images") { #清除 all caches under the/images directory, if the URL that is accessed matches this rule, the cookie in the header information is deleted.

# unset Req.http.cookie;

# }


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

{

return (pipe);

}

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

return (pass);

}

return(lookup);

}



Sub Vcl_hit # Hit module

{

if (req.request = = "PURGE") {# If the request type is the PURGE method, VARNISHD will set the cache period for this request to 0, which is to invalidate the cache of the URL, thus achieving the purpose of refreshing the varnish cache.

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

Error "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值, and process compressed content

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 = = | | 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" >

#

#

# <title> "} obj.status" "Obj.response {" </title>

#

# <body>

#

# <p> "} obj.response {" </p>

#

# <p>xid: "} req.xid {" </p>

#

# <address>

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

# </address>

# </body>

#

# "};

# return (deliver);

# }


This article is from the "Fate" blog, make sure to keep this source http://czybl.blog.51cto.com/4283444/1544837

Multiple Web varnish configuration examples and built-in variables

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.