Do wildcard address blocks apply configuration options?

Note: I’m ignoring the template, because it’s geared towards problems and I have more of a general question.

I am trying to figure out how to pare down redundant config declarations. I am wondering if that can be done by using wildcard addresses. However, the only mention of wildcards in the docs is for TLS. So, I am looking for confirmation whether this (below) is or is not possible.

Question: Given the configuration below, will the address ‘sub.example.com’ have logging and compression?

snippets.conf

(logs) {
    log {
        output file /var/log/caddy/example
   }
}

(compress) {
    encode zstd
}

example.conf

*.example.com {
    import logs
    import compress
}

sub.example.conf

sub.example.com {
   respond 'hi'
}

Caddyfile

import snippets.conf
import example.conf
import sub.example.conf

In your example, sub.example.com will NOT inherit configuration from *.example.com.

If you want to reuse code between blocks you’ll need to import the snippets into each block.

An alternative would be to simply not define a separate sub.example.com and instead put it under your *.example.com block with a host matcher:

*.example.com {
  # These execute for all requests
  import logs
  import compress

  @foo host foo.example.com
  handle @foo {
    # Only executes for foo.example.com
    respond "I'm foo"
  }

  @bar host bar.example.com
  handle @bar {
    # Only executes for bar.example.com
    respond "I'm bar"
  }

  handle {
    # Only executes if none of the others did
    # (handle blocks are mutually exclusive)
    respond "I'm the default handler"
  }
}
1 Like

I figured as much, but wanted to check.
Thanks for the confirmation, @Whitestrake.

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