高性能缓存服务器varnish部署

Varnish是一款高性能、开源的反向代理服务器和缓存服务器。Varnish使用内存缓存文件来减少响应时间和网络带宽消耗。

一、Varnish原理

这里写图片描述

Varnish的处理过程分为几个步骤:

  • Receive 状态,也就是请求处理的入口状态,根据 VCL 规则判断该请求应该是 Pass 或Pipe,或者进入 Lookup(本地查询)。
  • Lookup 状态,进入此状态后,会在 hash 表中查找数据,若找到,则进入 Hit 状态,否则进入 miss 状态。
  • Pass 状态,在此状态下,会进入后端请求,即进入 fetch 状态。
  • Fetch 状态,在 Fetch 状态下,对请求进行后端的获取,发送请求,获得数据,并进行本地的存储。
  • Deliver 状态, 将获取到的数据发送给客户端,然后完成本次请求。
二、varnish服务配置
1.实验环境

varnish缓存服务器:172.25.18.218

yum install varnish-3.0.5-1.el6.x86_64.rpm varnish-libs-3.0.5-1.el6.x86_64.rpm -y

客户端:172.25.18.18
Apache服务器: 172.25.18.1 172.25.18.118

2.配置
(1)/etc/varnish/default.vcl:配置各Child/Cache线程的缓存策略;

首先,先牛刀小试,测试一下varnish的web缓存的功能。

Varnish服务器:
vim /etc/varnish/default.vcl
backend default {
  .host = "172.25.18.118";   ##监听并缓存后端服务器
  .port = "80";
}
客户端:
[root@server1 ~]# echo "172.25.18.218   www.westos.com" >> /etc/hosts
[root@server1 ~]# echo "www.westos.com" > /var/www/html/index.html 
[root@server1 ~]# curl www.westos.com
www.westos.com
[root@server1 ~]# curl -I www.westos.com
HTTP/1.1 200 OK
Server: Apache/2.2.15 (Red Hat)
Last-Modified: Sat, 28 Jul 2018 03:15:13 GMT
ETag: "a0946-10-57206a301e956"
Content-Type: text/html; charset=UTF-8
Content-Length: 16
Accept-Ranges: bytes
Date: Sat, 28 Jul 2018 03:41:41 GMT
X-Varnish: 2021335288
Age: 0
Via: 1.1 varnish
Connection: keep-alive
(2)配置 varnish 服务端口 配置文件 : /etc/sysconfig/varnish 
varnish服务器:
vim /etc/sysconfig/varnish 
  7 # Maximum number of open files (for ulimit -n)
  8 NFILES=131072              ##文件最大打开数,不能超过内核和os限制。(内核限制>OS限制)
  9 
 10 # Locked shared memory (for ulimit -l)
 11 # Default log size is 82MB + header
 12 MEMLOCK=82000
 13 
 14 # Maximum number of threads (for ulimit -u)
 15 NPROCS="unlimited"
...
 66 VARNISH_LISTEN_PORT=80        ##将监听端口改为80
 68 # # Telnet admin interface listen address and port
 69 VARNISH_ADMIN_LISTEN_ADDRESS=127.0.0.1
 70 VARNISH_ADMIN_LISTEN_PORT=6082
 94 # # Default TTL used when the backend does not specify one
 95 VARNISH_TTL=120             ##可以设置TTL缓存时间
3.Rhel6.5下Varnish开启方式:
/etc/init.d/varnish start "开启" | reload "重新读取"
service varnish start / reload
4.Varnish服务端添加函数可以查看缓存命中情况:
[root@server3 html]# 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);
}
命中测试:
[root@18 Desktop]# curl bbs.westos.org -I
HTTP/1.1 200 OK
Server: Apache/2.2.15 (Red Hat)
Last-Modified: Sat, 28 Jul 2018 07:12:54 GMT
ETag: "a0945-5-57209f5075986"
Content-Type: text/html; charset=UTF-8
Content-Length: 5
Accept-Ranges: bytes
Date: Sat, 28 Jul 2018 08:57:10 GMT
X-Varnish: 1501752974 1501752972
Age: 3
Via: 1.1 varnish
Connection: keep-alive
X-Cache: HIT from westos cache       ##HIT 命中
5.Apache服务器不同域名对应不同发布目录:
[root@server1 ~]# echo "bbs.westos.org" > /www/index.html 
[root@server1 ~]# echo "www.westos.com " > /var/www/html/index.html 
[root@server1 ~]# vim /etc/httpd/conf/httpd.conf 
 990 NameVirtualHost *:80
1017 <VirtualHost *:80>
1018     DocumentRoot /www
1019     ServerName bbs.westos.org
1020 </VirtualHost>
1021 
1022 <VirtualHost *:80>
1023     DocumentRoot /var/www/html
1024     ServerName www.westos.com
1025 </VirtualHost>
客户端:
[root@18 Desktop]# curl www.westos.com
www.westos.com 
[root@18 Desktop]# curl bbs.westos.org
bbs.westos.org
三、varnish一般功能
1.varnish清理缓存
varnishadm ban.url .*$           //清除所有'
varnishadm ban.url /index.html    //清除 index.html 页面缓存
varnishadm ban.url /admin/$      //清除 admin 目录缓存
2.设置同时监听多个站点
backend web1 {
  .host = "172.25.18.118";
  .port = "80";
}
backend web2 {
  .host = "172.25.18.1";
  .port = "80";
}
sub vcl_recv {
    if (req.http.host ~ "^(www.)?westos.com") {
    set req.http.host = "www.westos.com";
    set req.backend = web2;
} elsif (req.http.host ~ "^bbs.westos.org") {
    set req.backend = web1;
    } else { error 404 "westos cache"; }
}

当访问 www.westos.org 域名时从 server2 上取数据,访问 bbs.westos.org 域名时到 web3 取数据,访问其他页面报错。
这里写图片描述

[root@server3 html]# /etc/init.d/varnish reload
Loading vcl from /etc/varnish/default.vcl
Current running config name is boot
Using new config name reload_2018-07-28T15:17:19
VCL compiled.

available       2 boot
active          0 reload_2018-07-28T15:17:19

Done
Web2 apache服务器:
[root@server1 ~]# echo "web2" > /var/www/html/index.html 
Web1 apache服务器:
[root@server1 ~]# echo "web1" > /var/www/html/index.html 
客户端访问测试:
[root@18 Desktop]# tail -n 1 /etc/hosts
172.25.18.218   www.westos.com      ##varnish服务器
[root@18 Desktop]# curl www.westos.com
web2

[root@18 Desktop]# curl bbs.westos.org
Web1
3.varnish设置web轮询

客户访问域名www.westos.com时,varnish可以选择web1,web2服务器轮询缓存。

director westos round-robin {
    { .backend = web1; }
    { .backend = web2; }
}


sub vcl_recv {
    if (req.http.host ~ "^(www.)?westos.com") {
    set req.http.host = "www.westos.com";
    set req.backend = westos;
    return (pass);
} elsif (req.http.host ~ "^bbs.westos.org") {
    set req.backend = web1;
    } else { error 404 "westos cache"; }
}
客户端检测:
[root@18 Desktop]# curl www.westos.com
web1
[root@18 Desktop]# curl www.westos.com
web2
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值