使用清漆进行负载平衡

时间:2015-02-15 08:32:04

标签: varnish

我为我的服务器写了一个清漆vcl规则,我不知道这对我的意图是否足够。我想在2个服务器端口和另一个服务器上运行我的项目,这样如果一台服务器出现故障,请求转发给其他服务器。此外,我想提供静态文件,使得不工作的apache web服务器并请求将它们直接发送到龙卷风。这是我的vcl规则,你能帮助我吗?

import directors;

probe healthcheck {
   .url = "/";
   .interval = 30s;
   .timeout = 1 s;
   .window = 5;
   .threshold = 2;
   .initial = 2;
   .expected_response = 200;
}

backend server1{
    .host="127.0.0.1"
    .port = 8004
     .probe = healtcheck;
} 
backend server1-2{
    .host="127.0.0.1"
    .port = 8005
     .probe = healtcheck;
} 
backend server2{
    .host="192.168.1.1"
    .port = 8004
     .probe = healtcheck;
} 
sub vcl_init {
    new vdir = directors.round_robin();
    vdir.add_backend(server1);
    vdir.add_backend(server1-2);
    vdir.add_backend(server2);
}

 sub vcl_recv{
set req.grace = 600s;



### always cache these items:

     if (req.request == "GET" && req.url ~ "\.(js)") {
                lookup;
        }

    ## images
        if (req.request == "GET" && req.url ~ "\.(gif|jpg|jpeg|bmp|png|tiff|tif|ico|img|tga|wmf)$") {
        lookup;
        }

    ## various other content pages  
    if (req.request == "GET" && req.url ~ "\.(css|html)$") {    
        lookup;
        }       

    ## multimedia 
        if (req.request == "GET" && req.url ~ "\.(svg|swf|ico|mp3|mp4|m4a|ogg|mov|avi|wmv)$") {
        lookup;
        }   

    ## xml

        if (req.request == "GET" && req.url ~ "\.(xml)$") {
        lookup;
        }
### do not cache these rules:

        if (req.request != "GET" && req.request != "HEAD") {
                pipe;
        }
### if it passes all these tests, do a lookup anyway;
        lookup;

 }

sub vcl_fetch {

  if (req.url ~ "\.(gif|jpg|jpeg|swf|css|js|flv|mp3|mp4|pdf|ico|png)(\?.*|)$") {
    set beresp.ttl = 365d;
  }
}

1 个答案:

答案 0 :(得分:0)

根据您是否需要为用户使用会话,您应该从round_robin切换到hash。这将确保将相同的用户定向到相同的后端。