Nginx – Varnish with separate backends and multiple domains on each backend


I'm using 3 nginx servers with multiple domains(sites) on each. Each server contains multiple WordPress sites.

Is it possible to configure Varnish and use separate backends (different IP's) and each backend has multiple domains(sites)?

I've tried to configure Varnish and use multiple backends, but the problem is that domains(sites) on Server-2 and Server-3 doesn't route properly. Frontpage shows up, but sub-pages is routed to Server-1.

I'm using Varnish 4 and this template with some modifications.

Not sure if round_robin is the right solution because I thought that was for identical servers in sync.

Configuring Varnish 4 and only 1 backend server works. But with multiple backend servers and domains it fails…

Thanks in advance!

Best Answer

In sub vcl_recv, you can use something like this :

if ( = "") {
    set req.backend_hint = domain1;
} elsif ( = "") {
    set req.backend_hint = domain2;
} elsif ( = "") {
    set req.backend_hint = domain3;