指定 varnish 的預設虛擬主機

指定 varnish 的預設虛擬主機

我在 Scientific Linux 6.4(64 位元)上使用 Varnish-3.0.5:

$ rpm -q varnish
varnish-3.0.5-1.el5.centos.x86_64
$ cat /etc/redhat-release 
Scientific Linux release 6.4 (Carbon)
$ uname -a
Linux XXX.XXX.XXX 2.6.32-358.23.2.el6.x86_64 #1 SMP Wed Oct 16 11:13:47 CDT 2013 x86_64 x86_64 x86_64 GNU/Linux
$ curl XXX.XX.XX.XXX

<html>
<head>
  <title>Page Unavailable</title>
  <style>
    body { background: #303030; text-align: center; color: white; }
    #page { border: 1px solid #CCC; width: 500px; margin: 100px auto 0; padding: 30px; background: #323232; }
    a, a:link, a:visited { color: #CCC; }
    .error { color: #222; }
  </style>
</head>
<body onload="setTimeout(function() { window.location = '/' }, 5000)">
  <div id="page">
    <h1 class="title">Page Unavailable</h1>
    <p>The page you requested is temporarily unavailable.</p>
    <p>We're redirecting you to the <a href="/">homepage</a> in 5 seconds.</p>
    <div class="error">(Error 503 Service Unavailable)</div>
  </div>
</body>
</html>
$ 

我試圖弄清楚如何配置預設虛擬主機,因為每當我運行IP 位址Error 503 Service Unavailable時,如果我的後端至少有一個關閉。curl我是否需要在req.http.host其中指定 IP 位址vcl_recv()才能停止收到 503?或我該如何指定預設虛擬主機?

答案1

首先我要說的是,在沒有看到您實際的 VCL 配置的情況下很難給出建議。

回答您的實際問題

您可以在 的開頭設定預設主機vcl_recv,請注意,您的後端應配置為回應該確切主機

sub vcl_recv {
  /* set a default host if no host is provided on the request or if it is empty */
  if ( ! req.http.host 
    || req.http.host == "") {
    set req.http.host = "your.default.host.tld";
  }
  # ...

}

我認為你不必搞亂IP req.http.host,你應該更好地使用curl將主機頭傳遞給varnish(類似於curl -H "Host: your.default.host.tld" http://XX.XX.XX.XXX/


關於這個主題的一些一般性建議:

將行為不當的控制邏輯新增至您的 VCL [1]

您的後端設定正確嗎?

請記住,清漆將使用“預設”後端(或控制器),除非指示在您的 VCl 邏輯上使用其他後端

新增運行狀況探測器並查看哪些後端出現故障

使用一致的運行狀況探測 [2] 並使用命令列命令,varnishadm debug.health請參閱文件以更好地理解 [3]

將重啟邏輯加入您的 vlc 錯誤中

像這樣的東西

sub vcl_error {
  # ...

  /* Try to restart request in case of failure */
  if (obj.status == 503 && req.restarts < 5) {
    set obj.http.X-Restarts = req.restarts;
    return(restart);
  }

  # Before any deliver
  return (deliver);
}

將偵錯邏輯新增至您的 vlc 錯誤合成回應中

vcl_fetch請記住,您可以在將後端錯誤代碼傳遞到清漆錯誤回應時添加偵錯標頭:

sub vcl_fetch {
  # ...

  set beresp.http.X-Debug-Backend-Code = beresp.status;

  # ...
}
sub vcl_error {
  # ...

  synthetic {""
  # Insert the following at the end of your current response
  <p>Backend Status code was "} + obj.http.X-Debug-Backend-Code + {"</p>
    </body>
  </html>
  "};

  # ...

  return (deliver);
}

[1]https://www.varnish-cache.org/docs/3.0/tutorial/handling_misbehaving_servers.html

[2]https://www.varnish-cache.org/docs/3.0/reference/vcl.html#backend-probes

[3]https://www.varnish-cache.org/trac/wiki/BackendPolling#CLIcommands

相關內容