Configuration varnish cache for multiple domain

Hari Metkari harimetkari at gmail.com
Mon Jan 24 05:40:35 CET 2011


Hi,


I am implementing varnish cache for multiple seprate domain.i.e sitetwo.comand
site3.com

I am configured varnish cache on varnish server local ip address
(192.168.126.30)
and my one site ip address 192.168.126.20 and two site ip address
192.168.126.60

I added multiple backend in VCL file.when I hit site one it calls to backend
of site two instead of backend one.
for single domain it's working fine but multiple domain not work.

Please help me below some varnish cache configuration files.
1)/etc/sysconfig/varnish  file


 VARNISH_VCL_CONF=/etc/varnish/default.vcl
#
# # Default address and port to bind to
# # Blank address means all IPv4 and IPv6 interfaces, otherwise specify
# # a host name, an IPv4 dotted quad, or an IPv6 address in brackets.
 VARNISH_LISTEN_ADDRESS=192.168.126.30
#localhost  default by hari
 VARNISH_LISTEN_PORT=80
#6081 #by default by hari
#
# # Telnet admin interface listen address and port
 VARNISH_ADMIN_LISTEN_ADDRESS=192.168.126.30
#127.0.0.1 #by default by hari
 VARNISH_ADMIN_LISTEN_PORT=6082
#
# # The minimum number of worker threads to start
 VARNISH_MIN_THREADS=1
#
# # The Maximum number of worker threads to start
 VARNISH_MAX_THREADS=1000
#
# # Idle timeout for worker threads
 VARNISH_THREAD_TIMEOUT=120
#
# # Cache file location
 VARNISH_STORAGE_FILE=/var/lib/varnish/varnish_storage.bin
#
# # Cache file size: in bytes, optionally using k / M / G / T suffix,
# # or in percentage of available disk space using the % suffix.
 VARNISH_STORAGE_SIZE=1G
#
# # Backend storage specification
 VARNISH_STORAGE="file,${VARNISH_STORAGE_FILE},${VARNISH_STORAGE_SIZE}"
#
# # Default TTL used when the backend does not specify one
 VARNISH_TTL=120
#
# # DAEMON_OPTS is used by the init script.  If you add or remove options,
make
# # sure you update this section, too.
 DAEMON_OPTS="-a ${VARNISH_LISTEN_ADDRESS}:${VARNISH_LISTEN_PORT} \
              -f ${VARNISH_VCL_CONF} \
              -T
${VARNISH_ADMIN_LISTEN_ADDRESS}:${VARNISH_ADMIN_LISTEN_PORT} \
              -t ${VARNISH_TTL} \
              -w
${VARNISH_MIN_THREADS},${VARNISH_MAX_THREADS},${VARNISH_THREAD_TIMEOUT} \
              -u varnish -g varnish \
              -s ${VARNISH_STORAGE}"
#
2) /etc/varnish/default.vcl  file

backend site2 {
      .host = "sitetwo.com";
      .port = "8081";
}
backend site3 {
      .host = "site3.com";
      .port = "8080";
}
sub vcl_recv {
    if (req.request != "GET" &&
      req.request != "HEAD" &&
      req.request != "PUT" &&
      req.request != "POST" &&
      req.request != "TRACE" &&
      req.request != "OPTIONS" &&
      req.request != "DELETE") {
#        /* Non-RFC2616 or CONNECT which is weird. */
        return (pipe);
    }
    if (req.request != "GET" && req.request != "HEAD") {
#        /* We only deal with GET and HEAD by default */
        return (pass);
    }
    if (req.http.Authorization || req.http.Cookie) {
#        /* Not cacheable by default */
        return (pass);
    }
if (req.http.host == "site3.com") {
        #You will need the following line only if your backend has multiple
virtual host names
        set req.http.host = "site3.com";
        set req.http.X-Orig-Host = req.http.host;
        set req.backend = site3;
        return (lookup);
}
if (req.http.host == "sitetwo.com") {
        #You will need the following line only if your backend has multiple
virtual host names
        set req.http.host = "sitetwo.com";
        set req.http.X-Orig-Host = req.http.host;
        set req.backend = site2;
        return (lookup);
}
    return (lookup);
}
#
sub vcl_pipe {
#    # Note that only the first request to the backend will have
#    # X-Forwarded-For set.  If you use X-Forwarded-For and want to
#    # have it set for all requests, make sure to have:
#    # set req.http.connection = "close";
#    # here.  It is not set by default as it might break some broken web
#    # applications, like IIS with NTLM authentication.
    return (pipe);
}
#
sub vcl_pass {
    return (pass);
}
#
sub vcl_hash {
    set req.hash += req.url;
    if (req.http.host) {
        set req.hash += req.http.host;
    } else {
        set req.hash += server.ip;
    }
    return (hash);
}
#
sub vcl_hit {
    if (!obj.cacheable) {
        return (pass);
    }
    return (deliver);
}
#
sub vcl_miss {
    return (fetch);
}
sub vcl_fetch {
    if (!obj.cacheable) {
        return (pass);
    }
    if (obj.http.Set-Cookie) {
        return (pass);
    }
    ##set obj.prefetch =  30s;
#The following vcl code will make Varnish serve expired objects. All object
will be kept up to two minutes past their expiration time or a fresh object
is generated
    set obj.grace = 2m;
    return (deliver);
}
#
sub vcl_deliver {
    return (deliver);
}

Thanks in Advanced


Thanks,
Hari Metkari
+91-9881462183
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://www.varnish-cache.org/lists/pipermail/varnish-misc/attachments/20110124/7815710b/attachment-0003.html>


More information about the varnish-misc mailing list