Environment variables in VCL

Guillaume Quintard guillaume at varnish-software.com
Tue Sep 10 05:38:44 UTC 2019


Hi,

The trouble is that the backends are C structures created when you compile
the VCL, but std.getenv() is called when you use the compile VCL, so that
way too late.

I see two ways:
- use a dynamic backend vmod (like vmod_goto), allowing you to regularly
interrogate DNS a build an elastic director
- create a static backend point to you k8s proxy, and just set
req.http.host to whatever value you want.

The last one is two of course create a template, but remember that varnish
resolves domain names in static backend when it compiles the VCL, so the
resolved IP may not be right all the time.
-- 
Guillaume Quintard


On Tue, Sep 10, 2019 at 2:17 AM Bender, Charles <charles at beachcamera.com>
wrote:

> Looking at a VCL I use to set hostname and this seems to work-
>
>
>
> sub vcl_deliver {
>
>   set resp.http.X-Cache-Node = server.hostname;
>
> }
>
>
>
> Maybe you can use in form of server.environmental_variable
>
>
>
> *From: *varnish-misc <varnish-misc-bounces+charles=
> beachcamera.com at varnish-cache.org> on behalf of Hugues Alary <
> hugues at betabrand.com>
> *Date: *Monday, September 9, 2019 at 6:09 PM
> *To: *varnish-misc <varnish-misc at varnish-cache.org>
> *Subject: *Environment variables in VCL
>
>
>
> Hi there,
>
>
>
> I'm using Varnish in a Kubernetes cluster and my configuration needs to
> use environment variables.
>
>
>
> More specifically, I'm trying to declare a `backend` with a `.host` whose
> value is an environment variable.
>
>
> backend b0 {
>   .host = "*$ENVIRONMENT_VARIABLE_HOSTNAME*";
>   .port = "80";
>   .connect_timeout = 300s;
>   .first_byte_timeout = 300s;
>   .between_bytes_timeout = 300s;
>
> }
>
>
>
> This doesn't work. It tried using the std.get_env() function but it
> doesn't work in this context.
>
>
>
> *Is there any way to use environment variables in VCL, or do I have to
> create a configuration template?*
>
>
>
> Thanks for your help,
>
>
>
> Cheers,
>
> -Hugues
>
>
> _______________________________________________
> varnish-misc mailing list
> varnish-misc at varnish-cache.org
> https://www.varnish-cache.org/lists/mailman/listinfo/varnish-misc
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://www.varnish-cache.org/lists/pipermail/varnish-misc/attachments/20190910/15f9a551/attachment.html>


More information about the varnish-misc mailing list