1

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"));
 }
asked Aug 20, 2020 at 17:31
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. Commented 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 Commented Aug 24, 2020 at 14:22

1 Answer 1

3

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.

answered Aug 24, 2020 at 15:20
4
  • thank you for your kind reply please can you clarify this line I keep same or need to enter domain "(www\.)?example.com" Commented 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. Commented 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. Commented Dec 12, 2023 at 20:49
  • @wjp_bill the Host header will handle that issue. No need to define a separate backend. As long as each site's domain is reflected through the Host header, things will be fine. Commented Dec 13, 2023 at 12:21

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.