2

I have several backends set up, each one is detected in the VCL and then the correct set up applied. This works for some of the sites, but a few get pointed to the same site.

I have read this and applied this, it works for some, but not for others.

Configure multiple sites with Varnish

For example I have some backends defined:

backend site4 {
    .host = "127.0.0.1";
    .port = "81";
}

backend site3 {
    .host = "127.0.0.1";
    .port = "81";
}

backend site2 {
    .host = "127.0.0.1";
    .port = "81";
}

backend site1 {
    .host = "127.0.0.1";
    .port = "81";
}

Then in vcl_recv I set the host and the correct backend:

if (req.http.host ~ "(?i)^(www.)?site1.co.uk") 
{
    set req.http.host = "www.site1.co.uk";
    set req.backend = site1;   
} 
elsif (req.http.host ~ "(?i)^(www.)?site2.co.uk") 
{
    set req.http.host = "www.site2.co.uk";
    set req.backend = site2;   
} 
elsif (req.http.host ~ "(?i)^(www.)?site3.com") 
{
    set req.http.host = "www.site3.com";
    set req.backend = site3;
} 
elsif (req.http.host ~ "(?i)^(www.)?site4.net") 
{
    set req.http.host = "www.site4.net";
    set req.backend = site4;
} 

www.site1.co.uk works, as does www.site2.co.uk, but www.site3.com and www.site4.net both show the content for www.site2.co.uk - it's like its ignoring my backend and just using the same one for certain sites.

The host is set correctly, because if I do:

curl -I http://site3.com

when using the following in vcl_fetch

set beresp.http.X-Host = req.http.host;  

I can see the host is correctly set as www.site3.com

Any ideas anyone?

Side note, is there a shorter method of defining my backends?

Community
  • 1
  • 1
Jake N
  • 10,535
  • 11
  • 66
  • 112

3 Answers3

2

Btw, if you have sites on same backend server, then you don't need to define new backend of each one.

ghloogh
  • 1,634
  • 16
  • 9
1

EDIT: My first answer was wrong. I misunderstood the question.

What I suggest you, is to debug and understand why it's different depending the backend :

  • disable all cache return pass; in vcl_recv after your if statement
  • run varnishlog when you run the requests

It should help

Doomsday
  • 2,650
  • 25
  • 33
0

For the sites that had not worked I had not added the new Apache port to the VirtualHost entry

I had

<VirtualHost *:80>

It needed to be

<VirtualHost *:80 *:81>

To work on the new Apache port

Jake N
  • 10,535
  • 11
  • 66
  • 112