This is my problem

I have 2 domain : mysite1.com and mysite2.com and one digital ocean Droplet, I installed nginx and varnish, everything work well with mysite1.com and now I want to add more domain to my droplet - mysite2.com

This is my config in default.vcl

backend default {
    .host = "127.0.0.1";
    .port = "8080";
}


backend mysite2 {
    .host = "127.0.0.1";
    .port = "8080";
}

sub vcl_recv {
    if (req.http.host == "mysite1.com" || req.http.host == "www.mysite1.com") {
        set req.backend = default;
    } elsif (req.http.host == "mysite2.com" || req.http.host == "www.mysite2.com") {
        set req.backend = mysite2;
    }
}

and now is ngix config in /etc/ngix/site-avaiable/mysite2

server {

server_name mysite2.com;
#server_name localhost;
listen 127.0.0.1:8080;
root /home/mysite2/sites/mysite2
// some bla bla here
}

My problem is whenever I try to connect to mysite2.com, everything with be move to mysite1.com.

If I turn off varnish, everthing is ok, but when I turn it on, I got this error, I think my config is not correct, but I don’t know why, any one have experience with it?

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

×
1 answer

@4checkervn

It depends on which version of Varnish you’ve installed & configured.

Try changing:

backend default {
    .host = "127.0.0.1";
    .port = "8080";
}

backend mysite2 {
    .host = "127.0.0.1";
    .port = "8080";
}

sub vcl_recv {
    if (req.http.host == "mysite1.com" || req.http.host == "www.mysite1.com") {
        set req.backend = default;
    } elsif (req.http.host == "mysite2.com" || req.http.host == "www.mysite2.com") {
        set req.backend = mysite2;
    }
}

To:

Varnish 3.x

backend default {
    .host = "127.0.0.1";
    .port = "8080";
}

sub vcl_recv {
    if (req.http.host ~ "^(www\.)?mysite1\.com$") {
        set req.backend = default;
    }

    if (req.http.host ~ "^(www\.)?mysite2\.com$") {
        set req.backend = default;
    }
}

Varnish 4.x

backend default {
    .host = "127.0.0.1";
    .port = "8080";
}

sub vcl_recv {
    if (req.http.host ~ "^(www\.)?mysite1\.com$") {
        set req.backend_hint = default;
    }

    if (req.http.host ~ "^(www\.)?mysite2\.com$") {
        set req.backend_hint = default;
    }
}

… and then restart Varnish.

The above using a regex to match the request instead of using ||. Keeps it short and sweet and only requires that you change mysite# inside the regex.

Submit an Answer