same ip address for backend config.

Michael Alger varnish at
Sat Jul 2 09:54:06 CEST 2011

On Sat, Jul 02, 2011 at 07:32:53AM +0900, Calvin Park wrote:
> Can I add "Host header" using same ip address ?
> Thanks.
> eg.
> backend svr1 {
>       .host = "";
>       .header="";
>       .port = "80";
> }

Yes you can, but not like that.

If your backend server is configured for name-based virtual hosting,
then you don't actually _need_ to do anything. When the browser sends
the request, it will include the Host: header saying which hostname it
thinks it's talking to. Varnish will send the same request headers to
your backend, which will then be able to select the correct virtual

You only need to use multiple backend definitions if your site(s)
content is served by multiple servers on different IP addresses and/or
ports; or if you want to use different parameters for some requests
(e.g. connection timeouts).

Even though you don't need to do anything special, it's still a good
idea to "normalize" the host header to prevent duplicate objects being
cached. e.g. and
probably refer to the same object, but are two different URLs and will
be cached independently.

To prevent that, in your vcl_fetch() you can look at the value of (i.e. the Host: header sent by the client) and implement
your own logic as required:

  if ( == "")
    set = "";
  else if ( == "")
    set = "";

You can also use this method to implement different logic for different
host headers, e.g. selecting a different backend (if you have multiple
backend servers on different IPs), or using different caching

Since some UAs may append the port number to the hostname even if it's
making the request on port 80 (i.e. "Host:") it's
probably a good idea to use patterns of this form:

  if ( ~ "(?i)^(www\.)?abc\.com")
    set = "";
  else if ( ~ "(?i)^(www\.)?def\.com")
    set = "";

You might also want to have a final else clause to catch any requests
which don't include a (recognised) Host header and send them an error

Note that the above patterns aren't 100% explicit, and will match
requests with hosts like "" as if they were for
"", but generally that doesn't matter and won't cause any
security risks. You might need to make the pattern a bit more explicit
if you do happen to host other sites with hostnames which share the same
prefix (or, put the longer one first in your comparisons).

More information about the varnish-misc mailing list