varnish 代理服务器的搭建

基本流程

firefox   ->  /hosts/dns   ->  varnish   ->  httpd

varnish 端

1:装包

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

2:改变varnish监听端口

vim /etc/sysconfig/varnish
66 VARNISH_LISTEN_PORT=80

3:修改varnish配置文件

cd /etc/varnish/
vim default.vcl

4:端口转发

7 backend default {
  8   .host = "172.25.200.2";
  9   .port = "80";
 10 }

5:修改缓存命中提示语

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

http

  • httpd 下载与基本配置

varnish 两端多个后台代理

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

backend web2 {
  .host = "172.25.200.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 lb 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 = lb;    #修改接收为lb
return(pass);        #为了测试方便,不进行缓存
} elsif (req.http.host ~ "^bbs.westos.org") {
set req.backend = web2;
} else {error 404 "westos cache";
}
}

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端


 //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

选择另一台主机

curl www.westos.org/index.html

server3 http 配置文

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    /www
1017 VirtualHost>
service varnish reload   #重新加载
varnishadm ban.url .*$      #清空varnish缓存
curl -I www.westos.org   #查看变化
  • 参考文章
    http://blog.csdn.net/wangpengqi/article/details/8889826

你可能感兴趣的:(linux_运维)