使用Varnish为网站加速

varnish概述

Varnish 的作者Poul-Henning Kamp是FreeBSD的内核开发者之一,他认为现在的计算机比起1975年已经复杂许多。在1975年时,储存媒介只有两种:内存与硬盘。但现在计算 机系统的内存除了主存外,还包括了cpu内的L1、L2,甚至有L3快取。硬盘上也有自己的快取装置,因此squid cache自行处理物件替换的架构不可能得知这些情况而做到最佳化,但操作系统可以得知这些情况,所以这部份的工作应该交给操作系统处理,这就是 Varnish cache设计架构。
监听端口号: 6081
官方网站:
https://www.varnish-cache.org/
下载:
http://varnish-cache.org/releases/index.html
在这里插入图片描述

varnish基础概念详解

1、比起squid更加轻量级,大致有以下几个特点:
2、可以基于内存缓存,也可以在磁盘上缓存
3、如果期望内存大小超过几十个G,比如图片服务器,纯粹使用内存,性能未必好,这时候可以使用磁盘进行缓存,或使用SSD X 2 做RAID1避免磁盘损坏,在实现随机访问上 ssd硬盘要比机械硬盘要好的多,如果必须要缓存在磁盘上还是建议使用ssd磁盘
4、可以利用虚拟内存方式,IO性能会非常好
5、支持设置0-60秒 精确缓存时间

支持VCL

其配置是通过vcl编程语言来完成的。 其配置需要先转换成C代码,所以使用vcl所写的配置,要先转换成C语言代码,因此要依赖于GCC 临时的编译vcl配置的,编译完之后才能运行起来
注: Varnish Configuration Language - VCL(varnish配置语言-VCL)。
Varnish使用区域配置语言,这种语言叫做“VCL”(varnish configuration language),在执行vcl时,varnish就把VCL转换成二进制代码。
VCL文件被分为多个子程序,不同的子程序在不同的时间里执行,比如一个子程序在接到请求时执行,另一个子程序在接收到后端服务器传送的文件时执行。

独特的日志存储及管理机制

日志既然保存在内存中,日志可以供多个应用程序所访问,所以一般查看命中率,当前请求有多少get post 方法等等,都需使用专用的工具才可以查看,比如varnishshtopvarnishlog 等命令工具用来查看日志信息

支持使用varnish状态引擎

通过巧妙的状态引擎的设计完成不同的引擎对用户的请求和缓存代理机制进行处理,用配置文件为状态引擎提供状态法则,完成缓存处理、完成代理处理等等

varnish 缓存原理

varnish缓存数据机制

在这里插入图片描述

VCL处理流程图

在这里插入图片描述
处理过程大致分为如下几个步骤:
Receive状态,也就是请求处理的入口状态,根据VCL规则判断该请求应该是Pass或Pipe,或者进入Lookup(本地查询)。
Lookup状态,在缓存中查找用户请求的对象,如果缓存中没有其请求的对象,后续操作很可能会将其请求的对象进行缓存;进入此状态后,会在hash表中查找数据,若找到,则进入Hit(命令中)状态,否则进入miss(没命中)状态。
Pass状态,在此状态下,会进入后端请求,即进入fetch取状态。
Fetch状态,在Fetch取状态下,对请求,进行后端的获取,发送请求,获得数据,并进行本地的存储。
Deliver 提供状态, 将获取到的数据发送给客户端,然后完成本次请求

注:
pass:绕过缓存,即不从缓存中查询内容或不将内容存储至缓存中;pipe:不对客户端进行检查或做出任何操作,而是在客户端与后端服务器之间建立专用“管道”,并直接将数据在二者之间进行传送;此时,keep-alive连接中后续传送的数据也都将通过此管道进行直接传送,并不会出现在任何日志中。
总结: 用户通过varnish加速时,有4线路可以获得数据。
在这里插入图片描述

安装varnish

1、安装varnish, 将上传varnish软件包
[root@yunzu63 ~]# rpm -ivh varnish-libs-4.0.5-1.el7.x86_64.rpm
[root@yunzu63 ~]# rpm -ivh jemalloc-3.6.0-1.el7.x86_64.rpm
[root@yunzu63 ~]# rpm -ivh varnish-3.0.6-1.el6.x86_64.rpm
vcl配置文件:
[root@yunzu63 ~]# ls /etc/varnish/default.vcl
varnish主配置文件:
[root@yunzu63 ~]# vim /etc/varnish/varnish.params
启varnish服务:
[root@yunzu63 ~]# systemctl start varnish

实战:缓存一个网站

实战:配置yunzu63成为 varnish服务器加速yunzu64 web服务器

配置yunzu63成为 varnish服务器

[root@yunzu63 ~]# vim /etc/varnish/default.vcl
###配置一个后端服务器
改:
16 backend default {
17 .host = “127.0.0.1”;
18 .port = “80”;
19 }
为:
backend web1 {
.host = “192.168.1.64”;
.port = “80”;
}

###查看缓存命中情况
在:
90 # sub vcl_deliver {
91 # }
追加:
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);
}
保存退出。

配置varnish服务端口

[root@yunzu63 ~]# vim /etc/varnish/varnish.params
改:
66 VARNISH_LISTEN_PORT=6081
为:
VARNISH_LISTEN_PORT=80

测试源站点:
在浏览器中访问:http://192.168.1.64/
测试加速
在浏览器中访问:http://192.168.1.63/

测试缓存命中:

扩展:curl命令
curl是通过url语法在命令行下上传或下载文件的工具软件,它支持http,https,ftp,ftps,telnet等多种协议,常被用来抓取网页和监控Web服务器状态。
参数:
-I 只取http响应头的信息,不取网页内容
例:
[root@yunzu64 src]# curl -I 192.168.1.64 #在yunzu63上查看yunzu64开启动的web服务器类型
HTTP/1.1 200 OK
Date: Tue, 23 Jan 2019 07:55:44 GMT
Server: Apache/2.4.6 (CentOS)
Last-Modified: Tue, 23 Jan 2018 07:54:01 GMT
ETag: “d-5636cd999c5e4”
Accept-Ranges: bytes
Content-Length: 13
Content-Type: text/html; charset=UTF-8

实例2:测试缓存命中

[root@yunzu63 ~]# curl -I 192.168.1.63
HTTP/1.1 200 OK
Date: Wed, 07 Feb 2019 14:00:49 GMT
Server: Apache/2.4.6 (CentOS)
Last-Modified: Wed, 07 Feb 2018 13:55:00 GMT
ETag: “d-5649fa431c0cb”
Content-Length: 13
Content-Type: text/html; charset=UTF-8
X-Varnish: 9
Age: 0
Via: 1.1 varnish-v4
X-Cache: MISS from 192.168.1.63 //表示没有命中
Connection: keep-alive

[root@yunzu63 ~]# curl -I 192.168.1.63
HTTP/1.1 200 OK
Date: Tue, 23 Jan 2018 07:54:50 GMT
Server: Apache/2.4.6 (CentOS)
Last-Modified: Tue, 23 Jan 2018 07:54:01 GMT
ETag: “d-5636cd999c5e4”
Content-Length: 13
Content-Type: text/html; charset=UTF-8
X-Varnish: 11 6
Age: 125
Via: 1.1 varnish-v4
X-Cache: HIT from 192.168.1.63 //表示命中
X-Cache-Hits: 11
Connection: keep-alive

使用varnish缓存多个网站

使用varnish加速多个不同域名站点的web服务器

配置yunzu64和yunzu62为web服务器
yunzu64之前已经配置成web服务器

配置yunzu62为web2服务器

[root@yunzu62 ~]# yum install httpd -y
[root@yunzu62 ~]# echo 192.168.1.62 > /var/www/html/index.html
[root@yunzu62 ~]# vim /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.1.62 yunzu62.cn
[root@yunzu62 ~]# systemctl start httpd

配置yunzu63 上的varnish服务器

[root@yunzu63 ~]# vim /etc/varnish/default.vcl
改:
backend web1 {
.host = “192.168.1.64”;
.port = “80”;
}
为:
backend web1 {
.host = “192.168.1.64”;
.port = “80”;
}
backend web2 {
.host = “192.168.1.62”;
.port = “80”;
}
#当访问www.yunzu.cn域名时从web1上取数据,访问bbs.yunzu.cn域名时到web2取数据
[root@yunzu63 ~]# vim /etc/varnish/default.vcl #在sub vcl_deliver 处定义以下内容:
sub vcl_recv {
if (req.http.host ~ “(?i)^(www.)?yunzu.cn " ) s e t r e q . h t t p . h o s t = " w w w . y u n z u . c n " ; s e t r e q . b a c k e n d h i n t = w e b 1 ; e l s i f ( r e q . h t t p . h o s t   " ( ? i ) b b s . y u n z u . c n ") { set req.http.host = "www.yunzu.cn"; set req.backend_hint = web1; } elsif (req.http.host ~ "(?i)^bbs.yunzu.cn ")setreq.http.host="www.yunzu.cn";setreq.backendhint=web1;elsif(req.http.host "(?i)bbs.yunzu.cn”) {
set req.backend_hint = web2;
return(hash);
}
}

重新加载varnish配置文件

[root@yunzu63 ~]# systemctl reload varnish

测试:网页访问 www.yunzu.cn 和 bbs.yunzu.cn则会显示不同的内容
在yunzu62上测试:

[root@yunzu62 ~]# vim /etc/hosts #添加hosts文件
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.1.62 yunzu62.cn
192.168.1.63 www.yunzu.cn
192.168.1.63 bbs.yunzu.cn

[root@yunzu62 ~]# yum install elinks -y
[root@yunzu62 ~]# elinks www.yunzu.cn --dump
192.168.1.64
[root@yunzu62 ~]# elinks bbs.yunzu.cn --dump
192.168.1.62

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值