In many cases, it may be necessary to host multiple vhosts in the same Varnish Enterprise instance. Varnish Enterprise provides a VCL label as a way to dispatch each vhosts to a separate VCL file. Then, a CLI command file can be used to automate the labeling and loading of tenant VCLs.
In this setup, in additional to tenant VCLs, an entrypoint is also required to dispatch traffic to a separate VCL file. In the below example, we’ll use main.vcl
as an entrypoint. Either server.vclConfigs
or an external ConfigMap could be used for the VCLs.
To setup multi-tenant VCL and entrypoint VCL with server.vclConfigs
, configure the following:
server:
# We need an entrypoint VCL separately from default.vcl (which is now used as a fallback VCL)
main.vcl: |
vcl 4.1
import std;
sub vcl_recv {
set req.http.host = std.tolower(req.http.host);
if (req.http.host ~ "(^|\.)example\.com(\:[0-9]+)?$") {
return (vcl(label_example));
} elseif (req.http.host ~ "(^|\.)home\.arpa(\:[0-9]+)?$") {
return (vcl(label_home));
}
}
# Define VCL for each domains
example.vcl: |
# ...
home.vcl: |
# ...
Alternatively, an external ConfigMap could also be used (see also Using an external VCL ConfigMap):
---
server:
extraVolumes:
- name: example
configMap:
name: example-vcl
- name: home
configMap:
name: home-vcl
extraVolumeMounts:
- name: example
mountPath: /etc/varnish/example.vcl
subPath: example.vcl
- name: home
mountPath: /etc/varnish/home.vcl
subPath: home.vcl
Once VCL files are configured, configure server.cmdfileConfig
for each tenant:
server:
cmdfileConfig: |
vcl.load vcl_example /etc/varnish/example.vcl
vcl.label label_example
vcl.load vcl_home /etc/varnish/home.vcl
vcl.label label_home
vcl.load vcl_main /etc/varnish/main.vcl
vcl.use vcl_main