【问题标题】:using varnish for load balancing使用 varnish 进行负载平衡
【发布时间】:2015-02-19 20:54:40
【问题描述】:

我为我的服务器编写了一个清漆 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;
  }
}

【问题讨论】:

    标签: varnish


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-03-12
      • 2019-10-20
      • 2012-06-04
      • 1970-01-01
      • 1970-01-01
      • 2017-10-25
      • 2015-09-24
      • 1970-01-01
      相关资源
      最近更新 更多