码迷,mamicode.com
首页 > 其他好文 > 详细

varnish 缓存多域名

时间:2016-09-25 11:56:23      阅读:221      评论:0      收藏:0      [点我收藏+]

标签:

实例解析

#
# This is an example VCL file for Varnish.
#
# It does not do anything by default, delegating control to the
# builtin VCL. The builtin VCL is called when there is no explicit
# return statement.
#
# See the VCL chapters in the Users Guide at https://www.varnish-cache.org/docs/
# and http://varnish-cache.org/trac/wiki/VCLExamples for more examples.
# Marker to tell the VCL compiler that this VCL has been adapted to the
# new 4.0 format.
vcl 4.0;
import directors;
probe backend_healthcheck { # 创建健康监测
  .url = /health.html;
  .window = 5;
  .threshold = 2;
  .interval = 3s;
}
backend web1 {	# 创建后端主机
  .host = "static1.lnmmp.com";
  .port = "80";
  .probe = backend_healthcheck;
}
backend web2 {
  .host = "static2.lnmmp.com";
  .port = "80";
  .probe = backend_healthcheck;
}
backend img1 {
  .host = "img1.lnmmp.com";
  .port = "80";
  .probe = backend_healthcheck;
}
backend img2 {
  .host = "img2.lnmmp.com";
  .port = "80";
  .probe = backend_healthcheck;
}
vcl_init {	# 创建后端主机组,即directors
  new web_cluster = directors.random();
  web_cluster.add_backend(web1);
  web_cluster.add_backend(web2);
  new img_cluster = directors.random();
  img_cluster.add_backend(img1);
  img_cluster.add_backend(img2);
}
acl purgers {	# 定义可访问来源IP
    "127.0.0.1";
    "192.168.0.0"/24;
}
sub vcl_recv {
  if (req.request == "GET" && req.http.cookie) {	# 带cookie首部的GET请求也缓存
    return(hash);
}
  if (req.url ~ "test.html") {	# test.html文件禁止缓存
    return(pass);
  }
  if (req.request == "PURGE") {	# PURGE请求的处理
    if (!client.ip ~ purgers) {
      return(synth(405,"Method not allowed"));
    }
    return(hash);
  }
  if (req.http.X-Forward-For) {	# 为发往后端主机的请求添加X-Forward-For首部
    set req.http.X-Forward-For = req.http.X-Forward-For + "," + client.ip;
  } else {
    set req.http.X-Forward-For = client.ip;
  }
  if (req.http.host ~ "(?i)^(www.)?lnmmp.com$") {	# 根据不同的访问域名,分发至不同的后端主机组
    set req.http.host = "www.lnmmp.com";
    set req.backend_hint = web_cluster.backend();
    } elsif (req.http.host ~ "(?i)^images.lnmmp.com$") {
      set req.backend_hint = img_cluster.backend();
    }
    return(hash);
  }
sub vcl_hit { # PURGE请求的处理
  if (req.request == "PURGE") {  
    purge;
    return(synth(200,"Purged"));
  }
}
sub vcl_miss {	# PURGE请求的处理
  if (req.request == "PURGE") {
    purge;
    return(synth(404,"Not in cache"));
  }
}
sub vcl_pass {	# PURGE请求的处理
  if (req.request == "PURGE") {
    return(synth(502,"PURGE on a passed object"));
  }
}
sub vcl_backend_response { # 自定义缓存文件的缓存时长,即TTL值
  if (req.url ~ "\.(jpg|jpeg|gif|png)$") {
    set beresp.ttl = 7200s;
  }
  if (req.url ~ "\.(html|css|js)$") {
    set beresp.ttl = 1200s;
  }
  if (beresp.http.Set-Cookie) { # 定义带Set-Cookie首部的后端响应不缓存,直接返回给客户端
    return(deliver);
  }
}
sub vcl_deliver {
  if (obj.hits > 0) {	# 为响应添加X-Cache首部,显示缓存是否命中
    set resp.http.X-Cache = "HIT from " + server.ip;
  } else {
    set resp.http.X-Cache = "MISS";
  }
}

varnish 缓存多域名

标签:

原文地址:http://www.cnblogs.com/fengjian2016/p/5905584.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!