varnish如何存储缓存对象:

file: 单个文件;不支持持久机制;

malloc: 缓存在内存中;

persistent:基于文件的持久存储;(此方式不建议使用)

vcl:配置缓存系统的缓存机制;【线程中缓存功能的工作机制】

一、在vs2和vs3上安装http

写入文件,内容一个为on vs2,另一个为on vs3

[root@vs2 ~]# yum install http
[root@vs2 ~]# for i in {1..10}; do echo "web$i on vs2" > /var/www/html/test$i.html; done
[root@vs2 ~]# ls /var/www/html/
test10.html  test2.html  test4.html  test6.html  test8.html
test1.html   test3.html  test5.html  test7.html  test9.html
[root@vs2 ~]# systemctl start httpd.service

二、安装varnish(在centos7上安装4.0.3版本)

[root@vs1 ~]# yum install varnish

三、varnish主程序的配置文件

[root@vs1 ~]# vim /etc/varnish/varnish.params # Varnish environment configuration description. This was derived from
# the old style sysconfig/defaults settings# Set this to 1 to make systemd reload try to switch vcl without restart.
RELOAD_VCL=1# Main configuration file. You probably want to change it.
VARNISH_VCL_CONF=/etc/varnish/default.vcl    #读取vcl配置文件的位置# Default address and port to bind to. Blank address means all IPv4
# and IPv6 interfaces, otherwise specify a host name, an IPv4 dotted
# quad, or an IPv6 address in brackets.
# VARNISH_LISTEN_ADDRESS=192.168.1.5
VARNISH_LISTEN_PORT=6081    #监听的服务端口为6081# Admin interface listen address and port
VARNISH_ADMIN_LISTEN_ADDRESS=127.0.0.1    #监听的管理地址为本机
VARNISH_ADMIN_LISTEN_PORT=6082    #监听的管理端口为6082# Shared secret file for admin interface
VARNISH_SECRET_FILE=/etc/varnish/secret    #密钥文件位置# Backend storage specification, see Storage Types in the varnishd(5)
# man page for details.
VARNISH_STORAGE="file,/var/lib/varnish/varnish_storage.bin,1G"    #缓存以文件的方式的存储位置和大小
#VARNISH_STORAGE="malooc,256M"    #以内存方式缓存,缓存大小为256M# Default TTL used when the backend does not specify one
VARNISH_TTL=120    #联系后端服务器超时时长# User and group for the varnishd worker processes
VARNISH_USER=varnish    #主进程所使用的用户
VARNISH_GROUP=varnish    #主进程所使用的组# Other options, see the man page varnishd(1)    #进程选项,线程池的最大值最小值和超时时长
DAEMON_OPTS="-p thread_pool_min=5 -p thread_pool_max=500 -p thread_pool_timeout=300"

四、varnish的命令行管理工具

[root@vs1 ~]# varnishadm -S /etc/varnish/secret -T 127.0.0.1:6082
help
help [<command>]    #获取帮助信息
ping [<timestamp>]    #测试服务器是否正常
auth <response>    #
quit                #退出
banner
status            #显示服务器状态信息
start            #启动子进程
stop            #停止子进程
vcl.load <configname> <filename>    #载入哪个文件为配置文件
vcl.inline <configname> <quoted_VCLstring>
vcl.use <configname>                #使用哪个vcl文件
vcl.discard <configname>            #删除哪个vcl文件
vcl.list                     #列出所有可用的vcl文件
param.show [-l] [<param>]            #显示运行时参数
param.set <param> <value>
panic.show                     #显示恐慌信息,显示进程或子进程上次挂掉的原因
panic.clear                                        #清除恐慌信息
storage.list                                        #显示缓存信息
vcl.show [-v] <configname>                        #显示vcl文件的详细信息,vcl编译前的样子
backend.list [<backend_expression>]                #显示后端服务器列表
backend.set_health <backend_expression> <state>    #手动上线下线后端服务器
ban <field> <operator> <arg> [&& <field> <oper> <arg>]...    #清理缓存中的缓存对象
ban.list                                             #显示定义的清理缓存规则

varnish的访问日志

[root@vs1 ~]# varnishlog
[root@vs1 ~]# varnishtop

varnish的统计信息

[root@vs1 ~]# varnishstat

五、vcl配置文件的说明

http://book.varnish-software.com/4.0/chapters/VCL_Basics.html

http://book.varnish-software.com/4.0/_p_w_picpaths/simplified_fsm.svg

vcl_recv    接收请求
cacheable    判断是否为可缓存对象
incache        判断hash后的结果是否存在
vcl_hash    可缓存对象hash计算
vcl_hit    缓存中命中
vcl_miss    缓存中未命中
vcl_fetch    获取后端内容
vcl_deliver    构建缓存发送
vcl_pipe        客户端请求的方法不是常见方法时,直接交给后端服务器处理
vcl_pass        不检查缓存直接从后端服务器取
vcl_error    varnish直接返回错误响应

六、vcl配置文件

[root@vs1 ~]# cp /etc/varnish/default.vcl /etc/varnish/test.vcl
[root@vs1 ~]# vim /etc/varnish/test.vcl
# 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;# Default backend definition. Set this to point to your content server.
backend vs2 {    #定义后端主机vs2.host = "172.16.24.102";.port = "80";.probe = {    #对后端主机的test1进行进行健康状态检测.url = "/test1.html";}
}
backend vs3 {.host = "172.16.24.104";.port = "80";.probe = {.url = "/test1.html";}
}
#import directors;    #加载directors模块,在负载均衡轮询时要用到
#sub vcl_init {        #轮询方式的负载均衡
#    new mycluster = directors.round_robin();
#    mycluster.add_backend(vs2);
#    mycluster.add_backend(vs3);
#}sub vcl_recv {# Happens before we check if we have this in cache already.## Typically you clean up the request here, removing cookies you don't need,# rewriting the request, etc.#url中开头带有login或者admin的直接从后端主机取结果不缓存if (req.url ~ "(?i)^/login" || req.url ~ "(?i)^/admin") {  return(pass);}#url以jpg,png,gif结尾的直接发给vs2,其他的都发给vs3if (req.url ~ "(?i)\.(jpg|png|gif)$") {set req.backend_hint = vs2;} else {set req.backend_hint = vs3;}#    set req.backend_hint = mycluster.backend();    #负载均衡集群#如果客户端请求为PRI返回405,如果请求的为非GET,HEAD,PUT,POST,TRACE,OPTIONS,DELETE都直接发给后端主机处理if (req.method == "PRI") {/* We do not support SPDY or HTTP/2.0 */return (synth(405));}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 {# Happens after we have read the response headers from the backend.## Here you clean the response headers, removing silly Set-Cookie headers# and other mistakes your backend does.
}sub vcl_deliver {# Happens when we have all the pieces we need, and are about to send the# response to the client.## You can do accounting or modifying the final object here.#如果缓存能命中就在返回值中插入HIT,未命中则插入MISSif (obj.hits>0) {set resp.http.X-Cache = "HIT";} else {set resp.http.X-Cache = "MISS";}
}[root@vs1 ~]# varnishadm -S /etc/varnish/secret -T 127.0.0.1:6082
vcl.load test2 test.vcl
200
VCL compiled.
vcl.use test2
200
VCL 'test2' now active

七、测试

在vs2上上传一张dog.jpg,在vs3上不上传任何图片

转载于:https://blog.51cto.com/xsllqs/1791225

利用varnish构建httpd缓存服务器相关推荐

  1. RHEL4- DNS服务(六)构建DNS缓存服务器

    RHEL4- DNS服务(六)构建DNS缓存服务器   如果要构建DNS缓存服务器其实不是很复杂,只要安装一个系统自带的软件包即可.其实这个软件包,我们在<RHEL4- DNS服务(一)bind ...

  2. DNS服务器构建(一)-—构建域名缓存服务器

    构建域名缓存服务器 大家都知道,平时我们访问某个网站输入的一般都是字符串(域名)如:www....com等等,但是在互联网中标识不同的主机的并不是这些字符串,而是主机具体的IP地址,所以我们其实是通过 ...

  3. varnish缓存服务器构建疑问

    标题索引 追朔原因 实验分解 抓包分析 追朔原因[此文需要待续] 当下是互联网时代也是CDN缓存时代,缓存可以提供互联网80%流量,因此缓存的构建和提高缓存的命中率是互联网行业必不可少的方式和手段,另 ...

  4. 如何利用Linux构建免费的缓存DNS服务器

    如何利用Linux构建免费的缓存DNS服务器   实验背景:       小诺公司目前的网络环境是所有用户都可以上外网,而且使用的DNS是通过DHCP服务器获取得到的,DHCP服务器上填写的DNS地址 ...

  5. 利用varnish做Discuz论坛的缓存服务器

    实验背景:公司有一台BBS服务器,用的是LNMP的架构搭建的.正好手头有一台空闲的虚拟机,于是想着给BBS前端加一台缓存服务器.于是选定了varnish,搜了很多教程,跌跌撞撞的完成了配置.这其中很多 ...

  6. 高性能缓存服务器Varnish详解

    一.简介 Varnish是一款高性能的开源HTTP加速器,挪威最大的在线报纸 Verdens Gang 使用3台Varnish代替了原来的12台Squid,性能比以前更好. Varnish 的作者Po ...

  7. nginx反向代理缓存服务器构建

    博主QQ:819594300 博客地址:http://zpf666.blog.51cto.com/ 有什么疑问的朋友可以联系博主,博主会帮你们解答,谢谢支持! 代理服务可简单的分为正向代理和反向代理: ...

  8. 【推荐】如何用 Nginx 构建反向代理缓存服务器?

    作者:一盏烛光,贤牛特邀工程师. 防伪码:曾经沧海难为水,除却巫山不是云. 代理服务可简单的分为正向代理和反向代理: 正向代理: 用于代理内部网络对 Internet 的连接请求(如×××/NAT), ...

  9. 高性能缓存服务器Varnish架构配置

    Varnish跟Squid都是一款内容加速缓存服务器,我们可以使用它们来对我们的网页内容进行缓存,以此来从某个方面提高用户体验度,提升网站整体的抗压能力. 目前自建的CDN中,有很多都是基于Squid ...

最新文章

  1. python读取只读word只读_Python用于NLP :处理文本和PDF文件
  2. mongodb启动不能锁定_使用MongoDB进行乐观锁定重试
  3. 中科大计算机学院博士导师,中科大计算机学院招生导师
  4. C# 判断一个字符串是否为url
  5. 「动手学深度学习」在B站火到没谁,加这个免费实操平台,妥妥天花板
  6. jQuery 2.0.3 源码分析Sizzle引擎 - 编译函数(大篇幅)
  7. 解决“ValueError: Stop argument for islice() must be None or an integer: 0 <= x <= sys.maxsize.”
  8. T61 拆机4短报警
  9. Zookeeper下载与安装教程(for windows)
  10. css全局加粗,CSS 实现矩形四个边角加粗的方法
  11. 氨基酸在php的溶液中,氨基酸等电点的计算和应用.ppt
  12. 如何做专利挖掘,关键是寻找专利点,其实并不太难
  13. python入门自学软件手机版,python编程教学app
  14. Vue(狂神学习笔记)2021-10-8
  15. 用轻量服务器搭建自己的pdf在线工具箱(支持pdf压缩以及pdf OCR)
  16. 图像处理基本库的学习笔记5--公共数据集,PASCAL VOC数据集,NYUD V2数据集的简介与提取,COCO2017,医学影像数据集汇总
  17. 数字化助力生产制造管理:专项生产管理系统
  18. ubuntu16.04 basler相机 图像采集卡设置
  19. CAD室内设计构思怎么写?
  20. FAILED org.spark_project.jetty.server.Server@8a6631b: java.net.BindException: Address already in use

热门文章

  1. 新款iPhone现已曝光,跟风华为“浴霸三摄”,没有5G版本
  2. 5亿美元续命!Uber自动驾驶存亡之秋喜获丰田投资
  3. 马斯克又有新麻烦上身!前员工将他告到美国证监会,曝出一大堆惊天八卦
  4. 欧空局2018机器学习系列课程发布:从概念到实践(视频+PPT)
  5. 无人驾驶货运再添新玩家:G7控股,腾讯系,主攻L4
  6. 讣告 | ACL终身成就奖得主Aravind Joshi去世
  7. 安装虚拟机时遇到的问题
  8. Python实现鸢尾花数据集分类问题——基于skearn的LogisticRegression
  9. 201621123053《Java程序设计》第十一周学习笔记文章
  10. 第一周(7.11)作业——1、自我介绍;2、决心书