您的位置:首页 > 运维架构 > Linux

linux学习之rhel6.5 varnish服务简介及基础配置 应用

2017-07-20 00:14 597 查看
 ############varnish##############

主机环境: rhel6.5 selinux and iptables disabled

实验主机: 192.168.0.250 varnish

192.168.0.188 apache

192.168.0.189 apache

基础知识

Varnish简述

varnish是一款高性能且开源的方向代理服务器和HTTP加速器,它的开发者poul-Henning kamp FreeBSD 核心的开发人员之一。varnish采用全新的软件体系机构,和现在的硬件体系配合紧密,varnish是一个轻量级的cache和反向代理软件。先进的设计理念和成熟的设计框架式varnish的主要特点。

2)varnish的特点:

1、基于内存进行缓存,重启后数据将消失。

2、利用虚拟内存方式,I\O性能好。

3、支持设置0~60秒的精确缓存时间。

4、VCL配置管理比较灵活。

5、具有强大的管理功能,例如top、stat、admin、list 等。

6、状态机设计巧妙、结构清晰。

7、利用二叉堆管理缓存文件,可达到积极删除目的。

3)varnish中vcl的使用

VCL 处理流程图



处理过程大致分为如下几个步骤:

(1)Receive 状态,也就是请求处理的入口状态,根据 VCL 规则判断该请求应该是 Pass (直接通过)或Pipe(管道),或者进入 Lookup(本地查询)。

(2)Lookup 状态,进入此状态后,会在 hash 表中查找数据,若找到,则进入 Hit 状态,否则进入 miss 状态。

(3)Pass 状态,在此状态下,会进入后端请求,即进入 fetch 状态。

(4)Fetch 状态,在 Fetch 状态下,对请求进行后端的获取,发送请求,获得数据,并进行本地的存储。

(5)Deliver 状态, 将获取到的数据发送给客户端,然后完成本次请求。

VCL语言常用的一些函数和变量简单介绍

vcl_recv模块

用于接受和处理请求,当请求成功被调用后,varnish通过判断请求的数据来决定如何处理请求。

Pass:表示直接进入pass模式,把请求交给vcl_pass模块处理。

Pipe:表示进入pipe模式,把请求交给vcl_pipe模块处理

vcl_pipe模块

此模块在请求进入pipe时被调用,用于将请求直接传递到后端主机,在请求和返回的内容没有改变的情况下,也就是在当前链接没有关闭时,服务器将不变的内容返回给客户端,直到该链接被关闭。

vcl_pass模块

此模块表示当此模块被pass后,用于将请求直接传递到后端应用服务器,后端应用服务器在接受请求后将数据发送给客户端,但不进行任何数据的缓存,在当前连接下每次都返回最新的内容。

Lookup

一个请求在vcl_recv中被lookup后,varnish将在缓存中提取数据,如果缓存中有相应的数据,就把控制权交给vcl_hit模块;如果缓存中没有相应的数据,请求将被设置为pass并将其交给vcl_miss模块。

(5)vcl_hit模块

执行lookup指令后,Varnish在缓存中找到请求的内容后将自动调用该模块。在此模块中,deliver表示将找到的数据发送给客户端,并把控制权交给vcl_deliver模块。

(6)vcl_miss模块

执行lookup后,Varnish在缓存中没有找到请求的内容时会自动调用该方法。此模块可以用于判断是否需要从后端服务器获取内容。在此模块中,fetch表示从后端获取请求的数据,并把控制权交给vcl_fetch模块。

(7)vcl_fetch模块

在后端主机更新缓存并且获取内容后调用该方法,接着,通过判断获取的内容来决定是将内容放入缓存,还是直接返回给客户端。

(8)vcl_deliver模块

当一个没有被缓存的数据交付给客户端的时候被调用。

(9)vcl_timeout 模块

在缓存数据到期前调用此模块。在此模块中,discard表示从缓存中清除到期数据。

(10)vcl_discard模块

在缓存数据到期后或缓存空间不够时,自动调用该模块。在此模块中keep表示将数据继续保留在缓存中。

通过 varnishadm 手动清除缓存

# varnishadm ban.url .*$

#清除所有

# varnishadm ban.url /index.html

#清除 index.html 页面缓存

# varnishadm ban.url /admin/$

#清除 admin 目录缓存

1.安装服务

[root@server1 ~]# cd /etc/varnish/

[root@server1varnish]#yum install varnish-3.0.5-1.el6.x86_64.rpm varnish-libs-3.0.5-1.el6.x86_64.rpm -y

配置——实验

1)[root@server1 varnish]# vim /etc/sysconfig/varnish ##varnish的配置文件,配置 varnish 服务端口

66 VARNISH_LISTEN_PORT=80 #varnish监听端口

[root@server1 varnish]# vim /etc/varnish/default.vcl ##配置一个后端服务器,缓存配置

7 backend default {

8 .host = "172.25.6.2";

9 .port = "80";

10 }

#该块用于配置一块Varnish默认的后端服务器,当varnish需要从后端服务器获取数据时就会访问自己的80端口,varnish也可以定义多台后端服务器实现负载均衡.

[root@server1 varnish]# /etc/init.d/varnish start

Starting Varnish Cache: [ OK ]

测试:

[root@server2 ~]# yum install httpd -y

[root@server2 ~]# /etc/init.d/httpd start #6.5系统开启服务命令

Starting httpd: httpd: Could not reliably determine the server's fully qualified domain name, using 172.25.6.2 for ServerName

[ OK ]
c580

[root@server2 ~]# vim /var/www/html/index.html

<h1>server2</h1>

[root@server3 ~]# yum install httpd -y

[root@server3 ~]# /etc/init.d/httpd start

Starting httpd: httpd: Could not reliably determine the server's fully qualified domain name, using 172.25.6.3 for ServerName

[ OK ]

[root@server3 ~]# vim /var/www/html/index.html

<h1>server3</h1>

Test:在浏览器搜索172.25.6.1 -----> 显示的是server2



2)

[root@server1 varnish]# vim default.vcl #查看缓存命中情况,命中即varnish缓存中是否有需要的数据,命中则显示HIT,未命中则显示MISS

7 backend default {

8 .host = "172.25.6.2";

9 .port = "80";

10 }

11

12 sub vcl_deliver {

13 if (obj.hits > 0) {

14 set resp.http.X-Cache = "HIT from westos cache";

15 }

16 else {

17 set resp.http.X-Cache = "MISS from westos cache";

18 }

19 return (deliver);

20 }

[root@server1 varnish]# /etc/init.d/varnish reload #修改配置文件后重新加载,不能retart

Loading vcl from /etc/varnish/default.vcl

Current running config name is boot

Using new config name reload_2017-07-18T22:55:21

VCL compiled.

available 2 boot

active 0 reload_2017-07-18T22:55:21

Done

测试:

用curl命令,curl直接加域名显示网页内容,curl -I 加域名显示头文件信息

[root@foundation6 ~]# curl www.westos.org

<h1>server2</h1>

[root@foundation6 ~]# vim /etc/hosts

172.25.6.1 www.westos.org

[root@foundation6 ~]# curl -I www.westos.org #第一次查询,varnish上无缓存,显示MISS

HTTP/1.1 200 OK

Server: Apache/2.2.15 (Red Hat)

Last-Modified: Tue, 18 Jul 2017 14:46:12 GMT

ETag: "530-11-5549893ab4f2c"

Content-Type: text/html; charset=UTF-8

Content-Length: 17

Accept-Ranges: bytes

Date: Tue, 18 Jul 2017 14:59:32 GMT

X-Varnish: 420117548

Age: 0

Via: 1.1 varnish

Connection: keep-alive

X-Cache: MISS from westos cache

[root@foundation6 ~]# curl -I www.westos.org #第一次查询,varnish上有缓存,显示HIT

HTTP/1.1 200 OK

Server: Apache/2.2.15 (Red Hat)

Last-Modified: Tue, 18 Jul 2017 14:46:12 GMT

ETag: "530-11-5549893ab4f2c"

Content-Type: text/html; charset=UTF-8

Content-Length: 17

Accept-Ranges: bytes

Date: Tue, 18 Jul 2017 15:01:22 GMT

X-Varnish: 420117551 420117548

Age: 110

Via: 1.1 varnish

Connection: keep-alive

X-Cache: HIT from westos cache

[root@server1 varnish]# varnishadm ban.url .*$ #清空所有varnish缓存

[root@foundation6 ~]# curl -I www.westos.org #清空缓存后第一次搜索,无缓存,显示MISS

HTTP/1.1 200 OK

Server: Apache/2.2.15 (Red Hat)

Last-Modified: Tue, 18 Jul 2017 14:46:12 GMT

ETag: "530-11-5549893ab4f2c"

Content-Type: text/html; charset=UTF-8

Content-Length: 17

Accept-Ranges: bytes

Date: Tue, 18 Jul 2017 15:05:17 GMT

X-Varnish: 420117557

Age: 0

Via: 1.1 varnish

Connection: keep-alive

X-Cache: MISS from westos cache

3)

[root@server1 varnish]# vim default.vc #配置两台后端服务器

7 backend web1 {

8 .host = "172.25.6.2";

9 .port = "80";

10 }

11

12 backend web2 {

13 .host = "172.25.6.3";

14 .port = "80";

15 }

16 sub vcl_recv {

17 if (req.http.host ~ "^(www.)?westos.org") { #当域名为以www.westso.org开头或westos.org时去8.1主机缓存

18 set req.http.host = "www.westos.org";

19 set req.backend = web1;

20 } elsif (req.http.host ~ "^bbs.westos.org") { #当域名以bbs.westos.org开头 时去8.2主机缓存

21 set req.backend = web2;

22 } else {error 404 "westos cache";

23 }

24 }

[root@server1 varnish]# /etc/init.d/varnish reload

Loading vcl from /etc/varnish/default.vcl

Current running config name is reload_2017-07-18T22:55:21

Using new config name reload_2017-07-18T23:12:49

VCL compiled.

available 0 boot

available 2 reload_2017-07-18T22:55:21

active 0 reload_2017-07-18T23:12:49

测试:

[root@foundation6 ~]# vim /etc/hosts

172.25.6.1 www.westos.org bbs.westos.org westos.org

[root@foundation6 ~]# curl www.westos.org

<h1>server2</h1>

[root@foundation6 ~]# curl westos.org

<h1>server2</h1>

[root@foundation6 ~]# curl bbs.westos.org

<h1>server3</h1>

[root@server2 ~]# /etc/init.d/httpd stop

Stopping httpd: [ OK ]

[root@foundation6 ~]# curl www.westos.org

<?xml version="1.0" encoding="utf-8"?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"

"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html>

<head>

<title>503 Service Unavailable</title>

</head>

<body>

<h1>Error 503 Service Unavailable</h1>

<p>Service Unavailable</p>

<h3>Guru Meditation:</h3>

<p>XID: 420117562</p>

<hr>

<p>Varnish cache server</p>

</body>

</html>

varnish 群组定义

VCL 可以把多个 backends 聚合成一个组,这些组被叫做 director,这样可以增强性能和弹力,当组里一个 backend 挂掉后,可以选择另一个健康的 backend。VCL 有多种 director,不同的 director 采用不同的算法选择 backend,主要有以下几种:

..Random director 会根据所设置的权值(weight)来选择 backend,.retries 参数表示尝试找到一个 backend 的最大次数,.weight 参数表示权值

..Round-robin director 在选择 backend 时,会采用循环的方式依次选择。

..Client director 根据 client.identity 来选择 backend,您可以设置 client.identity 的值为 session cookie 来标识 backend。

[root@server2 ~]# vim /etc/httpd/conf/httpd.conf #配置虚拟主机

990 NameVirtualHost *:80

1011 <VirtualHost *:80>

1012 DocumentRoot /var/www/html

1013 ServerName server2

1014 </VirtualHost>

1015

1016 <VirtualHost *:80>

1017 DocumentRoot /www/bbs

1018 ServerName bbs.westos.org

1019 </VirtualHost>

1020

1021 <VirtualHost *:80>

1022 DocumentRoot /www/westos

1023 ServerName www.westos.org

1024 </VirtualHost>

[root@server2 ~]# mkdir /www/bbs -p #建立默认目录,编写头文件

[root@server2 ~]# vim /www/bbs/index.html

<h1>bbs.westos.org</h1>

[root@server2 ~]# mkdir /www/westos

[root@server2 ~]# vim /www/westos/index.html

<h1>server2-www.westos.org</h1>

[root@server2 ~]# /etc/init.d/httpd restart

Stopping httpd: [ OK ]

Starting httpd: httpd: Could not reliably determine the server's fully qualified domain name, using 172.25.6.2 for ServerName

[ OK ]

[root@server3 ~]# vim /var/www/html/index.html

server3-www.westos.org

[root@server3 ~]# /etc/init.d/httpd restart

Stopping httpd: [ OK ]

Starting httpd: httpd: Could not reliably determine the server's fully qualified domain name, using 172.25.6.3 for ServerName

[ OK ]

[root@server1 varnish]# vim default.vc #这里用round-robin做实验

16 director lb round-robin { #建立一个名为lb的组,实现web1和web2之间的轮询

17 { .backend = web1; }

18 { .backend = web2; }

19 }

20

21 sub vcl_recv {

22 if (req.http.host ~ "^(www.)?westos.org") { #当客户端搜索符合条件的域名时,用lb组实现轮询

23 set req.http.host = "www.westos.org";

24 set req.backend = lb;

25 return (pass);

26 } elsif (req.http.host ~ "^bbs.westos.org") {

27 set req.backend = web1;

28 } else {error 404 "westos cache";

29 }

30 }

[root@server1 varnish]# /etc/init.d/varnish reload

Loading vcl from /etc/varnish/default.vcl

Current running config name is reload_2017-07-18T23:17:29

Using new config name reload_2017-07-18T23:45:35

VCL compiled.

available 0 boot

available 0 reload_2017-07-18T22:55:21

available 0 reload_2017-07-18T23:12:49

available 2 reload_2017-07-18T23:17:29

active 0 reload_2017-07-18T23:45:35

Done

测试:实现负载均衡

[root@foundation6 ~]# curl www.westos.org

server3-www.westos.org

[root@foundation6 ~]# curl www.westos.org

<h1>server2-www.westos.org</h1>

[root@foundation6 ~]# curl www.westos.org

server3-www.westos.org

[root@foundation6 ~]# curl www.westos.org

<h1>server2-www.westos.org</h1>







5)varnish cdn 推送平台

[root@server1 varnish]# yum install httpd -y

[root@server1 varnish]# vim /etc/httpd/conf/httpd.conf

Listen 8080

[root@server1 varnish]# ls

bansys.zip

[root@server1 varnish]# yum install unzip -y

[root@server1 varnish]# unzip bansys.zip -d /var/www/html/

[root@server1 varnish]# cd /var/www/html/bansys/

[root@server1 bansys]# ls

class_socket.php config.php index.php purge_action.php static

[root@server1 bansys]# mv * ..

[root@server1 bansys]# ls

[root@server1 bansys]# cd ..

[root@server1 html]# ls

bansys class_socket.php config.php index.php purge_action.php static

[root@server1 html]# rm -fr bansys/

[root@server1 html]# /etc/init.d/varnish start

Starting Varnish Cache: [ OK ]

[root@server1 html]# /etc/init.d/httpd start

Starting httpd: httpd: Could not reliably determine the server's fully qualified domain name, using 172.25.6.1 for ServerName

[ OK ]

[root@server1 html]# yum install php -y

[root@server1 html]# vim config.php (数据库的信息全部加备注)

25 //varnish主机列表

26 //可定义多个主机列表

27 $var_group1 = array(

28 'host' => array('172.25.6.1',),

29 'port' => '80',

30 );

31

32 //varnish群组定义

33 //对主机列表进行绑定

34 $VAR_CLUSTER = array(

35 'www.westos.org' => $var_group1,

36 );

[root@server1 html]# vim /etc/varnish/default.vcl

7 acl westos {

8 "127.0.0.1";

9 "172.25.8.0"/24;

10 }

11

12 backend web1 {

13 .host = "172.25.6.2";

14 .port = "80";

15 }

16

17 backend web2 {

18 .host = "172.25.6.3";

19 .port = "80";

20 }

21 director lb round-robin {

22 { .backend = web1; }

23 { .backend = web2; }

24 }

26 sub vcl_recv {

27 if (req.request == "BAN") {

28 if (!client.ip ~ westos) {

29 error 405 "Not allowed.";

30 }

31 ban("req.url ~ " + req.url);

32 error 200 "ban added";

33 }

34 if (req.http.host ~ "^(www.)?westos.org") {

35 set req.http.host = "www.westos.org";

36 set req.backend = lb;

37 #return (pass);

38 } elsif (req.http.host ~ "^bbs.westos.org") {

39 set req.backend = web1;

40 } else {error 404 "westos cache";

41 }

42 }

43 sub vcl_deliver {

44 if (obj.hits > 0) {

45 set resp.http.X-Cache = "HIT from westos cache";

46 }

47 else {

48 set resp.http.X-Cache = "MISS from westos cache";

49 }

50 return (deliver);

51 }

[root@server1 html]# /etc/init.d/varnish reload

Loading vcl from /etc/varnish/default.vcl

Current running config name is boot

Using new config name reload_2017-07-19T23:26:05

VCL compiled.

available 0 boot

active 0 reload_2017-07-19T23:26:05

Done

[root@server2 ~]# cat /www/westos/index.html

<h1>server2-www.westos.org</h1>

测试:

server2和server3轮询推送







内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: