Trying to use Environment Variables in Caddyfile and failing

1. The problem I’m having:

Im trying to use environment variables to fill trusted_proxies but not having any luck

2. Error messages and/or full log output:

My Caddyfile is as follows:

{

        {$CADDY_GLOBAL_OPTIONS}
}

Running the following I get an error


/srv # CADDY_GLOBAL_OPTIONS="servers {\ntrusted_proxies static 127.0.0.1/8\n}" caddy adapt
2025/10/23 21:44:27.468 INFO    using adjacent Caddyfile
Error: parsing caddyfile tokens for 'servers': wrong argument count or unexpected line ending after 'static', at Caddyfile:3

If I change my Caddyfile to be

{
        servers {
                trusted_proxies static 127.0.0.1/8
        }
}

then run

/srv # caddy adapt
2025/10/23 21:45:48.093 INFO    using adjacent Caddyfile
{}
2025/10/23 21:45:48.094 WARN    caddyfile       Caddyfile input is not formatted; run 'caddy fmt --overwrite' to fix inconsistencies    {"file": "Caddyfile", "line": 2}

It works fine

Is there something Im missing about how to include envs into Caddyfiles?

I’ve tried this with both “\n” and no “\n”

Of note it also works with:

/srv # CADDY_GLOBAL_OPTIONS="debug" caddy adapt
2025/10/23 21:52:42.770 INFO    using adjacent Caddyfile
{"logging":{"logs":{"default":{"level":"DEBUG"}}}}

and

/srv # CADDY_GLOBAL_OPTIONS="debug\
> " caddy adapt
2025/10/23 21:54:01.754 INFO    using adjacent Caddyfile
{"logging":{"logs":{"default":{"level":"DEBUG"}}}}

but not

/srv # CADDY_GLOBAL_OPTIONS="servers {\
> trusted_proxies 127.0.0.1/8\
> }" caddy adapt
2025/10/23 21:55:01.759 INFO    using adjacent Caddyfile
Error: parsing caddyfile tokens for 'servers': wrong argument count or unexpected line ending after '127.0.0.1/8}', at Caddyfile:3

3. Caddy version:

v2.10.2

4. How I installed and ran Caddy:

Docker

{
 	{$CADDY_GLOBAL_OPTIONS}
}

:80 {
	respond "Alive!"
}
$ CADDY_GLOBAL_OPTIONS='servers {
    trusted_proxies static 127.0.0.1/8
}' caddy adapt
2025/10/24 23:41:07.665	INFO	using adjacent Caddyfile
{"apps":{"http":{"servers":{"srv0":{"listen":[":80"],"routes":[{"handle":[{"body":"Alive!","handler":"static_response"}]}],"trusted_proxies":{"ranges":["127.0.0.1/8"],"source":"static"}}}}}}
2025/10/24 23:41:07.667	WARN	caddyfile	Caddyfile input is not formatted; run 'caddy fmt --overwrite' to fix inconsistencies	{"file": "Caddyfile", "line": 8}
$ CADDY_GLOBAL_OPTIONS='servers {
    trusted_proxies static 127.0.0.1/8
}' caddy run
$ jq . autosave.json
{
  "apps": {
    "http": {
      "servers": {
        "srv0": {
          "listen": [
            ":80"
          ],
          "routes": [
            {
              "handle": [
                {
                  "body": "Alive!",
                  "handler": "static_response"
                }
              ]
            }
          ],
          "trusted_proxies": {
            "ranges": [
              "127.0.0.1/8"
            ],
            "source": "static"
          }
        }
      }
    }
  }
}
1 Like

You can also do:

export CADDY_GLOBAL_OPTIONS="$(cat <<'EOF'
servers {
    trusted_proxies static 127.0.0.1/8
}
EOF
)"
$ docker run -d \
  -v "$PWD/Caddyfile:/etc/caddy/Caddyfile" \
  -e CADDY_GLOBAL_OPTIONS="$CADDY_GLOBAL_OPTIONS" \
  caddy:latest

or in docker-compose.yaml:

environment:
  CADDY_GLOBAL_OPTIONS: |
    servers {
        trusted_proxies static 127.0.0.1/8
    }
1 Like

Thanks!

Those work! I still cant get my specific setup to work but thats because im trying to set what you’ve done in the Dockerfile as an ENV setting and for whatever reason it doesnt translate but thats something I’ll have to work around

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