[Nope, Thread OP wrong] Variables in Caddyfile broken?

1. Output of caddy version:

latest caddy docker image as of this post

2. How I run Caddy:


a. System environment:

Arch, main docker image, through compose

b. Command:

docker-compose up -d

c. Service/unit/compose file:

    image: caddy:latest
    container_name: caddy
    restart: unless-stopped
      - "80:80"
      - "443:443"
      - "443:443/udp"
      - ${FOLDER_FOR_CONFIGS:?err}/caddy/Caddyfile:/etc/caddy/Caddyfile
      - ${FOLDER_FOR_CONFIGS:?err}/caddy/data:/data
      - ${FOLDER_FOR_CONFIGS:?err}/config:/config
      - /mnt/vps/var/lib/caddy:/vps-caddy

d. My complete Caddy config:

vars {                                                                                                                                   lcl ""                                                                                                                                                                                                                                                  edn "home.monke.fail"                                                                                                                                                                                                                                             evip ""                                                                                                         }                                                                                                                                                                                                                                                                 # Globals
{                                                                                                                                        debug true                                                                                                                       log {                                                                                                                                    level debug                                                                                                                      output file /data/caddy.log {                                                                                                            roll_size 10mb                                                                                                                   roll_keep 10
                        roll_keep_for 720h                                                                                                       }                                                                                                                        }                                                                                                                                storage file_system /vps-caddy/.local/share/caddy                                                                        }                                                                                                                                                                                                                                                                 home.monke.fail, *.home.monke.fail {                                                                                             }                                                                                                                                                                                                                                                                 test.home.monke.fail {                                                                                                                   respond "we hit the server!! must be a services issue"                                                                   }                                                                                                                                                                                                                                                                 jellyfin.home.monke.fail {
        reverse_proxy {                                                                                                          trusted_proxies                                                                                                }                                                                                                                        }                                                                                                                                                                                                                                                                 jellyseer.home.monke.fail {                                                                                                              reverse_proxy {
                trusted_proxies                                                                                                }                                                                                                                        }                                                                                                                                                                                                                                                                 lidarr.home.monke.fail {                                                                                                                 reverse_proxy {lcl}:8686 {                                                                                                               trusted_proxies                                                                                                }                                                                                                                        }                                                                                                                                                                                                                                                                 bazarr.{edn} {                                                                                                                           reverse_proxy {lcl}:6767 {
                trusted_proxies {evip}                                                                                                   }
                                                                                                                                 readarr.{edn} {
        reverse_proxy {lcl}:8787 {                                                                                                               trusted_proxies {evip}                                                                                                   }                                                                                                                        }
                                                                                                                                 sonarr.{edn} {                                                                                                                           reverse_proxy {lcl}:8989 {
                trusted_proxies {evip}                                                                                                   }
}                                                                                                                                                                                                                                                                 prowlarr.{edn} {
        reverse_proxy {lcl}:9696 {                                                                                                               trusted_proxies {evip}                                                                                                   }                                                                                                                        }
                                                                                                                                 qb.{edn} {                                                                                                                               reverse_proxy {lcl}:8200 {
                trusted_proxies {evip}                                                                                                   }                                                                                                                        }
                                                                                                                                 dashy.{edn} {                                                                                                                            reverse_proxy {lcl}:4000 {
                trusted_proxies {evip}                                                                                                   }                                                                                                                        }
                                                                                                                                 dashboard.{edn} {                                                                                                                        reverse_proxy {lcl}:3000 {
                trusted_proxies {evip}                                                                                                   }                                                                                                                        }

3. The problem I’m having:

can’t use variables at all, I have tried calling them through {vars.lcl} and {lcl}

4. Error messages and/or full log output:

{"level":"info","ts":1671785710.7593482,"msg":"using provided configuration","config_file":"/etc/caddy/Caddyfile","config_adapter":"caddyfile"}                                                                                                                   Error: adapting config using caddyfile: /etc/caddy/Caddyfile:2: unrecognized directive: lcl 

5. What I already tried:

I have moved the vars block from the bottom to top to confirm I’m not blind and missing some closing braces, I have also looked at a GitHub issue in surrounding the lack of variables in Caddyfiles, which then finished (ish) with the implementation of variables in Caddyfiles. Matt responded with a use case for them which I am sure I’m sticking to.

6. Links to relevant resources:

The vars directive is scoped to a vhost/site block.


vars {
	key value

localhost {
	respond {vars.key}

won’t work, as vars itself would be considered a site block.
Instead, place vars inside an existing site block (e.g. localhost):

localhost {
	vars {
		key value
	respond {vars.key}

Also, I was somewhat confused by your Caddyfile you shared, until I noticed it has horizontal scrolling.
You can and should use caddy fmt --overwrite to fix your formatting.

With that said, you cannot use vars in your site block’s hostname (e.g. dashboard.{edn}).

You can, however, use snippets :innocent:


i probably will use snippets and check out some more stuff that caddy offers to make those templates as easy as “service name” “port” (env var since i already have that in the .env file for the docker compose file.
yikes didn’t preview before posting, phone posting, must be Termux literally coping the blank space as it does sometimes for sone reason. will fix after i wake up after i sleep.
apologies for the eyebleach and thank you person who is indeed not james


If you use a lot of Docker/docker-compose, then you might be interested in GitHub - lucaslorentz/caddy-docker-proxy: Caddy as a reverse proxy for Docker

It allows you to configure Caddy dynamically using docker labels on your containers/services, which merges with the optional Caddyfile at CADDY_DOCKER_CADDYFILE_PATH (environment variable).

And in that Caddyfile, you could define your snippets, which in turn, can be imported via docker labels again :slight_smile:
(or other non-docker site-blocks)

Just as a quick fyi


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