【问题标题】:Varnish with 2 backends, always reverts to first backend带有 2 个后端的清漆,总是恢复到第一个后端
【发布时间】:2016-02-13 23:22:20
【问题描述】:

我正在尝试设置具有 2 个后端的清漆 (4) 服务器。但是,当我尝试使用 2 个不同的 URL 访问它时,它总是恢复到 server1 的后端。以下是我的 .vcl 文件的相关部分。

后端服务器配置:

backend server1 {
    .host = "127.0.0.1";
    .port = "83";
    .connect_timeout = 600s;
    .first_byte_timeout = 600s;
    .between_bytes_timeout = 600s;
}

backend server2 {
    .host = "127.0.0.1";
    .port = "84";
    .connect_timeout = 600s;
    .first_byte_timeout = 600s;
    .between_bytes_timeout = 600s;
}

还有 vcl_recv 部分

import std;
# Respond to incoming requests.
sub vcl_recv {

        if (req.method == "BAN") {
                # Same ACL check as above:
                if (!client.ip ~ purge) {
                        return(synth(403, "Not allowed."));
                }
                ban("req.http.host == " + req.http.host +
                      " && req.url == " + req.url);

                # Throw a synthetic page so the
                # request won't go to the backend.
                return(synth(200, "Ban added"));
        } else if (req.method == "PURGE") {
                if (!client.ip ~ purge) {
                        return(synth(405,"Not allowed."));
                }
                return (purge);
        } else if (req.method != "GET" && req.method != "POST" && req.method != "DELETE" && req.method != "PUT") {
                return( synth(405, "Method not allowed"));
        }

  if (req.url ~ "^/piwik/piwik.php") {
    set req.backend_hint = server1;
    return(pass);
  } else  if  (req.url ~ "^/piwik/piwik.js") {
    set req.backend_hint = server1;
    return(pass);
  } else if (req.url ~ "^/piwik"  ) {
#    error 404 "Not found";
#     return( synth(404, "File not found"));
     set req.url = "/404-page-not-found";
  } else if (req.url ~"/node/(\d+)/(.+)$") {
#     return( synth(404, "File not found"));
     set req.url = "/404-page-not-found";
  } else if (req.url ~"/monitoring") {
#     return( synth(404, "File not found"));
#     set req.url = "/404-page-not-found";
      return(pass);
  } else if (req.url ~"^/user") {
#     return( synth(404, "File not found"));
     set req.url = "/404-page-not-found";
  } else  if (req.url ~ "^/admin") {
#     return( synth(404, "File not found"));
     set req.url = "/404-page-not-found";
  } else  if (req.url ~ "^/team") {
#     return( synth(404, "File not found"));
     set req.url = "/404-page-not-found";
  } else  if (req.url ~ "^/administer") {
#     return( synth(404, "File not found"));
     set req.url = "/404-page-not-found";
  } else  if (req.url ~ "/edit?$") {
#     return( synth(404, "File not found"));
     set req.url = "/404-page-not-found";
  } else  if (req.url ~ "/add?$") {
#     return( synth(404, "File not found"));
     set req.url = "/404-page-not-found";
  } else  if (req.url ~ "^(www\.)?server2.com$") {
     set req.backend_hint = server2;
  } else  if (req.url ~ "^(www\.)?server1.com$") {
     set req.backend_hint = server1.com;
  }

如果我将以下语句添加到 vcl_recv 部分的底部,那么我可以访问 server2 的后端:

else {
   set req.backend_hint = server2;
}

【问题讨论】:

    标签: varnish varnish-vcl varnish-4


    【解决方案1】:

    在您使用的 VLC 中

    req.url ~ "^(www\.)?server2.com$"
    

    但 req.url 不包含主机名。你想做的大概是

    req.http.host ~ "^(www\.)?server2\.com$"
    

    或许

    req.http.host ~ "^(www\.)?server2\.com$" && req.url ~ "^/$"
    

    【讨论】:

      猜你喜欢
      • 2016-09-21
      • 2017-09-12
      • 1970-01-01
      • 2017-07-21
      • 2017-11-24
      • 1970-01-01
      • 2011-01-25
      • 1970-01-01
      • 2014-10-25
      相关资源
      最近更新 更多