Configure multiple sites with Varnish

You can support multiple frontend domains this way:

 backend example1 {
     .host = "backend.example1.com";
     .port = "8080";
 }
 backend example2 {
      .host = "backend.example2.com";
      .port = "8080";
 }
 sub vcl_recv {
    if (req.http.host == "example1.com") {
        #You will need the following line only if your backend has multiple virtual host names
        set req.http.host = "backend.example1.com";
        set req.backend = example1;
        return (lookup);
    }
    if (req.http.host == "example2.com") {
        #You will need the following line only if your backend has multiple virtual host names
        set req.http.host = "backend.example2.com";
        set req.backend = example2;
        return (lookup);
    }
 }

I'm using setup similar to Cristian's, but in if clauses I match req.http.host against regular expression:

#for www.example.com or example.com
if (req.http.host ~ "^(www\.)?example\.com$") {
        set req.backend = example_com;
        return (lookup);
}

#with any subdomain support
if (req.http.host ~ "^(.*\.)?example2\.com$") {
        set req.backend = example2_com;
        return (lookup);
}

Don't forget to set backends appropriately!