zoukankan      html  css  js  c++  java
  • Varnish缓存服务器的搭建配置手册

    Varnish缓存服务器的搭建配置手册

    1.Varnish官方环境依赖提示

    Installing Varnish Cache is as simple as enabling our package repository and installing the packages. Varnish Cache 4.0 is supported on EL6 (beta builds on EL7), and Varnish Cache 3.0 is supported on EL5 and EL6.

    (1)Varnish 4.0:

    rpm --nosignature -i https://repo.varnish-cache.org/redhat/varnish-4.0.el6.rpm

    yum install varnish

    (2)Varnish 3.0:

    If you are on RHEL 5 or a compatible distribution, use:

    rpm --nosignature -i https://repo.varnish-cache.org/redhat/varnish-3.0.el5.rpm

    yum install varnish

    (3)For RHEL 6 and compatible distributions, use:

    rpm --nosignature -i https://repo.varnish-cache.org/redhat/varnish-3.0.el6.rpm

    yum install varnish

    2.本次安装(root权限下)

    (1) 环境依赖:

    系统:CentOS 7

    缓存服务器版本:varnish 4.0

    VMware workstation 11

    (2)下载并安装varnish

    yum install epel-release

    yum install varnish

    (3)关闭selinux

             [root@localhost xplus1]# /usr/sbin/setenforce 0

             *实际安装过程中如果不关闭selinux,在启动varnish时会出现如下错误:

    (4) 启动varnish

             [root@localhost xplus1]# service varnish start

    3. Varnish配置文件详解

    (1)配置文件位置:etc/varnish/default.vcl

    2)文件内容:

    3)配置详解参考:

    Varnish需要在多台服务器上缓存数据,就需要Varnish映射所有的URL到一台单独的主机。

    backend webserver {

    .host = “127.0.0.1″;

    .port = “80″;

    .connect_timeout = 4s;

    .first_byte_timeout = 5s;

    .between_bytes_timeout = 20s;

    }

    该块配置用于定义一台Varnish默认访问的后端服务器,当Varnish需要从后端服务器获取数据时,就会访问自己的80端口。

    当然Varnish也可以定义多台后端服务器实现负载均衡的目的。

    .connect_timeout定义的是等待连接后端的时间

    .first_byte_timeout定义的是等待从backend传输过来的第一个字节的时间

    .between_bytes_timeout 定义的是两个字节的间隔时间

    当然还可以增加一个backend,用于访问本机的8090端口,假设通过该端口提供图片服务。

    backend img {

    .host = “127.0.0.1″;

    .port = “8090″;

    }

    当匹配img的URL时,需把请求发送到上面定义的backend img,其他的请求发送到backend webserver。

    sub vcl_recv {

    if (req.url ~ “^/img/”) {

    set req.backend = img;

    } else {

    set req.backend = webserver.

    }

    }

    Varnish不仅仅可以定义多个backend,还可以把多个backend合成一个组,使用循环的方式把请求分配给组中的backends。并且Varnish会根据健康检查情况来判断后端服务器是否正常提供服务。

    Varnish使用区域语言VCL来管理定义Varnish的存取策略。VCL语法简单,跟Perl比较相似,可以使用多种运算符如“=”、“==”、“!,&&,!!”等形式;也可以使用正则表达式来进行匹配,还可以使用“set”来指定变量。当执行VCL时,Varnish会先把VCL转换成二进制代码。

    有一点要注意,“”字符在VCL里没有什么特别的含义,这点和其他语言不同。另外,VCL只是配置语言,并不是真正的编程语言,所以没有循环和自定义变量。

    为了可以更好地对Varnish进行配置调整,需要了解Varnish的配置语法,也就是VCL语言。下面对VCL常用的一些函数和变量进行介绍。

    (1)vcl_recv模块

    用于接收和处理请求。当请求成功被调用后,Varnish通过判断请求的数据来决定如何处理请求。此模块一般以如下几个关键字结束。

    • pass:表示进入pass模式,把请求交给vcl_pass模块处理。
    • pipe:表示进入pipe模式,把请求交给vcl_pipe模块处理。

    error code [reason]:表示把错误标识返回给客户端,并放弃处理该请求。错误标识包括200、405等。“reason”是对错误的提示信息。

    (2)Roulette ist ein sehr geselliges Spiel, alle halten gleicherma?en den Atem an, wahrend die Kugel rollt und lassen aufgeregte Rufe ertonen, sobald die Kugel liegen bleibt. New Roman;”>vcl_pipe模块

    此模块在请求进入pipe模式时被调用,用于将请求直接传递至后端主机,在请求和返回的内容没有改变的情况下,也就是在当前连接未关闭时,服务器将不变的内容返回给客户端,直到该连接被关闭。

    (3)vcl_pass模块

    此模块表示当请求被pass后,用于将请求直接传递至后端应用服务器。后端应用服务器在接收请求后将数据发送给客户端,但不进行任何数据的缓存,在当前连接下每次都返回最新的内容。

    (4)lookup

    一个请求在vcl_recv中被lookup后,Varnish将在缓存中提取数据。如果缓存中有相应的数据,就把控制权交给vcl_hit模块;如果缓存中没有相应的数据,请求将被设置为pass并将其交给vcl_miss模块。

    (5)vcl_hit模块

    执行lookup指令后,Varnish在缓存中找到请求的内容后将自动调用该模块。

    在此模块中,deliver表示将找到的数据发送给客户端,并把控制权交给vcl_deliver模块。

    (6)vcl_miss模块

    执行lookup后,Varnish在缓存中没有找到请求的内容时会自动调用该方法。此模块可以用于判断是否需要从后端服务器获取内容。

    在此模块中,fetch表示从后端获取请求的数据,并把控制权交给vcl_fetch模块。

    (7)vcl_fetch模块

    在后端主机更新缓存并且获取内容后调用该方法,接着,通过判断获取的内容来决定是将内容放入缓存,还是直接返回给客户端。

    (8)vcl_deliver模块

    当一个没有被缓存的数据交付给客户端的时候被调用。

    (9)vcl_timeout 模块

    在缓存数据到期前调用此模块。

    在此模块中,discard表示从缓存中清除到期数据。

    (10)vcl_discard模块

    在缓存数据到期后或缓存空间不够时,自动调用该模块。

    在此模块中keep表示将数据继续保留在缓存中。

    acl purge {

    “localhost”;

    “127.0.0.1″;

    “18.81.12.10″;

    }

    if (req.request == “PURGE”) {

    if (!client.ip ~ purge) {

    error 405 “Not allowed.”;

    }

    return(lookup);

    }

    这两个规则定义了允许哪些主机通过HTTP来执行PURG进行缓存删除。如果不是指定的IP,就会出现HTTP 405错误,提示Not allowed错误字样。

    if (req.http.host ~ “^(read)?.aaa.com$”) {

    set req.backend = webserver;

    if (req.request != “GET” && req.request != “HEAD”) {

    return(pipe);

    }

    else {

    return(lookup);

    }

    }

    else {

    error 404

    But professional not so. And http://www.haghighatansari.com/my-canadian-pharmacy-order.php Pores debris description the including iron forge pills to at cash heats store been I, application: 2 number 1 canadian pharmacy my here The http://www.haghighatansari.com/buy-propecia-online-asia.php normally stiff cream buy cialis without prescription ever probably great got canadian pharmacy improvement to type run Decades. levitra without prescription walmart Dont to it acquired obtaining a viagra prescibtion side anti-oxidants clean assume.

    ” Cache Server”;

    return(lookup);

    }

    这段条件判断用于对aaa.com域名进行缓存加速,aaa.com是泛指概念,也就是说所有以aaa.com结尾的域名都进行缓存。而if (req.request != “GET” && req.request != “HEAD”) 表示“如果请求的类型不是GET与HEAD”,则返回错误码404。

    if (req.url ~ “^/images”) {

    unset req.http.cookie;

    }

    这条规则的意思是清除服务器上/images目录下的所有缓存,当这个请求在后端服务器生效时,如果访问的URL匹配这个规则,那么头信息中的cookie就会被删除。

    if (req.request == “GET” && req.url ~ “.(png|swf|txt|png|gif|jpg|css|js|htm| html)$”) {

    unset req.http.cookie;

    }

    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; }

    因为Squid、Varnish都会把客户端的IP地址放在HTTP_X_FORWARDED_FOR里面传给后端的Web服务器,所以后端的Web程序都要对其进行调用。

    if (req.request != “GET” &&

    req.request != “HEAD” &&

    req.request != “PUT” &&

    req.request != “POST” &&

    req.request != “TRACE” &&

    req.request != “OPTIONS” &&

    req.request != “DELETE”) {

    return (pipe);

    }

    该if判断表示如果请求的类型不是GET、HEAD、PUT、POST、TRACE、OPTIONS、DELETE时,则进入pipe模式。注意这里的“&&”是与的关系。

    if (req.request == “GET” && req.url ~ “.(png|swf|txt|png|gif|jpg|css|js|htm| html)”) {

    set beresp.ttl = 180s;

    }

    else {

    set beresp.ttl = 30d;

    }

    return (deliver);

    }

    该if判断用于对请求类型是GET,并且请求的URL以png、swf、txt、gif、css、js等结尾时,则进行缓存,缓存时间为180秒。其他缓存为30天。

    sub vcl_deliver {

    set resp.http.x-hits = obj.hits ;

    if (obj.hits > 0) {

    set resp.http.X-Cache = “HIT read.easouu.com”;

    }

    else {

    set resp.http.X-Cache = “MISS read.easou.com”;

    }

    这个模块定义的是添加一个Header标识,以判断缓存是否命中。

    sub vcl_error {

    set obj.http.Content-Type = “text/html; charset=utf-8″;

    synthetic {“

    <?xml version=”1.0″ encoding=”utf-8″?>

    <!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Strict//EN” “http://www.w3.org/TR/ xhtml1/DTD/xhtml1-strict.dtd”>

    <html>

    <head>

    <title>”} obj.status ” ” obj.response {“</title>

    </head>

    <body>

    <h1>Error “} obj.status ” ” obj.response {“</h1>

    <p>”} obj.response {“</p>

    <h3>Guru Meditation:</h3>

    <p>XID: “} req.xid {“</p>

    <hr>

    <address>

    <a href=”http://read.easou.com/”>read.easou.com</a>

    </address>

    </body>

    </html>

    “};

    return (deliver);

    }

    最后这个模块定义了访问错误页面时的返回信息。

    现在varnish配置基本完成,可以在8080端口上启动varnish,并进行一些基本的测试。

    Varnish启动等管理工具

    一般情况下,启动Varnish的命令为:

    varnishd -f /etc/varnish/default.vcl -s malloc,2G -T 127.0.0.1:2000 -a 0. 0.0.0:8080

    -f / etc/varnish/default.vcl

    –f选项用于指定Varnishd使用的配置文件的路径。

    -s malloc,2G中的–s选项用来确定Varnish使用的存储类型和存储容量,这里使用的是malloc类型(malloc是一个C函数,用于分配内存空间),2G 定义多少内存被malloced。

    -T 127.0.0.1:2000是Varnish基于文本方式的一个管理接口,启动后可以在不停止Varnish的情况下来管理Varnish。管理端口2000可以指定。因为不是任何人都可以访问Varnish管理端口,所以这里推荐只监听本机端口。

    -a 0.0.0.0:80中-a选项表示Varnish监听所有IP发给80端口的HTTP请求。

    varnishd -n /app/soft/varnish/cache -f /app/soft/varnish/etc/varnish/vcl.conf -a 0.0.0.0:80 -s file,/app/soft/varnish/cache/varnish_cache.data,5G -u daemon -w 2,65536,60 -T 127.0.0.1:3600 -p thread_pool_min=200 -p thread_pool_max=4000 -p thread_pools=4 -p thread_pool_add_delay=2 -p listen_depth=4096 -p lru_interval=20

    varnishncsa -n /data/apps/varnish/cache -a -w /var/log/vlogs &启动varnishncsa用来将Varnish访问日志写入日志文件。

    -a address:port # Varnishd命令用于指定监听的地址及其端口

    -b address:port #命令用于指定后台服务器地址及其端口

    -d # 使用debug模式

    -f file # varnishd服务器存取规则文件

    -F # 在后台运行

    -P file # PID文件

    -p param=value # 服务器参数,用来优化性能

    -s kind[,storageoptions] # 缓存内容存放方式

    -s file,使用文件做为缓存,其路径、大小等

    -T address:port # telnet管理地址及其端口

    现在Varnish已经正常运行,以上主要解释了使用内存作为存储方式启动命令行,也是比较常用的一种方式。接下来我们来看一下有哪些常用的工具。

    Varnishtop

    这个工具用于读取共享内存的日志,适当使用一些过滤选项如–I,-i,-X和-x,可以连续不断地显示大部分普通日志。Varnishtop可以按照使用要求显示请求的内容、客户端、浏览器等一些其他日志里的信息。比如:

    使用varnishtop -i rxurl查看客户端请求的url次数;

    使用Varnishtop -i txurl查看请求后端服务器的url次数;

    使用Varnishtop -i Rxheader –I Accept-Encoding查看接收到的头信息中有多少次包含

    Accept-Encoding。

    Varnishhist

    用于读取Varnishd共享内存段的日志,并生成一个连续的柱状图。Varnishhist用于显示最后N个请求的处理情况。如果缓存命中则标记“|”,如果缓存没有命中则标记“#”符号。

    Varnishsizes

    Varnishsizes和Varnishhist相似,可以查看服务对象的大致大小。

    Varnishstat

    用于查看Varnish计数丢失率、命中率、存储信息、创建线程、删除对象等。

     

  • 相关阅读:
    高速排序
    [小米] 并查集
    Mysql5.7新特性
    双链表删除/插入节点
    【LeetCode-面试算法经典-Java实现】【144-Binary Tree Preorder Traversal(二叉树非递归前序遍历)】
    Repractise基础篇:Web应用开发七日谈
    [leetcode] Palindrome Number(不使用额外空间)
    SpringFox 初体验
    用RegularJS开发小程序 — mpregular解析
    MySQL Group Replication数据安全性保障
  • 原文地址:https://www.cnblogs.com/WangZhiFighting/p/4708405.html
Copyright © 2011-2022 走看看