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"));
 }
1 Answer 1
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 no 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.
- 
 thank you for your kind reply please can you clarify this line I keep same or need to enter domain "(www\.)?example.com"jt9489– jt94892020年08月24日 18:17:17 +00:00Commented 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 matcheswww.example.comandexample.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.Thijs Feryn– Thijs Feryn2020年08月25日 08:06:48 +00:00Commented Aug 25, 2020 at 8:06
- 
 What would you do if both sites are on the same IP? I've got the same problem with 2 sites in Plesk and I can't get the cache to flush in varnish without restarting it.wjp_bill– wjp_bill2023年12月12日 20:49:52 +00:00Commented Dec 12, 2023 at 20:49
- 
 @wjp_bill theHostheader will handle that issue. No need to define a separate backend. As long as each site's domain is reflected through theHostheader, things will be fine.Thijs Feryn– Thijs Feryn2023年12月13日 12:21:18 +00:00Commented Dec 13, 2023 at 12:21
88.151.196.121? If so, no changes are required, because Varnish will use theHostheader and the URL to identify objects in cache. The same applies to clearing the cache.