Varnish:根据 URL 使用多个后端

Varnish:根据 URL 使用多个后端

我有几个内部网站,我想通过使用 varnish 插入 URL 来访问它们。例如,如果输入

    http://www.example.com/serv1/--->http://192.168.0.1/application1
    http://www.example.com/serv2/--->http://192.168.0.1/application2

不过目前我还不清楚如何配置varnish,我目前的配置如下:default.vlc

    vcl 4.0;

    backend vm1 {
        .host = "www.example.com";
        .port = "81";
        .connect_timeout = 6000s;
        .first_byte_timeout = 6000s;
        .between_bytes_timeout = 6000s;
    }

    backend serv1 {
        .host = "192.168.0.1";
        .port = "80";
        .connect_timeout = 6000s;
        .first_byte_timeout = 6000s;
        .between_bytes_timeout = 6000s;
    }

    backend serv2 {
        .host = "192.168.0.3";
        .port = "80";
        .connect_timeout = 6000s;
        .first_byte_timeout = 6000s;
        .between_bytes_timeout = 6000s;
    }

    sub vcl_recv {
           if (req.url == "^/serv1/*$") {
               set req.url = regsub(req.url, "^/serv1/*$","/application1");
               set req.backend_hint = serv1;
           } else {
        set req.backend_hint = vm1;
        }

           if (req.url == "^/serv2/*$") {
               set req.url = regsub(req.url, "^/serv1/*$","/application2");
               set req.backend_hint = serv2;
           } else {
        set req.backend_hint = vm1;
        }          
}

答案1

你的意思可能是:

http://www.example.com/serv1/--->http://192.168.0.1/application1
http://www.example.com/serv2/--->http://192.168.0.3/application2

无论哪种方式,您都必须重写Host标题。并使用正确的正则表达式:

vcl 4.0;

backend vm1 {
    .host = "www.example.com";
    .port = "81";
    .connect_timeout = 6000s;
    .first_byte_timeout = 6000s;
    .between_bytes_timeout = 6000s;
}

backend serv1 {
    .host = "192.168.0.1";
    .port = "80";
    .connect_timeout = 6000s;
    .first_byte_timeout = 6000s;
    .between_bytes_timeout = 6000s;
}

backend serv2 {
    .host = "192.168.0.3";
    .port = "80";
    .connect_timeout = 6000s;
    .first_byte_timeout = 6000s;
    .between_bytes_timeout = 6000s;
}

sub vcl_recv {
       if (req.url == "^/serv1($|/)") {
           set req.url = regsub(req.url, "^/serv1","/application1");
           set req.http.host = "192.168.0.1";
           set req.backend_hint = serv1;
       } else {
           set req.backend_hint = vm1;
       }

       if (req.url == "^/serv2($|/)") {
           set req.url = regsub(req.url, "^/serv2","/application2");
           set req.http.host = "192.168.0.3";
           set req.backend_hint = serv2;
       } else {
          set req.backend_hint = vm1;
       }          
}

答案2

您的代码中存在一些错误:

vcl 4.0;

backend vm1 {
    .host = "www.example.com";
    .port = "81";
    .connect_timeout = 6000s;
    .first_byte_timeout = 6000s;
    .between_bytes_timeout = 6000s;
}

backend serv1 {
    .host = "192.168.0.1";
    .port = "80";
    .connect_timeout = 6000s;
    .first_byte_timeout = 6000s;
    .between_bytes_timeout = 6000s;
}

backend serv2 {
    .host = "192.168.0.3";
    .port = "80";
    .connect_timeout = 6000s;
    .first_byte_timeout = 6000s;
    .between_bytes_timeout = 6000s;
}

sub vcl_recv {

       set req.backend_hint = vm1;                                        // Default backend

       if (req.url ~ "^/serv1(/.*)?$") {                                  // ~ operator
           set req.url = regsub(req.url, "^/serv1","/application1");
           set req.backend_hint = serv1;
       }

       if (req.url ~ "^/serv2(/.*)?$") {                                  // ~ operator
           set req.url = regsub(req.url, "^/serv2","/application2");
           set req.backend_hint = serv2;
       }          
}

相关内容