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);
}
varnish4.0 流程图以及说明的更多相关文章
- varnish4.0缓存代理配置
防伪码:你必须非常努力,才能看起来毫不费力. 一.varnish原理: 1)Varnish简介: varnish缓存是web应用加速器,同时也作为http反向缓存代理.你可以安装varnish在任何h ...
- 编译安装 varnish-4.1.2和yum 安装 varnish-4.0.3
vanish可以让用户自己选择缓存数据是存于内存还是硬盘,存于内存一般基于二八法则即常访问的数据是磁盘存储的总数据五分之一,因此内存也应该是硬盘文件大概五分之一.如果有多台vanish则,总内存满足即 ...
- varnish4.0简介
Varnish 4.0 简介 Varnish 是一款开源的HTTP加速器和反向代理服务器,它的主要特点有: (1)是基于内存缓存,重启后数据将消失.(2)利用虚拟内存方式,io性能好.(3)支持设置0 ...
- OAuth2.0流程图
OAuth2.0是用户验证和授权标准
- Varnish 4.0 实战(转)
简介 Varnish 是一款高性能且开源的反向代理服务器和 HTTP 加速器,其采用全新的软件体系机构,和现在的硬件体系紧密配合,与传统的 squid 相比,varnish 具有性能更高.速度更快.管 ...
- varnish 4.0编译安装小记
varnish 4.0 编译问题 centos-6.5 x86环境 装varnish遇到几个错误要先安装python-docutils然后提示error1,于是安装:libedit-devel然后提示 ...
- Activiti工作流学习-----基于5.19.0版本(7)
八.BPMN 2.0流程图详解 BPMN 2.0的标准的出现是好事,用户不在被某个工作流开发商绑架或者在工作流中开发妥协,Activiti作为BPMN标准的一套解决方案,使得用户在选择工作流框架时可以 ...
- Varnish 4.0
Varnish 4.0 实战 简介 Varnish 是一款高性能且开源的反向代理服务器和 HTTP 加速器,其采用全新的软件体系机构,和现在的硬件体系紧密配合,与传统的 squid 相比,varn ...
- OAuth2.0的原理介绍
OAuth2.0是一个关于授权(authorization)的开放网络标准,在全世界得到广泛应用,目前的版本是2.0版. OAuth2.0(开放授权)是一个正式的互联网标准协议. 允许第三方网站在用户 ...
随机推荐
- Gamma校正与线性空间
基础知识部分 为了方便理解,首先会对(Luminance)的相关概念做一个简单介绍.如果已经了解就跳到后面吧. 我们用Radiant energy(辐射能量)来描述光照的能量,单位是焦耳(J),因为光 ...
- 移动APP项目研发流程及版本规划(转)
一个移动APP项目研发规模可大可小,但都离不开以下几个成员:产品经理.ui设计师.前端开发.后端开发.测试等.如何合理安排项目成员工作.确保项目顺利进行呢?一个清晰合理的项目研发流程控制很重要. 项目 ...
- JS中对象排序
详细代码如下: var s=[{name:"abc",value:10},{name:"dbc",value:5},{name:"acc", ...
- 关于php的一些小知识!
浏览目录: 一.PHP的背景和优势: 二.PHP原理简介: 三.PHP运行环境配置: 四.编写简单的PHP代码以及测试. 一.PHP的背景和优势 1.1 什么是PHP? PHP是能让你生成动态 ...
- 一步一步打造自己的Android图片浏览器(原创)
今天我们试着来制作一个自己的Android图片浏览器. 图片浏览器应该具有什么功能呢?鉴于不同的人不同的理解,这里提出一个基本的需求: 搜索手机内的所有图片,展示于一个列表中: 列表中展示的是图片的缩 ...
- caffe中权值初始化方法
首先说明:在caffe/include/caffe中的 filer.hpp文件中有它的源文件,如果想看,可以看看哦,反正我是不想看,代码细节吧,现在不想知道太多,有个宏观的idea就可以啦,如果想看代 ...
- 【转】有关Oracle随机字符串的生成方法及具体应用
Oracle生成随机字符串的方法是通过dbms_random.string实现的. 1.dbms_random.string用法Oracle官方文档参考链接:http://download.oracl ...
- Python for Informatics 第11章 正则表达式五(译)
注:文章原文为Dr. Charles Severance 的 <Python for Informatics>.文中代码用3.4版改写,并在本机测试通过. 11.4 转义字符 之前我们在正 ...
- CSV格式数据如何导入SqlServer?
一.使用微软数据库IDE管理软件:Microsoft SQL Server Management Studio 1.标准的CSV文件格式如下: 2.建数据表 3.在需要导入的数据库右键点击“任务”,选 ...
- Mybatis拦截器 mysql load data local 内存流处理
Mybatis 拦截器不做解释了,用过的基本都知道,这里用load data local主要是应对大批量数据的处理,提高性能,也支持事务回滚,且不影响其他的DML操作,当然这个操作不要涉及到当前所lo ...