Varnish概述
Varnish是一款高性能的开源HTTP加速器,其采用全新的软件体系机构,和现在的硬件体系紧密配合,与传统的 squid 相比,varnish 具有性能更高、速度更快、管理更加方便等诸多优点,很多大型的网站都开始尝试使用 varnish 来替换 squid,这些都促进 varnish 迅速发展起来。
Varnish 的作者Poul-Henning Kamp是FreeBSD的内核开发者之一,他认为现在的计算机比起1975年已经复杂许多。在1975年时,储存媒介只有两种:内存与硬盘。但现在计算机系统的内存除了主存外,还包括了CPU内的L1、L2,甚至有L3快取。硬盘上也有自己的快取装置,因此Squid Cache自行处理物件替换的架构不可能得知这些情况而做到最佳化,但操作系统可以得知这些情况,所以这部份的工作应该交给操作系统处理,这就是 Varnish cache设计架构。
varnish 安装与简单配置
下载 varnish 安装包(传送门:http://download.youkuaiyun.com/download/running_free/9978451),下载后放在自己方便使用的目录下
##server1
[root@server1 ~]# cd /software/
[root@server1 software]# ls
varnish-3.0.5-1.el6.x86_64.rpm varnish-libs-3.0.5-1.el6.x86_64.rpm
[root@server1 software]# yum install -y *
简单配置与测试
[root@server1 software]# vim /etc/varnish/default.vcl
7 backend web1 {
8 .host = "172.25.27.2";
9 .port = "80";
10 }
[root@server1 software]# vim /etc/sysconfig/varnish
7 # Maximum number of open files (for ulimit -n)
8 NFILES=65535
9
10 # Locked shared memory (for ulimit -l)
11 # Default log size is 82MB + header
12 MEMLOCK=64000
13
14 # Maximum number of threads (for ulimit -u)
15 NPROCS="unlimited"
66 VARNISH_LISTEN_PORT=80
[root@server1 software]# vim /etc/security/limits.conf
51 varnish - nofile 65535
[root@server1 software]# service varnish start
##server2
[root@server2 ~]# service httpd start
Starting httpd: httpd: Could not reliably determine the server's fully qualified domain name, using 172.25.27.2 for ServerName
[ OK ]
[root@server2 ~]# touch /var/www/html/index.html
[root@server2 ~]# echo "<h1>server 222222222</h1>">/var/www/html/index.html
##真机测试访问
解析
[root@foundation27 iso]# vim /etc/hosts
172.25.27.1 server1 www.redhat.org
[root@foundation27 iso]# curl 172.25.27.1
<h1>server 222222222</h1>
[root@foundation27 iso]# curl www.redhat.org
<h1>server 222222222</h1>
浏览器测试访问
查看缓存命中情况
###查看缓存命中情况
[root@server1 software]# vim /etc/varnish/default.vcl
sub vcl_deliver {
if (obj.hits > 0) {
set resp.http.X-Cache = "HIT from westos cache";
}
else {
set resp.http.X-Cache = "MISS from westos cache";
}
return (deliver);
}
# service varnish reload
###测试缓存命中
###第一次为MISS,第二次以后为HIT
[root@foundation27 iso]# curl -I www.redhat.org
HTTP/1.1 200 OK
Server: Apache/2.2.15 (Red Hat)
Last-Modified: Thu, 14 Sep 2017 02:12:04 GMT
ETag: "200eb-1a-5591ccd7ba4b3"
Content-Type: text/html; charset=UTF-8
Content-Length: 26
Accept-Ranges: bytes
Date: Thu, 14 Sep 2017 02:39:26 GMT
X-Varnish: 1596377212
Age: 0
Via: 1.1 varnish
Connection: keep-alive
X-Cache: MISS from westos cache
[root@foundation27 iso]# curl -I www.redhat.org
HTTP/1.1 200 OK
Server: Apache/2.2.15 (Red Hat)
Last-Modified: Thu, 14 Sep 2017 02:12:04 GMT
ETag: "200eb-1a-5591ccd7ba4b3"
Content-Type: text/html; charset=UTF-8
Content-Length: 26
Accept-Ranges: bytes
Date: Thu, 14 Sep 2017 02:39:35 GMT
X-Varnish: 1596377213 1596377212
Age: 9
Via: 1.1 varnish
Connection: keep-alive
X-Cache: HIT from westos cache
通过 varnishadm 手动清除缓存
### 通过 varnishadm 手动清除缓存
# varnishadm ban.url .*$ #清除所有
# varnishadm ban.url /index.html #清除 index.html 页面缓存
# varnishadm ban.url /admin/$ #清除 admin 目录缓存
定义多个不同域名站点的后端服务器
###定义多个不同域名站点的后端服务器
[root@server1 software]# vim /etc/varnish/default.vcl
backend web1 {
.host = "172.25.27.2";
.port = "80";
}
backend web2 {
.host = "172.25.27.3";
.port = "80";
}
#当访问 www.westos.org 域名时从 web1 上取数据,访问 bbs.westos.org 域名时到 web2 取数据,访问其他页面报错。
sub vcl_recv {
if (req.http.host ~ "^(www.)?redhat.org") {
set req.http.host = "www.redhat.org";
set req.backend = web1;
} elsif (req.http.host ~ "^bbs.redhat.org") {
set req.backend = web2;
} else {error 404 "redhat cache";
}
}
# service varnish reload
[root@foundation27 iso]# vim /etc/hosts
172.25.27.1 server1 www.redhat.org bbs.redhat.org
[root@foundation27 iso]# curl bbs.redhat.org
<h1>server 333333333</h1>
[root@foundation27 iso]# curl www.redhat.org
<h1>server 222222222</h1>
浏览器测试访问
定义负载均衡
###定义负载均衡
[root@server1 software]# vim /etc/varnish/default.vcl
#定义健康检查 ##可不配置
probe healthcheck {
.url = "/index.html"; # 哪个 url 需要 varnish 请求
.interval = 5s; #检查的间隔时间
.timeout = 1s; #等待多长时间探针超时
.window = 5; #维持 5 个 sliding window 的结果
.threshold = 3; #至少有三次 window 是成功的,就宣告 bachend 健康
}
---------------------------------------------------------------------
backend web1 {
.host = "172.25.27.2";
.port = "80";
}
backend web2 {
.host = "172.25.27.3";
.port = "80";
}
director lb round-robin { #把多个后端聚合为一个组,并检测后端健康状况
{ .backend = web1; }
{ .backend = web2; }
}
sub vcl_recv {
if (req.http.host ~ "^(www.)?redhat.org") {
set req.http.host = "www.redhat.org";
set req.backend = lb ;
return (pass); #为了测试方便,不进行缓存。
} elsif (req.http.host ~ "^bbs.redhat.org") {
set req.backend = web2;
} else {error 404 "redhat cache";
}
}
sub vcl_deliver {
if (obj.hits > 0) {
set resp.http.X-Cache = "HIT from redhat cache";
}
else {
set resp.http.X-Cache = "MISS from redhat cache";
}
return (deliver);
}
# service varnish reload
浏览器第一次访问
第二次访问