Using Varnish to redirect to other web server's subfolders

Guru Meditation (?)

Assuming you have a web server giving the main content of your site (example.com for this post); but, there's another site hosted in a different web server (different.com/checkthis) and you want that everything inside checkthis folder be served inside example.com (i.e. example.com/checkthis/index.html).

Since example.com is a heavy site; there is a Varnish server in front of it to make it faster to users; so, you can use the same varnish to make the necessary redirections.

Your initial varnish config file is similar to this:

backend default {
.host = "172.16.1.5";
.port = "80"
}
sub vcl_recv {
set req.backend_hint = default;
}

To get your goal; you should define a new backend and then some conditions to redirect requests to the different.com site backend. You can add a backend server like this:

backend checkthis {
.host = "172.16.1.6";
.port = "80";
}

And then, to use that backend; you should edit vcl_recv function to be like this:

sub vcl_recv {
if (req.url ~ "^checkthis" ||
req.url ~ "^checkthis/") {
set.req.backend_hint = checkthis;
}
else {
set req.backend_hint = default;
}
}

And that's all. When you restart/reload the varnish server; your new functionality will be working as expected.

Hope it helps :D