How can I create common directives in Caddyfile that I can use instead of constantly needing to copy paste the same configs over and over again for each domain

1. The problem I’m having:

I have a Caddyfile with ~10 subdomains that typically have three directives:

example.com {
  forward_auth authelia:9091 {
    uri ...
    copy_headers ...
  }

  reverse_proxy http://homepage:3000

  tls {
    resolvers 1.1.1.1
    dns cloudflare TOKEN_HERE 
  }
}

whoami.example.com {
  forward_auth authelia:9091 {
    uri ...
    copy_headers ...
  }

  reverse_proxy http://whoami:80

  tls {
    resolvers 1.1.1.1
    dns cloudflare TOKEN_HERE 
  }
}

# Same thing over and over again with reverse proxy being different

I want to make it a bit more manageable by making some common directives and using them when defining my domains. Basically I want to be able to do something like this:

example.com {
  forward_authelia
  tls_cloudflare
  reverse_proxy http://homepage:3000
}

whoami.example.com {
  forward_authelia
  tls_cloudflare
  reverse_proxy http://whoami:80
}

Is this something that is possible to do with Caddyfile (maybe not the way I have written but something similar)? If yes, how? If no, can someone suggest me a config adapter that can handle this while having a syntax similar to Caddyfile (I really like Caddyfile syntax)?

2. Error messages and/or full log output:

No error message

3. Caddy version:

v2.7.6

4. How I installed and ran Caddy:

Setup a docker compose file and ran it inside podman.

a. System environment:

Caddy is running in Podman

b. Command:

podman compose up -d

Use snippets

2 Likes

This is exactly what I was looking for. Thank you!