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(开放授权)是一个正式的互联网标准协议. 允许第三方网站在用户 ...
随机推荐
- 添加已运行daemon进程(falcon-agent)到supervisor测试
falcon-agent now is running already, pid= falcon-agent now is running already, pid= falcon-agent now ...
- cve-2015-5122漏洞分析
HackTem爆出的第二枚0day,分析了下,做个记录. Poc中一开始会分配一个Array类型的_ar结构. 第一次赋值 此时在a[0 ] –a[1e-1] 处已被赋值为Vector.<uin ...
- C# Winform TreeView 的一些基本用法
下面是treeview的用法TreeView组件是由多个类来定义的,TreeView组件是由命名空间"System.Windows .Forms"中的"TreeView& ...
- Android之UI编程(一):线性布局
package com.example.fk_layout; import android.app.Activity; import android.os.Bundle; public class L ...
- [机器学习] ——KNN K-最邻近算法
KNN分类算法,是理论上比较成熟的方法,也是最简单的机器学习算法之一. 该方法的思路是:如果一个样本在特征空间中的k个最相似(即特征空间中最邻近)的样本中的大多数属于某一个类别,则该样本也属于这个类别 ...
- 有限状态机(FSM)
在游戏开发中,AI是个永恒不变的话题,如果你要的AI只是很简单的一个逻辑 那么有限状态机是一个很好的解决方案,尽管在实际开发中,AI的设计并不是一个简单的逻辑, 如果用有限状态机,维护起来会非常麻烦, ...
- [Android]关于Installation error: INSTALL_PARSE_FAILED_MANIFEST_MALFORMED ,修改包名
查了很多,说修改manifest.本来是没有任何的修改,自动生成的,最后发现参考了人家的一篇: http://bbs.csdn.net/topics/390613702 修改包名,包名带了大些的开头了 ...
- wordexpress
登陆数据库:mysql -uroot -p 创建数据库:CREATE DATABASE wordpress; 创建数据库用户:CREATE USER wordpress@localhost IDENT ...
- java 对象输入输出流
对象的输入输出流的作用: 用于写入对象 的信息读取对象的信息. 对象的持久化. 比如:用户信息. ObjectInputStream : 对象输入流 ...
- MySQL查询语句(select)详解(2)
7.子查询 当进行查询的时候,需要的条件是另外一个select语句的结果,这时候就要用到子查询 用于子查询的主要关键字有:in,not in,=,!=,exists,not exists等. 以下两张 ...