Includes in caddyfile

Are there any plans to support includes in Caddyfile?

We store virtualhost configurations in separate files, so, it’d be great to do something like this in Caddyfile:

   some general conifg
   include /etc/caddy/additional_config.caddy
include  /etc/caddy/snippets.caddy
include /etc/caddy/vhosts.caddy

Where /etc/caddy/vhosts.caddy would have something like:

include /etc/caddy/vhosts/
include /etc/caddy/vhosts/
include /etc/caddy/vhosts/

And, of course /etc/caddy/vhosts/ would be the final vhost config, like: {

If it’s not planned (or supported already), we’ll be forced to use JSON API to add vhosts to caddy and load them all ons starting time. However, I haven’t found a way to use JSON API securely, or am I just missing this? I mean anyone having access to localhost (shared webhosting) could do actions with it. I’d suggest using control socket, like “nginx unit” does (Configuration — NGINX Unit). That way /path/to/control.caddy.sock would be only accessible by root, and no one else would be able to do actions with the API.

Thank you!

It is supported, actually! It’s the import directive. See Caddyfile Concepts — Caddy Documentation

/cc @matt it’s not in the Caddyfile Directives — Caddy Documentation page, should we add it?

1 Like

I’ve seen that one, but it requires defining the snippet in caddyfile, while we want to include a config block to the configfile itself (the reason why I gave this in example):
include /etc/caddy/snippets.caddy

Can we include snippets without any snippets defined in the same fille? :slight_smile: I guess no?

Yes, the last line of that section reads:

The import directive can also be used to include other files in its place. As a special case, it can appear almost anywhere within the Caddyfile.


Just to chime in here, this makes sense to me. It’s a meta directive, but in my opinion still a directive.

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