varnish反向代理服务器

1.先简单介绍一下VCL

VCL,即为Varnish Configuation Language,用来定义varnish的存取策略,VCL语法比较简单,跟C和perl比较相似,可以使用指定运算符“=”,比较运算符“==”,逻 辑运算符“!,&&,!!”等形式。还支持正则表达样和用“~”进行ACL匹配运算,同时还可以使用“set”这样的关键字来指定变量。
需要注意的是,“\”字符在VCL里没有特别的含义,这点与其它语言略有不同,另外,VCL只是配置,并不是真正的编程语言,没有循环,也没有自定义变量。
2.VCL处理流程图

处理过程大致分为如下几个步骤:
(1) Receive状态,也就是请求处理的入口状态,根据VCL规则判断该请求应该是Pass或Pipe,或者进入Lookup(本地查询)。
(2) Lookup状态,进入此状态后,会在hash表中查找数据,若找到,则进入Hit状态,否则进入miss状态。
(3) Pass状态,在此状态下,会进入后端请求,即进入fetch状态。
(4) Fetch状态,在Fetch状态下,对请求进行后端的获取,发送请求,获得数据,并进行本地的存储。
(5) Deliver状态, 将获取到的数据发送给客户端,然后完成本次请求。


搭建环境:

                192.168.2.107  varnish主机

                192.168.2.111  apache服务器

                192.168.2.112  apache服务器  

varnish组件:

varnish-3.0.5-1.el6.x86_64.rpm
 varnish-libs-3.0.5-1.el6.x86_64.rpm

一.安装配置

varnish服务器107配置:

1.yum install libedit gcc
2.rpm -ivh varnish-3.0.5-1.el6.x86_64.rpm   varnish-libs-3.0.5-1.el6.x86_64.rpm

3.vim /etc/sysconfig/varnish
VARNISH_LISTEN_PORT=80   修改varnish端口为80端口
4.vim  /etc/varnish/default.vcl
    backend default {
  .host = "192.168.2.111";       配置一个后端服务器
  .port = "80";
 5./etc/init.d/httpd stop  varnish 服务器必须关闭httpd服务
6./etc/init.d/varnish start

httpd服务器111配置

按照自己需求搭建httpd服务器即可(端口为80)

7.在varnish服务器107进行测试代理:

[root@desktop107 varnish]# curl -I 192.168.2.107(varnish服务器IP)
HTTP/1.1 200 OK
Server: Apache/2.2.15 (Red Hat)
Last-Modified: Sat, 31 Jan 2015 13:19:26 GMT
ETag: "106c-8-50df291fd69be"
Content-Type: text/html; charset=UTF-8
Content-Length: 8
Accept-Ranges: bytes
Date: Sat, 31 Jan 2015 14:28:00 GMT
X-Varnish: 1263928790
Age: 0                                   # varnish默认120秒清理缓存一次Via: 1.1 varnish
Connection: keep-alive

注:

varnishadm ban.url .*$                      清除所有缓存
varnishadm ban.url /index.html         清除index.html页面缓存
varnishadm ban.url  /admin/$           清除admin目录缓存

二.多个不同域名站点的后端服务器

1.在varnish服务器添加节点信息

[root@desktop107 ~]# vim /etc/varnish/default.vcl

backend default {
  .host = "192.168.2.111";
  .port = "80";
}
backend default {
  .host = "192.168.2.112";
  .port = "80";
}
sub vcl_recv {
if (req.http.host ~ "^(www.)?centrald1.org") {
set req.http.host = "www.centrald1.org";
set req.backend = web1;
} elsif (req.http.host ~ "^bbs.centrald2.org") {
set req.backend = web2;
} else {
error 404 "centrald cache";
}
}
sub vcl_deliver {
if (obj.hits > 0){
set resp.http.X-Cache = "HIT from centrald cache";
}
else {
set resp.http.X-Cache = "MISS from centrald cache";
}
return (deliver);

}

2.搭建111,112两台服务器的web服务

3.重启varnish服务器的varnish服务

/etc/init.d/varnish reload
4.在本地hosts中配置域名解析,则可已访问不同的页面,得到不同的网页内容

vim /etc/hosts

192.168.2.107   centrald1.org
192.168.2.107   www.centrald1.org
192.168.2.107   bbs.centrald2.org

三varnish后端服务器的负载均衡
1.varnish 配置文件的修改
vim /etc/varnish/default.vcl

  director lb round-robin {      添加这一段
        {.backend = web1;}
        {.backend = web2;}
}


sub vcl_recv {
if (req.http.host ~ "^(www.)?centrald1.org") {
set req.http.host = "www.centrald1.org";
set req.backend = lb;  lb为负载均衡节点 
  return (pass);   #为了检测方便,不进行缓存。实际应用环境中一般不加这一条                                 
} elsif (req.http.host ~ "^bbs.centrald2.org") {
set req.backend = web2;
} else {
error 404 "centrald cache";
}
}
由于有两个web2,所以在web2的主机上建立虚拟主机
2.vim /etc/httpd/conf/httpd.conf         在web2的主机上建立虚拟主机
NameVirtualHost *:80     取消这一句前面的#

<VirtualHost *:80>
    DocumentRoot /var/www/html
    ServerName www.centrald1.org
</VirtualHost>

<VirtualHost *:80>
    DocumentRoot /bbs
    ServerName bbs.centrald2.org
</VirtualHost>
3.mkdir /bbs
   vim /bbs/index.html   建立虚拟主机的访问页面
4./etc/init.d/varnish reload varnish服务器重启
  注:varnishadm ban.url .*$    清除varnish缓存
5. 在访问centrald1.org 时 会进行负载均衡,类似于lvs,采用轮循的方式去访问后端web服务器。在测试时,设置的centrald1.org的两个页面内容不一致,刷新页面不变,这是因为varnish缓存的作用,可以进行缓存清 理,需要在varnish的服务器中进行清理(如果添加了return(pass),则会自动清理缓存,实际搭建过程不建议这样做,否则失去了 varnish的意义了)


你可能感兴趣的:(代理,varnish,反向)