Using varnish as a HTTP Router

A Layer 7 Routing Option

So one of the novel uses I have put the Varnish Cache to is a HTTP (Layer 7) Router.

Our Setup:

We have a single IP address that forwards port 80 to a Virtual Machine. This virtual machine runs varnish. We have a whole number of virtual machines that we use for development and need to be accessible from the great wild web. How do we do this?

HTTP Router Setup

The simple solution is to setup multiple backend definitions and do if statements on the req.http.host.

backend int_dev_server_1 {
    .host = "10.1.2.1";
    .port = "80";
}

backend int_dev_server_2 {
    .host = "10.1.2.2";
    .port = "80";
}

sub vcl_recv {
	// ... your normal config stuff

	if (req.http.host ~ "^(.*)dev-server-1.example.com") {
	    set req.backend = int_dev_server_1;
	    return(pipe);
	}
	if (req.http.host ~ "^(.*)dev-server-2.example.com") {
	    set req.backend = int_dev_server_2;
	    return(pipe);
	}

}