varnish服务
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了varnish服务相关的知识,希望对你有一定的参考价值。
1.安装varnish
http :// repo . varnish - cache . org / redhat / varnish -3.0/ el 6/ x 86_64/
# yum install varnish-3.0.5-1.el6.x86_64.rpm varnish-libs-3.0.5-1.el6.x86_64.rpm -y
2.配置
### 配置一个后端服务器
# vim /etc/varnish/default.vcl
backend default {
.host = "172.25.7.2";
.port = "80";
}
### 配置 varnish 服务端口
# vim /etc/sysconfig/varnish
66 VARNISH_LISTEN_PORT=80
### 重启varnish
# /etc/init.d/varnish start
# service varnish start
### 查看缓存命中情况
# 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
在172.25.7.2上:
[[email protected] ~]# cat /var/www/html/index.html
<h1>server2</h1>
###测试缓存命中
[[email protected] varnish]# curl www.westos.org
<h1>server2</h1>
[[email protected] varnish]# curl -I 172.25.7.1
HTTP/1.1 200 OK
Server: Apache/2.2.15 (Red Hat)
Last-Modified: Wed, 19 Jul 2017 09:05:44 GMT
ETag: "7fc25-8-554a7efdcc97c"
Content-Type: text/html; charset=UTF-8
Content-Length: 8
Accept-Ranges: bytes
Date: Thu, 20 Jul 2017 03:29:11 GMT
X-Varnish: 1699361872 1699361871
Age: 35
Via: 1.1 varnish
Connection: keep-alive
X-Cache: HIT from westos cache## 命中
[[email protected] varnish]# curl -I 172.25.7.1
HTTP/1.1 200 OK
Server: Apache/2.2.15 (Red Hat)
Last-Modified: Wed, 19 Jul 2017 09:05:44 GMT
ETag: "7fc25-8-554a7efdcc97c"
Content-Type: text/html; charset=UTF-8
Content-Length: 8
Accept-Ranges: bytes
Date: Thu, 20 Jul 2017 03:29:08 GMT
X-Varnish: 1699361871
Age: 0
Via: 1.1 varnish
Connection: keep-alive
X-Cache: MISS from westos cache## 未命中
### 通过 varnishadm 手动清除缓存
# varnishadm ban.url .*$ #清除所有
# varnishadm ban.url /index.html #清除 index.html 页面缓存
# varnishadm ban.url /admin/$ #清除 admin 目录缓存
###定义多个不同域名站点的后端服务器
# vim /etc/varnish/default.vcl
12 backend web1 {
13 .host = "172.25.7.2";
14 .port = "80";
15 }
16
17 backend web2 {
18 .host = "172.25.7.3";
19 .port = "80";
20 }
#当访问 www.westos.org 域名时从 web1 上取数据,访问 bbs.westos.org 域名时到 web2 取数据,
访问其他页面报错。
sub vcl_recv {
if (req.http.host ~ "^(www.)?westos.org<h1>server2</h1>
") {
set req.http.host = "www.westos.org";
set req.backend = web1;
} elsif (req.http.host ~ "^bbs.westos.org") {
set req.backend = web2;
} else {
error 404 "westos cache";
}
}
# service varnish reload
测试:当访问 www.westos.org 域名时从 web1 上取数据,访问 bbs.westos.org 域名时到 web2 取数据,访问其他页面报错
[[email protected] ~]# curl www.westos.org
<h1>server2</h1>
[[email protected] ~]# curl westos.org
<h1>server2</h1>
[[email protected] ~]# curl bbs.westos.org
<h1>server3-bbs.wesros.org</h1>
[[email protected] ~]# curl 172.25.7.1 -I
HTTP/1.1 404 westos cache
Server: Varnish
Content-Type: text/html; charset=utf-8
Retry-After: 5
Content-Length: 398
Accept-Ranges: bytes
Date: Thu, 20 Jul 2017 06:27:58 GMT
X-Varnish: 1539950550
Age: 0
Via: 1.1 varnish
Connection: close
X-Cache: MISS from westos cache
###定义负载均衡
1.#定义健康检查
probe healthcheck {
.url = "/index.html"; # 哪个 url 需要 varnish 请求
.interval = 5s; #检查的间隔时间
.timeout = 1s; #等待多长时间探针超时
.window = 5; #维持 5 个 sliding window 的结果
.threshold = 3; #至少有三次 window 是成功的,就宣告 bachend 健康
}
2.# vim /etc/httpd/conf/httpd.conf
136 Listen 8080 监听8080端口
3.# vim /etc/varnish/default.vcl
9 backend web1 {
10 .host = "172.25.7.2";
11 .port = "80";
12 }
13
14 backend web2 {
15 .host = "172.25.7.3";
16 .port = "80";
17 }
18 director lb round-robin {#把多个后端聚合为一个组,并检测后端健康状况
19 {.backend = web1;}
20 {.backend = web2;}
21}
22 sub vcl_recv {
23 if (req.http.host ~ "^(www.)?westos.org") {
24 set req.http.host = "www.westos.org";
25 set req.backend = lb;
26 return (pass);#为了测试方便,不进行缓存。
27 } elsif (req.http.host ~ "^bbs.westos.org") {
28 set req.backend = web2;
29 } else {error 404 "westos cache";
30 }
31 }
# service varnish reload
测试:网页自动刷新用F5
命令: for i in {1..10};do curl www.westos.org/index.html; done
<h1>server2-www.westos.org</h1>
<h1>server3-www.wesros.org</h1>
<h1>server2-www.westos.org</h1>
<h1>server3-www.wesros.org</h1>
<h1>server2-www.westos.org</h1>
<h1>server3-www.wesros.org</h1>
<h1>server2-www.westos.org</h1>
<h1>server3-www.wesros.org</h1>
<h1>server2-www.westos.org</h1>
<h1>server3-www.wesros.org</h1>
###varnish cdn 推送平台
1.安装php
# yum install unzip -y
# unzip bansys.zip -d /var/www/html/#解压到指定目录
# yum install php -y
2.配置服务
# vim /var/www/html/bansys/config.php #只保留如下设置,其余注释掉
25 //varnish主机列表
26 //可定义多个主机列表
27 $var_group1 = array(
28 ‘host‘ => array(‘172.25.7.1‘),
29 ‘port‘ => ‘80‘,
30 );
31
32
33 //varnish群组定义
34 //对主机列表进行绑定
35 $VAR_CLUSTER = array(
36 ‘www.westos.org‘ => $var_group1,
37 );
38
39
40 //varnish版本
41 //2.x和3.x推送命令不一样
42 $VAR_VERSION = "3";
43
44 ?>
3.#bansys 有两种工作模式,分别是:telnet 和 http 模式。
#telnet 模式需要关闭 varnish 服务管理端口的验证,注释掉/etc/sysconfig/varnish 文件中的 “ -S $
{VARNISH_SECRET_FILE}”这行,重启 varnish 服务即可。
#如果是 http 模式需要对 varnish 做以下设置:
# vim /etc/varnish/default.vcl
7 acl westos {
8 "172.25.7.1";#设置访问控制
9 "172.25.7.0"/24;
10 }
27 sub vcl_recv {
28
29 if (req.request == "BAN") {
30 if (!client.ip ~ westos) {
31 error 405 "Not allowed.";
32 }
33 ban("req.url ~ " + req.url);
34 error 200 "ban added";
35 }
36 }
# service varnish reload
htppd服务端:
[[email protected] westos]# cat /www/westos/index.html
<h1>server2-www.westos.org</h1>
<h1>server2-www.westos.org</h1>
<h1>server2-www.westos.org</h1>
<h1>server2-www.westos.org</h1>
<h1>server2-www.westos.org</h1>
<h1>server2-www.westos.org</h1>
<h1>server2-www.westos.org</h1>
[[email protected] ~]# cat /var/www/html/index.html
<h1>server3-www.wesros.org</h1>
# service httpd restart
测试:当推送时界面轮询一次
以上是关于varnish服务的主要内容,如果未能解决你的问题,请参考以下文章