varnish代理服务器的搭建

Varnish是一款高性能的开源HTTP加速器,开源的反向代理服务器和缓存服务器,挪威最大的在线报纸 Verdens Gang 使用3台Varnish代替了原来的12台Squid,性能比以前更好


网站的基本访问方式

firefox   ->  /hosts/dns   ->  varnish  -> 服务器-> varnish ->  httpd

varnish的配置

  • 注意添加域名解析

1.varnish的安装,配置

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

[root@server1 varnish]# sysctl -a |grep file   #查看虚拟机最大打开文件数
fs.file-nr = 480    0    98861
fs.file-max = 98861

vim /etc/sysconfig/varnish    #修改

# Maximum number of open files (for ulimit -n)
NFILES=98861

# Locked shared memory (for ulimit -l)
# Default log size is 82MB + header
MEMLOCK=82000

# Maximum number of threads (for ulimit -u)
NPROCS="unlimited"

vim /etc/security/limits.conf

 51 varnish         -       nofile          98861
 52 varnish         -       memlock         82000
 53 varnish         -       nproc           unlimited

 2.修改varnish的端口

vim /etc/sysconfig/varnish     
66 VARNISH_LISTEN_PORT=80

3.varnish的配置文件

cd /etc/varnish/

vim default.vcl

脚本配置文件:/etc/sysconfig/varnish #用于指定配置参数,可以修改缓存保存时间,大小等

4:缓存(通过缓存机制,可以使得在不同地区的人可以通过访问市级的varnish服务器,可以提高访问速度)

vim /etc/varnish/default.vcl

backend default {
     .host = "172.25.12.2";
     .port = "80";
  }

 可以通过访问172.25.12.1(varnish服务器)来访问172.25.12.2的信息,服务器相当与缓存了2里面的信息,可以直接在服务器里面拿取

5:修改缓存命中提示语

 7 backend default {
  8   .host = "172.25.12.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 }

 通过curl 172.25.12.1 -I 可以看到里面的反馈消息HIT from westos cache(代表获取到了缓存消息)或者MISS from westos cache(没有获取缓存消息)

6,varnish轮询

(轮询可以减轻主服务器的压力)

backend web1 {
  .host = "172.25.12.2";
  .port = "80";
}

backend web2 {
  .host = "172.25.12.3";
  .port = "80";
}
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);
}
#varlish 负载均衡

# lb 轮询机制配置
director westos round-robin {
{       .backend = web1; }
{       .backend = web2; }
}


sub vcl_recv {
if (req.http.host ~ "^(www.)?westos.org") {
set req.http.host = "www.westos.org";
set req.backend = westos;    #修改接收为westos
return(pass);        #为了测试方便,不进行缓存
} elsif (req.http.host ~ "^bbs.westos.org") {
set req.backend = web2;
} else {error 404 "westos cache";
}
}

 在此处可以采用http的虚拟主机功能

可以通过访问的地址不同去访问不同的页面

1.安装httpd

2.vim /etc/httpd/conf/httpd.conf

990 NameVirtualHost *:80

1010 <VirtualHost *:80>
1011         ServerName bbs.westos.org
1012         DocumentRoot    /var/www/html
1013 </VirtualHost>
1014 <VirtualHost *:80>
1015         ServerName www.westos.org
1016         DocumentRoot    /www1
1017 </VirtualHost>

varnish cdn 推送

unzip -x bansys.zip    #解压推送php页面
yum install -y php
vim /etc/httpd/conf/httpd.conf     #修改http监听端口
136 Listen 8080
netstat -antlp | grep :80
unzip -x bansys.zip     #把解压内容放到http默认发布目录
mv * /var/www/html/
vim /var/www/html/config.php    #修改php内容,推送主机以及varnish端
<?php

 //varnish主机列表
 //可定义多个主机列表
 $var_group1 = array(
                        'host' => array('172.25.200.1'),     #定义varnish端
                                                'port' => '80',
                    );

 //varnish群组定义
 //对主机列表进行绑定
 $VAR_CLUSTER = array(
                         'www.westos.org' => $var_group1,   #对www推送
                         'bbs.westos.org' => $var_group1,   #对bbs推送
                     );


 //varnish版本
 //2.x和3.x推送命令不一样
 $VAR_VERSION = "3";

?>

 /etc/init.d/httpd start
vim /etc/varnish/default.vcl

acl westos {            #定义varnish可推送网段
"127.0.0.1";
"172.25.200.0"/24;
}


backend web1 {
  .host = "172.25.200.2";
  .port = "80";
}

backend web2 {
  .host = "172.25.200.3";
  .port = "80";
}


sub vcl_recv {


if (req.request == "BAN") { #添加推送代码
if (!client.ip ~ westos) {
error 405 "Not allowed.";
}
ban("req.url ~ " + req.url);
error 200 "ban added";
}

if (req.http.host ~ "^(www.)?westos.org") {
set req.http.host = "www.westos.org";
set req.backend = lb;
#return(pass);          #注释
} elsif (req.http.host ~ "^bbs.westos.org") {
set req.backend = web2;
} else {error 404 "westos cache";
}
}

/etc/init.d/varnish reload

 测试

打开网页 ip:8080

选择 http

推送内容写 /index.html

将会将/index.html的缓存页面清楚重新缓存

猜你喜欢

转载自blog.csdn.net/u010489158/article/details/81258620