Setting up multiple sites using varnish

We have a server that should serve several domains, although varnish, for example. example1.com, example2.com and example3.com

Our current .vcl file is as follows:

sub vcl_recv { set req.http.Host = "example1.com"; lookup; } 

How to set the correct req.http.Host for the correct incoming request?

+44
caching configuration varnish
Jul 26 '10 at 10:35 on
source share
4 answers

You can support multiple front-end domains this way:

  backend example1 { .host = "backend.example1.com"; .port = "8080"; } backend example2 { .host = "backend.example2.com"; .port = "8080"; } sub vcl_recv { if (req.http.host == "example1.com") { #You will need the following line only if your backend has multiple virtual host names set req.http.host = "backend.example1.com"; set req.backend = example1; return (lookup); } if (req.http.host == "example2.com") { #You will need the following line only if your backend has multiple virtual host names set req.http.host = "backend.example2.com"; set req.backend = example2; return (lookup); } } 
+79
Jul 30 '10 at 16:02
source share
β€” -

I use a setting similar to Cristian's, but in cases where I match req.http.host against the regex:

 #for www.example.com or example.com if (req.http.host ~ "^(www\.)?example\.com$") { set req.backend = example_com; return (lookup); } #with any subdomain support if (req.http.host ~ "^(.*\.)?example2\.com$") { set req.backend = example2_com; return (lookup); } 

Remember to install the servers correctly.

+24
Aug 03 2018-10-03T00:
source share

failed to add comment so we go

minor modification for varnish 4

 #for www.example.com or example.com if (req.http.host ~ "^(www\.)?example\.com$") { set req.backend_hint = example_com; return (lookup); } #with any subdomain support if (req.http.host ~ "^(.*\.)?example2\.com$") { set req.backend_hint = example2_com; return (lookup); } 

replace backend with backend_hint

+6
Aug 6 '15 at 16:00
source share

I would like to add a little more detail to both Cristian Vidmar and msurovcak posts

"(req.http.host ==" example1.com ")" Template:

We used the described template to host tens to hundreds of sites on the server.

You can continue the custom rules for specific sites throughout your configuration (vcl_fetch / vcl_backend_response, vcl_hash, etc.) with

if (req.http.host == "example1.com") {

an example wherever it is needed.

Combine this with the template engine to allow specific clients to be managed using separate files that contain their own logic (they are all tied to a specific site if the blocks isolate the code).

Then you include each individual site block in the default.vcl file using:

include "/etc/varnish/www.example1.com.vcl";

Additional extension for complete separation of backends:

If you host completely different websites, then splitting the backend (and splitting the cache) is a good way.

If the sites are similar (same code base / js / css / images), it may be interesting to run a resource domain, for example. resources.example.com that all sites use.

Then you can have one cache (and a very high hit rate) for each of the common elements of several sites and still maintain the differences on individual www sites.

Another alternative to using split rear ends:

Another option is to split Varnish instances through containers. Each of them becomes its own isolated world, which is governed (and lives and dies) individually. This can be a good security option, and the overhead of several processes is minimal for modern infrastructure.

Some of the benefits of this are that you can support different versions of varnishes and different varnish launch options for each instance.

This can be useful for individual logging, the use of different ESI modes for each instance, and individual memory configuration / settings.

We do this at www.section.io , and it also makes it possible to launch different containers in different geographical locations or in the same containers in different places in order to get as close to geographically dispersed user bases as possible.

+4
Aug 6 '15 at 2:36
source share



All Articles