How to use admin config load with Caddyfile

1. The problem I’m having:

I want use Caddyfile to caddy config from a http url because I want to keep the secret http url in an environment variable (just like load/http - json/admin/config/load/http — Caddy Documentation but instead of json, I want caddyfile. I didn’t find any relevant documentation for that.

2. Error messages and/or full log output:

Not relevant.

3. Caddy version:

2.11.2

4. How I installed and ran Caddy:

From nixpkgs

5. Links to relevant resources:

load/http - json/admin/config/load/http — Caddy Documentation

Set the Content-Type header to text/caddyfile

You can have your Caddyfile pull the value from env vars, no need to use admin to load the config. See Caddyfile Concepts — Caddy Documentation you do something like {env.CLOUDFLARE_API_TOKEN}

1 Like

I meant to ask how to use Caddyfile for the config before loading from a http url.

My purpose is to have my below config file version controlled and keep the sensitive information in environment variables.

{
  "admin": {
    "listen": "{$ADMIN_LISTEN_ADDR}",
    "config": {
      "load": {
        "module": "http",
        "url": "{$CADDY_CONFIG_URL}"
      }
    }
  }
}

This is the json config that I need. This doesn’t work because I need to resolve environment variables $CADDY_CONFIG_URL and $ADMIN_LISTEN_ADDR. If I have an equivalent Caddyfile, then I can use environment variables.

There are 2 different types of environment variables, one is Caddyfile-specific and is replaced at parse-time (e.g. {$HOST}), while the other is more general and is replaced at runtime (e.g. {env.HOST}).

More is here:

3 Likes

Thanks. It works! Never knew that we can still substitute environment variables in json config.