0

I have a dedicated server centos 6 Apache I have different Magento 2 sites on the server with there own ip and domain. I have installed and configured Varnish for one site / ip running Magento 2 site and used the Varnish configuration file generated in the admin and uploaded to [Home]/etc/varnish and renamed the file as default.vcl below is the settings section.

My problem is if I try to add any more sites/ip the varnish will not restart shows failed. please can someone tell me exactly how to add more sites to the configuration file and I can fill in my ip accordingly.

Secondly I want to ask when I have different Magento 2 sites and 1 varnish when I clear cache in any site will it also clear / purge for all sites - or will they work independently

backend default {
    .host = "88.151.196.121";
    .port = "8080";
    .first_byte_timeout = 600s;
    .probe = {
        .url = "/pub/health_check.php";
        .timeout = 2s;
        .interval = 5s;
        .window = 10;
        .threshold = 5;
   }
}

acl purge {
    "localhost";
}

sub vcl_recv {
    if (req.method == "PURGE") {
        if (client.ip !~ purge) {
            return (synth(405, "Method not allowed"));
        }

2
  • Are these Magento sites hosted on the same machine and are they also served by 88.151.196.121? If so, no changes are required, because Varnish will use the Host header and the URL to identify objects in cache. The same applies to clearing the cache. Aug 24, 2020 at 8:16
  • Thanks - all sites are Magento 2 and hosted on same server - the servers main ip is 88.151.196.121 and one magento 2 site is running on this ip - another Magento 2 site is running on ip address 88.151.196.123 - but as I mentioned varnish only seems to work on 1 site and I have to enter that ip in the configuration file - so confused
    – jt9489
    Aug 24, 2020 at 14:22

1 Answer 1

2

Based on your comment, I can conclude that the 2 Magento sites are served on 2 different IP addresses.

These sites will be defined as 2 separate backends, because their IP address differs.

Let's take these backend definitions as an example:

backend backend1 {
    .host = "88.151.196.121";
    .port = "8080";
    .first_byte_timeout = 600s;
    .probe = {
        .url = "/pub/health_check.php";
        .timeout = 2s;
        .interval = 5s;
        .window = 10;
        .threshold = 5;
   }
}
backend backend2 {
    .host = "88.151.196.123";
    .port = "8080";
    .first_byte_timeout = 600s;
    .probe = {
        .url = "/pub/health_check.php";
        .timeout = 2s;
        .interval = 5s;
        .window = 10;
        .threshold = 5;
   }
}

Varnish has now way of knowing which backend to pick. If both sites were hosted on the same IP address, Varnish would use the value of the Host header to determine the various sites.

In this case, we'll need to create a backend hint to tell Varnish when to pick which backend. We could use hostname matching for this.

This means you'll have to add the following VCL code:

sub vcl_recv {
    if(req.http.host ~ "(www\.)?example.com") {
        set req.backend_hint = backend1;
    } else {
        set req.backend_hint = backend2;
    }
}

Because this is VCL, you can tailor these routing rules to your exact needs.

2
  • thank you for your kind reply please can you clarify this line I keep same or need to enter domain "(www\.)?example.com"
    – jt9489
    Aug 24, 2020 at 18:17
  • The ~ operator in VCL is there to match regular epxressions. In this case the regex is "(www\.)?example.com", which matches www.example.com and example.com. You can put your own domain names in there. You can also use == to match the string and use the || operator to match multiple domains instead of using regexes. Aug 25, 2020 at 8:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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