Can JSON config file interpolate environment variables?

The documentation on environment variables only talks about Caddyfile format:

Also, in the Placeholders section in the documentation, it suggests to use curly braces: {env.*}. It’s not clear, however, whether I can use it the same way in the JSON file? For example:

{
  "upstreams": [{"dial": "{env.MAIN_HOSTNAME}"}]
}

Will that work?

Yes, that should work. But only in config locations where the module actually executes the replacer (the internal mechanism for finding and transforming placeholders into actual values). Not all modules and config properties use the replacer. Luckily though, the upstream dial addresses do get passed through the replacer.

I think by this point most config inputs do go through a replacer where people have been known to use/want them, so that should be promising news.

Update. I tried this interpolation and it stopped working.

{
  "apps": {
    "http": {
      "servers": {
        "nextjs": {
          "listen": [":80"],
          "routes": [
            {
              "match": [{
                "host": ["{env.PRIMARY_HOST}"]
              }],
              "handle": [{
                "handler": "reverse_proxy",
                "upstreams": [{
                  "dial": "nextjs:3000"
                }]
              }]
            }
          ]
        }
      }
    }
  }
}

If I rollback to specifying the host directly, it works again.
If I use interpolation {env.PRIMARY_HOST}, the reponse Caddy gives is an absolutely empty page. The logs don’t indicate any issues. So I’m not sure how to investigate what’s going on. Ideas?

Are you sure the env var is set properly? Run ‘caddy env’ to see, or append ‘–env’ to your command to see.

This topic was automatically closed after 30 days. New replies are no longer allowed.