yum安装varnish

三台虚拟机都要做
systemctl stop firewalld      //关闭防火墙
setenforce 0         //关闭监控
80.101
yum install -y httpd
vi /etc/httpd/conf/httpd.conf
找到ServerName www.example.com:80吧#去掉
vi /var/www/html/index.html
<h1>server 1</h1>
systemctl start httpd
80.102
yum install -y httpd
vi /etc/httpd/conf/httpd.conf
找到ServerName www.example.com:80吧#去掉
vi /var/www/html/index.html
<h1>server 2</h1>
systemctl start httpd
1.安装varnish(从Centos7开始,varnish已被收入到epel仓库)
cd /etc/yum.repos.d/
mv back/* ./
yum   install epel-release -y  //需要联网
yum -y install varnish

yum安装varnish

2.新建varnish用户
useradd -M -s /sbin/nologin varnish

 
`
3.varnish配置文件
/etc/varnish/varnish.params 主配置文件
/etc/varnish/default.vcl VCL配置文件

VCL
Varnish Configuration Language (VCL) 是一种动态语言,是varnish配置语言,用来描述请求处理和制定缓存策略。vcl配置内容由manager process 创建的VCC子进程转换成C语言代码,再经由gcc编译成共享对象,最后装载到cacher process中生效。
 
VCL文件被分为多个子程序,不同的子程序在不同的时间里执行,比如一个子程序在接到请求时执行,另一个子程序在接受到后端服务器传送的文件时执行。
 
 
VCL处理流程图`

 yum安装varnish
 
 

处理过程大致分为如下几个步骤
1、Receive状态:请求处理的入口状态,根据VCL规则判断该请求应该是Pass或Pipe或者进入Lookup(本地查询)
 
2、Lookup状态,在缓存中查找用户请求的对象,如果缓存中没有其请求的对象,后续操作很可能会将其请求的对象进行缓存;进入此状态后,会在hash表中查找数据,若找到,则进入Hit(命中)状态,否则进入miss状态
 
3、Pass状态,在此状态下,会进入后端(源服务器)请求,即进入fetch状态,不走缓存
 
4、Fetch状态,在Fetch状态下,对请求,进行后端的获取,发送请求,获得源服务器的数据,并进行本地的存储
 
5、Deliver提供状态,将获取到的数据发送给客户端,然后完成本次请求。
 
注:
Pass:绕过缓存,既不从缓存中查询内容或不将内容存储至缓存中;
 
Pipe:不对客户端进行检测或作出任何操作,而是在客户端与后端服务器之间建立专用“管道”,并直接将数据在二者之间进行传送;此时,keep-alive连接中后续传送的数据都将通过此管道进行直接传送,并不会出现在任何日志中。
 
 
语法
  (1)支持注释  // # /* */
  (2)不支持循环
  (3)sub $name:用于定义子例程
        sub vcl_recv {

        }
  (4)有众多内置的变量,变量的可调用位置与state engine有密切相关性
  (5)支持终止语句,return(action),没有返回值
  (6)"域"专用
  (7)操作符 =,==,!,&&,||
 
 
常用语句
 
  if     else
  set name=value  
  unset name
  req.http.HEADER:调用请求报文中http协议的指定的变量
  req.request:请求方法
 
varnish变量种类
req——请求
resp——响应
client——客户端
server——服务端
bereq——向后端请求时产生的req
beresp——后端响应时产生的resp
obj——项目对象
storage——大小
 
常用变量:
bereq和req:
bereq(req).http.HEADERS: 由varnish发往backend server的请求报文的指定首部;
bereq(req).request:请求方法;
bereq(req).url:   请求路径
bereq(req).proto: 请求协议
bereq(req).backend:指明要调用的后端主机;
 
beresp和resp
                beresp.proto:响应使用的协议
                beresp.status:响应的状态码
                beresp.reason:原因短语;
                beresp.backend.ip:响应的后端ip地址
                beresp.backend.name:响应的后端域名
                beresp.http.HEADER: 从backend server响应的报文的首部;
                beresp.ttl:后端服务器响应的内容的余下的生存时
 
obj
                obj.ttl: 对象的ttl值;
                obj.hits:此对象从缓存中命中的次数;
 
server
                server.ip
                server.hostname
 
 
CDN

 
 

实战:使用varnish加速多个不同域名站点的web服务器
 
varnish:192.168.80.100
web1:192.168.80.101——www.aa.com
web2:192.168.80.102——www.bb.com

 
 
 
 

vi /etc/varnish/default.vcl
backend web1 {
    .host = "192.168.80.101";
    .port = "80";
}

backend web2 {
    .host = "192.168.80.102";
    .port = "80";
}

sub  vcl_recv{
if  (req.http.host ~ "(?i)^(www.)?aa.com$") {
set req.http.host = "www.aa.com";
set req.backend_hint = web1;
} elsif (req.http.host ~ "(?i)^www.bb.com$") {
set req.backend_hint = web2;
return(hash);
}
}
判断当访问www.aa.com域名时从web1上取数据,访问www.bb.com域名是到web2取数据。

yum安装varnish

#添加一个Header标识,以判断缓存是否命中
sub vcl_deliver {
    if (obj.hits > 0) {
                set resp.http.X-Cache = "HIT  FROM"  + req.http.host;
set  resp.http.X-Cache-Hits = obj.hits;
        } else {
                set resp.http.X-Cache = "MISS  FROM"  + req.http.host;
        }
    return (deliver);
}

yum安装varnish

systemctl restart varnish   //重启varnish
vi /etc/hosts
192.168.80.100   www.aa.com
192.168.80.100   www.bb.com

 
 yum安装varnish

yum install -y elinks
elinks  www.aa.com  --dump   #elinks文本界面浏览器

 yum安装varnish
注有问题改这里
vi varnish.params
yum安装varnish

猜你喜欢

转载自blog.51cto.com/14158288/2350295
今日推荐