码迷,mamicode.com
首页 > 其他好文 > 详细

负载均衡varnish

时间:2015-10-29 16:26:39      阅读:287      评论:0      收藏:0      [点我收藏+]

标签:负载均衡varnish

  1. 下载安装varnish

[root@node3 ~]# rpm --nosignature -i https://repo.varnish-cache.org/redhat/varnish-4.0.el6.rpm

[root@node3 ~]# yum install varnish.x86_64

[root@node3 ~]# egrep -v  "^$|^#"  /etc/sysconfig/varnish
NFILES=131072
MEMLOCK=82000
NPROCS="unlimited"
RELOAD_VCL=1
VARNISH_VCL_CONF=/etc/varnish/default.vcl
VARNISH_LISTEN_PORT=88          -远程访问端口
VARNISH_ADMIN_LISTEN_ADDRESS=192.168.2.112
VARNISH_ADMIN_LISTEN_PORT=6082         --监控管理地址
VARNISH_SECRET_FILE=/etc/varnish/secret
VARNISH_MIN_THREADS=50
VARNISH_MAX_THREADS=1000
VARNISH_THREAD_TIMEOUT=120
VARNISH_STORAGE_SIZE=256M
VARNISH_STORAGE="malloc,${VARNISH_STORAGE_SIZE}"
VARNISH_TTL=120
DAEMON_OPTS="-a ${VARNISH_LISTEN_ADDRESS}:${VARNISH_LISTEN_PORT} \
             -f ${VARNISH_VCL_CONF} \
             -T ${VARNISH_ADMIN_LISTEN_ADDRESS}:${VARNISH_ADMIN_LISTEN_PORT} \
             -t ${VARNISH_TTL} \
             -p thread_pool_min=${VARNISH_MIN_THREADS} \
             -p thread_pool_max=${VARNISH_MAX_THREADS} \
             -p thread_pool_timeout=${VARNISH_THREAD_TIMEOUT} \
             -u varnish -g varnish \
             -S ${VARNISH_SECRET_FILE} \
             -s ${VARNISH_STORAGE}"

[root@node3 varnish]# netstat -antulp | grep varnish
tcp        0      0 0.0.0.0:88                  0.0.0.0:*                   LISTEN      5107/varnishd       
tcp        0      0 192.168.2.112:6082          0.0.0.0:*                   LISTEN      5105/varnishd       
tcp        0      0 :::88                       :::*                        LISTEN      5107/varnishd      
[root@node3 ~]# cd /etc/varnish/

2.简单的反向代理

[root@node3 varnish]# vim default.vcl

vcl 4.0;
backend node1 {
    .host = "192.168.2.112";
    .port = "8082";
}
sub vcl_recv {
}

sub vcl_backend_response {
}

sub vcl_deliver {
}

技术分享

2.根据文件对服务器分发

[root@node3 varnish]# vim default.vcl
vcl 4.0;

backend node1 {
    .host = "192.168.2.112";
    .port = "8082";
}

backend node2 {
    .host = "192.168.2.114";
    .port = "8082";
}

sub vcl_recv {

    if (req.url ~ "^/index.html") {
       set req.backend_hint = node1;
    }else {
       set req.backend_hint = node2;
    }
}
sub vcl_backend_response {
}

sub vcl_deliver {
}

技术分享

技术分享


3.权限主机分配服务器

[root@node3 varnish]# vim default.vcl 

vcl 4.0;


backend node1 {

    .host = "192.168.2.112";

    .port = "8082";

}


backend node2 {

    .host = "192.168.2.114";

    .port = "8082";

}


backend node3 {

    .host = "192.168.2.115";

    .port = "8082";

}

sub vcl_recv {

    if (req.http.host  ~ "www.tong.com" || req.http.host ~ "tong.com") {

         set req.backend_hint = node1;

    }elif (req.http.host  ~ "www.cheng.com" || req.http.host ~ "cheng.com") {

         set req.backend_hint = node2;

    }

    else {

         set req.backend_hint = node3;

    }

}


sub vcl_backend_response {

}


sub vcl_deliver {

}

技术分享

技术分享

技术分享

技术分享


本文出自 “一起走过的日子” 博客,请务必保留此出处http://tongcheng.blog.51cto.com/6214144/1707744

负载均衡varnish

标签:负载均衡varnish

原文地址:http://tongcheng.blog.51cto.com/6214144/1707744

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!