0

I have several internal sites, which I would like to reach by inserting a url with varnish. For example, if typed

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

At the moment, however, I do not understand how to configure varnish. My current configuration is as follows: 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;
        }          
}
Dennis Nolte
  • 2,848
  • 4
  • 26
  • 36

2 Answers2

1

You maybe meant:

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

Either way you have to rewrite Host header as well. And use proper regexes:

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;
       }          
}
Danila Vershinin
  • 4,738
  • 3
  • 16
  • 21
  • Hi Daniel, thanks for reply. But I need to show the original address from where the client come. For example: http://www.example.com/serv1/--->http://192.168.0.1/application1--> I need to see http:///www.example.com/serv1/ and not to show the internal backend server address. – Riccardo Grassini Sep 26 '17 at 20:43
  • Hi Riccardo, Varnish will do exactly that (and config above). If you're seeing different - then most likely you have a redirect in your application that bears ```192.168.0.1``` in ```Location``` header. – Danila Vershinin Sep 26 '17 at 22:36
  • Thanks to Daniel my problem was in fact the header of the application, which redirected the wrong path, creating a mistake! – Riccardo Grassini Oct 03 '17 at 08:01
0

You have some mistakes in your code:

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;
       }          
}
  • You should highlight, and better yet, explain the mistakes, rather than posting an uncommented fix. This will help everybody to learn, understand and avoid future mistakes. – Roman Feb 18 '20 at 16:09