Hosting multiple sites with dynamic root directories

My Caddy version (caddy version):

v2.0.0-rc.2 h1:7NOaxYrsnQ5D3rcLGn2UnLZHLfBpgrfM/JNTLhjCJ1c=

I know its possible to host mutiple sites with Caddy using blocks contained in the Caddyfile like so:

host1.com {
  root * /sites/host1
}

host2.com {
  root * /sites/host2
}

host3.com {
  root * /sites/host1
}

What I’m trying to figure out though is if it’s possible to host multiple sites with Caddy dynamically, similar to how Apache can dynamically route traffic it with the VirtualDocumentRoot directive. In this setup I have n number of hosts running on a development server and would like routing to their respective site folders to be as minimal as possible without having to maintain a lengthy configuration file.

I know close configuration is possible with something like the following:

:443 {
  root * /sites/{host}
}

which will allow sites to be served from a directory named the same as the host. I’m wondering if further customization is possible like with VirtualDocumentRoot's “Directory Name Interpolation” allowing the use of a %N.M construct to select part of the server name. Does Caddy have a similar feature to select part of the requested name in order to keep my file structure clean?

Yes, you can use {http.request.host.labels.*} where * is the right-most segment of the domain. It’s documented in the placeholder table here:

Ah, cool! Thanks so much, the documentation for Caddy 2 is a bit spaced out for the time being so I must’ve missed it in my search but I’m glad caddy has this option.

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