Using catch-all subdomain as root path

1. Caddy version (caddy version):

N/A

2. How I run Caddy:

Would like to use Caddyfile with a wildcard DNS entry.
[edit: in the below nginx.conf, I was able to capture the subdomain value (server_name ~^([a-zA-Z0-9\-]+).+$ ;) and use it when setting the context root path (root /www/$1)]

a. System environment:

N/A

b. Command:

N/A

d. My complete Caddyfile or JSON config:

N/A

3. The problem I’m having:

Is there an easy way of doing something similar with Caddy as the following nginx.conf?

server {
        listen  80;
        server_name ~^([a-zA-Z0-9\-]+).+$ ;
        root /www/$1;
}

4. Error messages and/or full log output:

5. What I already tried:

6. Links to relevant resources:

I think this is what you’re looking for?

Next time, please fill out the help topic template.

Thank you!
Apologies, I was not able to articulate my question properly (or I just don’t understand your example).
I would like to catch the actual subdomain and use it later on.

So in the example from the site, in the fallback handler, if the server gets called from whatever12.example.com , I would like to serve the content from /www/whatever12

*.example.com {
	# Fallback for otherwise unhandled domains
	handle {
		## would like to catch the actual value of '*' somehow
               file_server
               root * /www/<the-actual-subdomain>/
	}
}

In the nginx.conf, I’m able to capture the value with a regexp group, and reuse it later:

        server_name ~^([a-zA-Z0-9\-]+).+$ ;
        root /www/$1;

You can do this in one of two ways.

Either you can use label placeholders to pull out the n-th segment of the hostname, or you can use a header_regexp Host matcher to pull out the value with a regexp capture group.

With labels:

*.example.com {
	handle {
		root * /www/{labels.2}
		file_server
	}
}

With a regexp matcher:

*.example.com {
	@subdomain header_regexp sub Host ^([a-zA-Z0-9\-]+).+$
	handle @subdomain {
		root * /www/{re.sub.1}
		file_server
	}
}

Thanks a lot!

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