Назад | Перейти на главную страницу

Varnish: используйте несколько бэкэндов в зависимости от URL

У меня есть несколько внутренних сайтов, на которые я хотел бы попасть, вставив URL-адрес с лаком. Например, если набрать

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

Однако на данный момент я не понимаю, как настроить лак. Моя текущая конфигурация выглядит следующим образом: 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;
        }          
}

Вы, возможно, имели в виду:

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;
       }          
}

В вашем коде есть ошибки:

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;
       }          
}