varnish 缓存多域名

实例解析

#
# 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";
  }
}
时间: 2024-07-28 19:49:54

varnish 缓存多域名的相关文章

Varnish缓存代理简介与配置

一.varnish原理: 1)Varnish简介: varnish缓存是web应用加速器,同时也作为http反向缓存代理.你可以安装varnish在任何http的前端,同时配置它缓存内容.与传统的 squid 相比,varnish 具有性能更高.速度更快.管理更加方便等诸多优点.有一部分企业已经在生产环境中使用其作为旧版本的squid的替代方案,以在相同的服务器成本下提供更好的缓存效果,Varnish更是作为CDN缓存服务器的可选服务之一. 根据官网的介绍,Varnish的主要特性如下:http

varnish缓存代理的安装

二.安装varnish 1.安装依赖关系的软件包(注:使用centos在线yum源) [[email protected] ~]# yum -y install autoconfautomake libedit-devel libtool ncurses-devel pcre-devel pkgconfigpython-docutils python-sphinx 2.安装varnish Varnish的官方网址为http://varnish-cache.org,可以在这里下载最新版本的软件.

linux之搭建varnish缓存代理服务器

一.varnish原理: 1)Varnish简介: varnish缓存是web应用加速器,同时也作为http反向缓存代理.你可以安装varnish在任何http的前端,同时配置它缓存内容.与传统的 squid 相比,varnish 具有性能更高.速度更快.管理更加方便等诸多优点.有一部分企业已经在生产环境中使用其作为旧版本的squid的替代方案,以在相同的服务器成本下提供更好的缓存效果,Varnish更是作为CDN缓存服务器的可选服务之一. 根据官网的介绍,Varnish的主要特性如下:http

Web架构:varnish缓存代理服务器超详细剖析

小生博客:http://xsboke.blog.51cto.com 小生 Q Q:1770058260 -------谢谢您的参考,如有疑问,欢迎交流 目录 varnishi简介 varnish配置组成 --------------------------------------  vcl内置预设变量 --------------------------------------  功能语句与对象 --------------------------------------  varnish中内置

centos 7.5 部署varnish缓存服务器

博文大纲: 一.varnish简介 二.Varnish 如何工作 三.部署varnish缓存服务器 一.varnish简介 Varnish是高性能开源的反向代理服务器和HTTP缓存服务器,其功能与Squid服务器相似,都可以用来做HTTP缓存.可以安装 varnish 在任何web前端,同时配置它缓存内容.与传统的 squid 相比,varnish 具有性能更高.速度更快.管理更加方便等诸多优点.有一部分企业已经在生产环境中使用其作为旧版本的 squid的替代方案,以在相同的服务器成本下提供更好

基于Centos 7 部署Varnish缓存代理服务器

博文结构Varnish的概述与工作原理等等安装Varnish缓存代理服务器 一.Varnish概述 1.Varnish 简介 Varnish是一款高性能且开源的反向代理服务器和HTTP加速器,其采用全新的软件体系机构,和现在的硬件体系紧密配合.与传统的squid相比,Varnish具有高性能.速度快.管理更加方便等优点,目前很多大型的网站都开始尝试使用Varnish来代替squid,这便是Varnish迅速发展的最根本的原因. Varnish的主要特征: (1)缓存代理位置:可以使用内存也可以使

部署varnish缓存服务器

一.简介Varnish是高性能开源的反向代理服务器和HTTP缓存服务器,其功能与Squid服务器相似,都可以用来做HTTP缓存.可以安装 varnish 在任何web前端,同时配置它缓存内容.与传统的 squid 相比,varnish 具有性能更高.速度更快.管理更加方便等诸多优点.有一部分企业已经在生产环境中使用其作为旧版本的 squid的替代方案,以在相同的服务器成本下提供更好的缓存效果,Varnish 更是作为 CDN 缓存服务器的可选服务之一. varnish的主要特性如下: ? 缓存位

Varnish缓存机制详细介绍及简单配置

Varnish是一款高性能的开源HTTP加速器,其主要用来做为反向代理中的缓存服务器使用,但其实Varnish本身也是具有反向代理功能的,但在创建连接和维持连接上,与Nginx相比差距很大,现在有一个很流行的架构就是前端用Nginx作为反向代理,后面加Varnish缓存服务器为Web服务加速 在将Varnish前先谈谈我们的浏览器缓存机制,现在的浏览器基本都具有缓存功能,它能将我们以前访问过的静态内容和可进行缓存的动态内容缓存再本地,而后在下次访问相同资源时,如果可以确认Server端的资源未发

varnish缓存服务

缓存的基础知识1.程序本身具有局部性时间局部性 过去访问到的数据,也有可能被两次访问 空间局部性 一个数据被访问到时,离它最近的文件可能马上也会被访问 2.命中率文档命中率 从文档个数进行衡量 字节命中率 从内容大小进行衡量 3.缓存系统的特性缓存对象 有生命周期,且是定期清理的 缓存空间耗尽 使用LRU(最近最少使用算法)或者MRU算法进行缓存项清理 不可缓存项 用户私有数据 4.缓存系统一般处理步骤接收请求解析请求 提取请求的URL及各种首部 查询缓存新鲜度检测创建响应报文发送响应报文记录日