I have one VPS serving 2 Wordpress installations. Using Varnish > Nginx > PHP-FPM Right now I have this in the conf.d folder of nginx:

domain1.com.conf domain2.com.conf

A configuration example for one domain goes like this:

server {
    server_name domain1.com www.domain1.com *.domain1.com;
    expires max;
    root /home/domain1.com;

    index index.php;

The default VCL file for Varnish:

backend default {
  .host = "";
  .port = "81";
  .connect_timeout = 600s;
    .first_byte_timeout = 600s;
    .between_bytes_timeout = 600s;

All the domains have the same listen value,

How do I make Varnish to actually serve the cached version of the right domain?

  • Varnish will serve whatever content is requested, the mechanics of the back-end server are not that important to it. It looks at the host header and the details of the request. If you want specific control over the process, in sub vcl_recv(), use a regex matching your domain against req.http.Host (and then add whatever additional processing you want - e.g. modifying cookies). You can also do the same in vcl_fetch() do the same, modifying the headers (for example) of the content that is served. – cyberx86 Dec 19 '11 at 3:58
  • This has been asked on stackoverflow, and has another resolution. Check the answer: stackoverflow.com/a/3397592 – Paul Dec 19 '11 at 18:54

To split your cache by domain name change your VCL configuration with next: add this to vcl_hash subroutine:

for varnish 2.1.x

if (req.http.host) {
 set req.hash += req.http.host;
else {
 set req.hash += server.ip;

for varnish 3.x

if (req.http.host) {
else {
  • The default.vcl (lines 84-92) already includes that (at least for Varnish 3.x) - but +1 for hashing – cyberx86 Dec 19 '11 at 6:18
  • I find that doesn't work out of the box. I may have some problems with cloudflare's caching service. I will try it now and report it back in a couple of hours. – user104531 Dec 19 '11 at 12:29
  • Cloudflare has to be disabled to don't cache the old version of your website while development. Also, having the last Varnish version helps. – user104531 Dec 19 '11 at 14:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.