varnish4.0 流程图以及说明

varnish4.0 流程图以及说明

varnish4.0 流程图以及说明

varnish4.0 流程图以及说明

varnish 中的内置变量

req
repos
client
server
bereq
beresp bereq
bereq.http.HEADER 由varnish发往backend server 的请求报文指定首部
bereq.request 请求方法
bereq.url
bereq.proto: 协议版本 beresp
bereq.proto
bereq.status:
bereq.backend.ip
beresp.backend.name
beresp.http.HEADER 从backend server 响应报文指定首部
bereq.ttl 后端服务器响应的内容的余下的生存时长; 官方文档
http://www.varnish-cache.org/trac/wiki/VCLExampleDirector
支持虚拟主机:
if (req.http.host == "www.fengpic.com") { } 强制对某些资源的请求,不检查缓存:
/admin
/login if (req.url ~ "(?i)^/login" || req.url ~ "(?i)^/admin") {
return(pass)
} test.html
if (req.url ~ "(?i)test.html$") {
return(pass)
} sub vcl_recv{
  if (req.url ~"(?i)\.(jpg|png|gif)$") {
    set req.backend_hint = "fdfs_g1_s1"
  else {
    set req.backend_hint = "fdfs_g1_s2"
    }
  } 对域名进行缓存 sub vcl_recv {
  if (req.http.host ~ "^(ww.)?example.com%") {
    set req.backend_hint = vdir1.backend();
  }
} 对多个group进行缓存

backend fdfs_g1_s1 {
.host = "192.168.20.151";
.port = "8080";
.probe = {
.url = "/";
.timeout = 1s;
.interval = 10s;
.window = 5;
.threshold = 2;
}
}


backend fdfs_g1_s2 {
.host = "192.168.20.152";
.port = "8080";
.probe = {
.url = "/";
.timeout = 1s;
.interval = 10s;
.window = 5;
.threshold = 2;
}
}


backend fdfs_g2_s1 {
.host = "192.168.20.153";
.port = "8080";
.probe = {
.url = "/";
.timeout = 1s;
.interval = 10s;
.window = 5;
.threshold = 2;
}
}


backend fdfs_g2_s2 {
.host = "192.168.20.154";
.port = "8080";
.probe = {
.url = "/";
.timeout = 1s;
.interval = 10s;
.window = 5;
.threshold = 2;
}
}


sub vcl_init {
new vdir1 = directors.round_robin();
vdir1.add_backend(fdfs_g1_s1);
vdir1.add_backend(fdfs_g1_s2);

new vdir2 = directors.round_robin();
vdir2.add_backend(fdfs_g2_s1);
vdir2.add_backend(fdfs_g2_s2);

return (ok);
}


sub vcl_recv {
  if (req.url ~ "($i)^/group1") {
    set req.backend_hint = vdir1.backend();
  }
  if (req.url ~ "($i)^/group2") {
    set req.backend_hint = vdir2.backend();
  }
}


首先是必须定义版本号: vcl 4.0。VMOD’s更独立化,官方推荐是加载Standard VMOD’s(std)。

另外director已变为VMOD,如需使用,需要import directors。

vcl_fetch函数被vcl_backend_response和vcl_backend_fetch代替,且req.*不再适用vcl_backend_response,只能使用bereq.*。

至于vcl_backend_fetch貌似没哪个doc见到详细用法。

error变更为return(synth(http_code,message)),req.backend成了req.backend_hint,req.request变更为req.method,obj为只读对象了。

vcl_synth采用resp.*,而非原来的obj.*。

vcl_error变更为vcl_backend_error,必须使用beresp.*,而不是obj.*。

关键字"purge;"命令,已被去除。在vcl_recv使用return(purge)。

hit_for_pass通过set beresp.uncacheable = true;来指定。

vcl_recv必须将lookup变更返回hash,vcl_hash必须将hash变更返回lookup,vcl_pass必须将pass变更返回fetch。

req.backend.healty被std.healthy(req.backend)代替,但是设置不了grace,鸡肋,被抛弃了,现在仅能做的就是keepalive的作用了。

req、bereq,resp、beresp之间不同,可被使用的位置不同。

server.port、client.port分别变更为std.port(server.ip)、std.port(client.ip),跟上面healthy一样,需要import std。

session_linger变更为timeout_linger,sess_timeout变更为timeout_idle,sess_workspace被抛弃了。

remove被完全弃用了,不过我一直用unset的说。

return(restart)变更为return(retry),vcl_backend_fetch会被使用到。

自定义sub函数不能以vcl_开头,调用方式call udf。
 vcl 4.0;

 import std;
import directors; backend fdfs_g1_s1 {
.host = "192.168.20.151";
.port = "";
.probe = {
.url = "/";
.timeout = 1s;
.interval = 10s;
.window = ;
.threshold = ;
}
} backend fdfs_g1_s2 {
.host = "192.168.20.152";
.port = "";
.probe = {
.url = "/";
.timeout = 1s;
.interval = 10s;
.window = ;
.threshold = ;
}
} acl purgers {
"localhost";
"127.0.0.1";
} sub vcl_init {
new vdir = directors.round_robin();
vdir.add_backend(fdfs_g1_s1);
vdir.add_backend(fdfs_g1_s2);
return (ok);
} sub vcl_recv { set req.backend_hint = vdir.backend(); if (req.http.x-forwarded-for) {
set req.http.X-Forwarded-For = req.http.X-Forwarded-For + ", " + client.ip;
} else {
set req.http.X-Forwarded-For = client.ip;
} if (req.method == "PURGE") { # PURGE请求的处理
if (!client.ip ~ purgers) {
return(synth(,"Method not allowed"));
}
#清理缓存
return (purge);
}
# 禁止缓存的文件
if (req.url ~ "\.(php|jsp|do|cgi)$") {
return (pass);
} if (req.method == "PRI") {
#/* We do not support SPDY or HTTP/2.0 */
return (synth());
}
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);
} sub vcl_backend_response { if (beresp.http.cache-control !~ "s-maxage") {
if (bereq.url ~ "\.(gif|jpeg|jpg|png)$") {
set beresp.ttl = 3600s;
unset beresp.http.set-cookie;
} if (bereq.url ~ "\.(css|js)$") {
set beresp.ttl = 600s;
unset beresp.http.set-cookie;
} if (bereq.url ~ "^[^?]*\.(7z|avi|bz2|flac|flv|amr|gz|mka|mkv|mov|mp3|mp4|mpeg|mpg|ogg|ogm|opus|rar|tar|tgz|tbz|txz|wav|webm|xz|zip)(\?.*)?$") {
unset beresp.http.set-cookie;
set beresp.do_stream = true;
set beresp.do_gzip = false;
}
} else {
if (beresp.ttl > 0s) {
unset beresp.http.Set-Cookie;
}
} if (beresp.ttl <= 0s || beresp.http.Set-Cookie || beresp.http.Surrogate-control ~ "no-store" || (!beresp.http.Surrogate-Control && beresp.http.Cache-Control ~ "no-cache|no-store|private") || beresp.http.Vary == "*") {
set beresp.ttl = 120s;
set beresp.uncacheable = true;
} return (deliver);
} sub vcl_backend_fetch {
return (fetch);
} sub vcl_pipe {
# By default Connection: close is set on all piped requests, to stop
# connection reuse from sending future requests directly to the
# (potentially) wrong backend. If you do want this to happen, you can undo
# it here.
# unset bereq.http.connection;
return (pipe);
} sub vcl_pass {
return (fetch);
} sub vcl_hash {
hash_data(req.url);
if (req.http.host) {
hash_data(req.http.host);
} else {
hash_data(server.ip);
}
return (lookup);
} sub vcl_purge {
return (synth(, "Purged"));
} sub vcl_hit {
if (obj.ttl >= 0s) {
#// A pure unadultered hit, deliver it
return (deliver);
}
if (obj.ttl + obj.grace > 0s) {
#// Object is in grace, deliver it
#// Automatically triggers a background fetch
return (deliver);
}
#// fetch & deliver once we get the result
return (miss);
} sub vcl_miss {
return (fetch);
} sub vcl_deliver {
set resp.http.Via = "cache";
unset resp.http.X-Varnish;
set resp.http.Server="cache-server";
unset resp.http.Age;
# set resp.http.x-hits = obj.hits; if (obj.hits > ) {
set resp.http.X-Cache = "Hit aaaaaa CDN";
}else {
set resp.http.X-Cache = "Miss";
}
return (deliver);
} sub vcl_synth {
set resp.http.Content-Type = "text/html; charset=utf-8";
set resp.http.Retry-After = "";
synthetic( {"<!DOCTYPE html>
<html>
<head>
<title>"} + resp.status + " " + resp.reason + {"</title>
</head>
<body>
<h1>Error "} + resp.status + " " + resp.reason + {"</h1>
<p>"} + resp.reason + {"</p>
<h3>Guru Meditation:</h3>
<p>XID: "} + req.xid + {"</p>
<hr>
<p>Varnish cache server</p>
</body>
</html>
"} );
return (deliver);
} sub vcl_backend_error {
set beresp.http.Content-Type = "text/html; charset=utf-8";
set beresp.http.Retry-After = "";
synthetic( {"<!DOCTYPE html>
<html>
<head>
<title>"} + beresp.status + " " + beresp.reason + {"</title>
</head>
<body>
<h1>Error "} + beresp.status + " " + beresp.reason + {"</h1>
<p>"} + beresp.reason + {"</p>
<h3>Guru Meditation:</h3>
<p>XID: "} + bereq.xid + {"</p>
<hr>
<p>Varnish cache server</p>
</body>
</html>
"} );
return (deliver);
} sub vcl_fini {
return (ok);
}
 vcl 4.0;

 import std;
import directors; backend fdfs_g1_s1 {
.host = "192.168.20.151";
.port = "";
.probe = {
.url = "/";
.timeout = 1s;
.interval = 10s;
.window = ;
.threshold = ;
}
} backend fdfs_g1_s2 {
.host = "192.168.20.152";
.port = "";
.probe = {
.url = "/";
.timeout = 1s;
.interval = 10s;
.window = ;
.threshold = ;
}
} backend fdfs_g2_s1 {
.host = "192.168.20.113";
.port = "";
.probe = {
.url = "/";
.timeout = 1s;
.interval = 10s;
.window = ;
.threshold = ;
}
} backend fdfs_g2_s2 {
.host = "192.168.20.115";
.port = "";
.probe = {
.url = "/";
.timeout = 1s;
.interval = 10s;
.window = ;
.threshold = ;
}
} acl purgers {
"localhost";
"127.0.0.1";
} sub vcl_init {
new g1vdir = directors.round_robin();
g1vdir.add_backend(fdfs_g1_s1);
g1vdir.add_backend(fdfs_g1_s2); new g2vdir = directors.round_robin();
g2vdir.add_backend(fdfs_g2_s1);
g2vdir.add_backend(fdfs_g2_s2); return (ok);
} sub vcl_recv { if (req.url ~ "/group1") {
set req.backend_hint = g1vdir.backend();
} if (req.url ~ "/group2") {
set req.backend_hint = g2vdir.backend();
} if (req.http.x-forwarded-for) {
set req.http.X-Forwarded-For = req.http.X-Forwarded-For + ", " + client.ip;
} else {
set req.http.X-Forwarded-For = client.ip;
} if (req.method == "PURGE") { # PURGE请求的处理
if (!client.ip ~ purgers) {
return(synth(,"Method not allowed"));
}
#清理缓存
return (purge);
}
# 禁止缓存的文件
if (req.url ~ "\.(php|jsp|do|cgi)$") {
return (pass);
} if (req.method == "PRI") {
#/* We do not support SPDY or HTTP/2.0 */
return (synth());
}
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);
} sub vcl_backend_response { if (beresp.http.cache-control !~ "s-maxage") {
if (bereq.url ~ "\.(gif|jpeg|jpg|png)$") {
set beresp.ttl = 3600s;
unset beresp.http.set-cookie;
} if (bereq.url ~ "\.(css|js)$") {
set beresp.ttl = 600s;
unset beresp.http.set-cookie;
} if (bereq.url ~ "^[^?]*\.(7z|avi|bz2|flac|flv|amr|gz|mka|mkv|mov|mp3|mp4|mpeg|mpg|ogg|ogm|opus|rar|tar|tgz|tbz|txz|wav|webm|xz|zip)(\?.*)?$") {
unset beresp.http.set-cookie;
set beresp.do_stream = true;
set beresp.do_gzip = false;
}
} else {
if (beresp.ttl > 0s) {
unset beresp.http.Set-Cookie;
}
} if (beresp.ttl <= 0s || beresp.http.Set-Cookie || beresp.http.Surrogate-control ~ "no-store" || (!beresp.http.Surrogate-Control && beresp.http.Cache-Control ~ "no-cache|no-store|private") || beresp.http.Vary == "*") {
set beresp.ttl = 120s;
set beresp.uncacheable = true;
} return (deliver);
} sub vcl_backend_fetch {
return (fetch);
} sub vcl_pipe {
# By default Connection: close is set on all piped requests, to stop
# connection reuse from sending future requests directly to the
# (potentially) wrong backend. If you do want this to happen, you can undo
# it here.
# unset bereq.http.connection;
return (pipe);
} sub vcl_pass {
return (fetch);
} sub vcl_hash {
hash_data(req.url);
if (req.http.host) {
hash_data(req.http.host);
} else {
hash_data(server.ip);
}
return (lookup);
} sub vcl_purge {
return (synth(, "Purged"));
} sub vcl_hit {
if (obj.ttl >= 0s) {
#// A pure unadultered hit, deliver it
return (deliver);
}
if (obj.ttl + obj.grace > 0s) {
#// Object is in grace, deliver it
#// Automatically triggers a background fetch
return (deliver);
}
#// fetch & deliver once we get the result
return (miss);
} sub vcl_miss {
return (fetch);
} sub vcl_deliver {
set resp.http.Via = "cache";
unset resp.http.X-Varnish;
set resp.http.Server="cache-server";
unset resp.http.Age;
# set resp.http.x-hits = obj.hits; if (obj.hits > ) {
set resp.http.X-Cache = "Hit aaaaaa CDN";
}else {
set resp.http.X-Cache = "Miss";
}
return (deliver);
} sub vcl_synth {
set resp.http.Content-Type = "text/html; charset=utf-8";
set resp.http.Retry-After = "";
synthetic( {"<!DOCTYPE html>
<html>
<head>
<title>"} + resp.status + " " + resp.reason + {"</title>
</head>
<body>
<h1>Error "} + resp.status + " " + resp.reason + {"</h1>
<p>"} + resp.reason + {"</p>
<h3>Guru Meditation:</h3>
<p>XID: "} + req.xid + {"</p>
<hr>
<p>Varnish cache server</p>
</body>
</html>
"} );
return (deliver);
} sub vcl_backend_error {
set beresp.http.Content-Type = "text/html; charset=utf-8";
set beresp.http.Retry-After = "";
synthetic( {"<!DOCTYPE html>
<html>
<head>
<title>"} + beresp.status + " " + beresp.reason + {"</title>
</head>
<body>
<h1>Error "} + beresp.status + " " + beresp.reason + {"</h1>
<p>"} + beresp.reason + {"</p>
<h3>Guru Meditation:</h3>
<p>XID: "} + bereq.xid + {"</p>
<hr>
<p>Varnish cache server</p>
</body>
</html>
"} );
return (deliver);
} sub vcl_fini {
return (ok);
}
上一篇:MongoDB文档的增删改操作


下一篇:sql数据库语言练习,增删改查