Centos

Varnish 4.1:這是使用預設 localhost 的正確 VCL 程式碼,除非它不健康,然後回退到 director?

  • April 22, 2016

我只是想確保這是實現這一目標的最理想方式。

這是設置:基本上我們有 3 台伺服器通過 DNS 循環“平衡”。Varnish 在每台伺服器上都配置了一個標準的燈組。

基本上,當請求進來時,我們會檢查預設後端是否健康,如果不是,那麼我們會退回到我們的主管,該主管會輪詢其他兩個伺服器,直到預設後端再次健康為止。所以我們只希望 Varnish 始終使用 localhost ,除非我們的後端不健康。這是我的程式碼:

probe healthcheck {
  .url = "/info.php";
       .timeout = 1s;
       .interval = 4s;
       .window = 5;
       .threshold = 3;
       .expected_response = 200;
}

# Default backend definition. Set this to point to your content server.
backend default {
   .host = "127.0.0.1";
   .port = "8080";
   .probe = healthcheck;
}

#Cluster nodes
backend lamp02 {
 .host  = "192.168.0.102";
 .port = "8080";
 .probe = healthcheck;
}
backend lamp03 {
 .host  = "192.168.0.103";
 .port = "8080";
 .probe = healthcheck;
}

sub vcl_init {
   new server_pool  = directors.round_robin();
   server_pool.add_backend(lamp02);
   server_pool.add_backend(lamp03);
}

sub vcl_recv {
   # Happens before we check if we have this in cache already.
   #
   # Typically you clean up the request here, removing cookies you don't need,
   # rewriting the request, etc.

   if (!std.healthy(req.backend_hint)) {
     set req.backend_hint = server_pool.backend();
   } else {
     set req.backend_hint = default;
   }
}

這是最有效的方法嗎?

謝謝!

是的。您可以積極而不是消極地這樣做,如果您的 VCL 變得複雜,這可能會更好,但您的方法很好。

if (std.healthy(req.backend_hint)) {
 set req.backend_hint = default;
} else {
  set req.backend_hint = server_pool.backend();
}

引用自:https://serverfault.com/questions/758775