Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

solution: using multiple separate varnish configurations (labels) #92

Open
iham opened this issue Sep 14, 2022 · 0 comments
Open

solution: using multiple separate varnish configurations (labels) #92

iham opened this issue Sep 14, 2022 · 0 comments

Comments

@iham
Copy link

iham commented Sep 14, 2022

according to this article, since varnish 5 you can separate configurations by using labels.

situation:

  • single machine
  • 1 varnish
  • 2 (or even more) plone projects (running in separate zope instances)
  • (optionally using load balancing)

problem:

  • not able to configure that in the recipe
  • therefore manually adapting the generated vcl file(s)

here is a small introduction how to solve this:

create a section per domain/project to achieve its varnish config you desire:
buildout.cfg

[buildout]

parts+=
    varnish-conf-1
    varnish-conf-2

[varnish-configuration-base]
recipe = plone.recipe.varnish:configuration
# whatever settings you see fit for all the generated config files
# most commonly will be stuff like
# bind =
# cookie-whitelist =
# grace-* =
# health-probe-* =
# balancer =
# whatever else ...

[varnish-conf-1]
<= varnish-configuration-base
backends =
    {domain-a}:127.0.0.1:{port-a like 8080}
    {domain-a}:127.0.0.1:{port-b like 8081}

zope2_vhm_map =
     {domain-a}:{plone instance inside zope; most often /Plone}

[varnish-conf-2]
<= varnish-configuration-base
backends =
    {domain-b}:127.0.0.1:{port-a like 8090}
    {domain-b}:127.0.0.1:{port-b like 8091}
    {domain-b}:127.0.0.1:{port-b like 8092}

zope2_vhm_map =
     {domain-b}:{plone instance inside zope; most often /Plone}

this will generate two files:

  • ./parts/varnish-conf-1/default.vcl
  • ./parts/varnish-conf-2/default.vcl

we can copy both files to /etc/varnish (assuming you are on Debian/ubuntu)

sudo cp ./parts/varnish-conf-1/default.vcl /etc/varnish/varnish-conf-1.vcl
sudo cp ./parts/varnish-conf-2/default.vcl /etc/varnish/varnish-conf-2.vcl

now lets go for the labels as mentioned in https://info.varnish-software.com/blog/one-vcl-per-domain and update/replace the /etc/varnish/default.vcl:

vcl 4.0;

# fake, never-used backend
# to silence the compiler
backend fake {
	.host = "0:0";
}
sub vcl_recv {
	if (req.http.host == "domain-a") {
		return (vcl(conf-1));
	} else if (req.http.host == "domain-b") {
		return (vcl(conf-2));
	} else {
		return (synth(404));
	}	
}

see the "vcl(conf-*)" lines?
those are the varnish labels which are loading the vcl files.

in order to make varnish do its label "magic" on each startup we need a script:
/etc/varnish/start.cli

vcl.load vcl-conf-1 /etc/varnish/conf-1.vcl
vcl.load vcl-conf-2 /etc/varnish/conf-2.vcl
vcl.label conf-1 vcl-conf-1
vcl.label conf-2 vcl-conf-2
vcl.load vcl-default /etc/varnish/default.vcl
vcl.use vcl-default

in the systemd varnish.service change the parameters to use the start.cli instead of a file to load (empty -f is necessary):

ExecStart=/usr/sbin/varnishd \
    -I /etc/varnish/start.cli \
    -f '' \
    -what-ever-params-you-have

and reload systemctl and service varnish restart.

done - no more handwritten code inside the generated varnish configs.

@iham iham changed the title using multiple separate varnish configurations (labels) solution: using multiple separate varnish configurations (labels) Sep 14, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant