same ip address for backend config.

Michael Alger varnish at mm.quex.org
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 = "1.2.3.4";
>       .header="www.abc.com";
>       .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
server.

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. http://example.com/home and http://www.example.com/home
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
req.http.host (i.e. the Host: header sent by the client) and implement
your own logic as required:

  if (req.http.host == "abc.com")
  {
    set req.http.host = "www.abc.com";
  }
  else if (req.http.host == "def.com")
  {
    set req.http.host = "www.def.com";
  }

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
strategies.

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

  if (req.http.host ~ "(?i)^(www\.)?abc\.com")
  {
    set req.http.host = "www.abc.com";
  }
  else if (req.http.host ~ "(?i)^(www\.)?def\.com")
  {
    set req.http.host = "www.def.com";
  }

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
page.

Note that the above patterns aren't 100% explicit, and will match
requests with hosts like "www.abc.company.com" as if they were for
"www.abc.com", 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