Search code examples
dockervarnishvarnish-vcl

Cannot use std vmod in varnish 6.5.1


I am using the official varnish:6.5.1 docker image and have this vcl:

vcl 4.0;

import std;

backend default {
    .host = std.getenv("PROXY_HOST");
    .port = std.getenv("PROXY_PORT");
}
.....

When I try to run the image (with docker-compose) it instantly fails with this error:

varnish_1  | Could not delete 'vcl_boot.1612728251.581028/vgc.sym': No such file or directory
varnish_1  | Error:
varnish_1  | Message from VCC-compiler:
varnish_1  | Expected CSTR got 'std'
varnish_1  | (program line 369), at
varnish_1  | ('/etc/varnish/default.vcl' Line 13 Pos 17)
varnish_1  |         .host = std.getenv("PROXY_HOST");

How is this failing? I would understand not being able to connect to the backend but the VCL parse should be fine, the documentation on the std VMOD is very simple for getenv.

What am I missing here?

EDIT

backend default {
    .host = "${PROXY_HOST}";
    .port = "${PROXY_PORT}";
}

in combination with

#!/bin/bash
envs=`printenv`

for env in $envs
do
    IFS== read name value <<< "$env"

    sed -i "s|\${${name}}|${value}|g" /etc/varnish/default.vcl
done

varnishd -f /etc/varnish/default.vcl

works as per this post but that seems hardly optimal.


Solution

  • Varnish Cache only supports static backends

    Varnish Cache, the open source version of Varnish doesn't support dynamic backends.

    When the VCL file is loaded and compiled, the .host and .port values need to be strings, not expressions.

    The error message also indicates this:

    Expected CSTR got 'std'
    

    The compiler says it expects a C-string, which means something that starts with a quote, but instead it found std.

    Dynamic backend support in Varnish Enterprise

    Varnish Enterprise, the commercial version of Varnish, does support dynamic backends. See https://docs.varnish-software.com/varnish-cache-plus/vmods/goto/ for more information.

    Disclaimer: although Varnish Enterprise is the commercial version of Varnish Cache, you can still use it without upfront license payments if you use it on AWS, Azure or GCP.