How to serve userX.abc.com/dataX to /var/www/html/userX/dataX?

1. Caddy version (caddy version):

v2.5.2 h1:eCJdLyEyAGzuQTa5Mh3gETnYWDClo1LjtQm2q9RNZrs=

2. How I run Caddy:

caddy reload
with Caddyfile

a. System environment:

Linux 5.4.0-109-generic #123-Ubuntu x86_64

b. Command:

caddy reload

c. Service/unit/compose file:

-

d. My complete Caddyfile or JSON config:

john.abc.com {
	# Enable the static file server.
	file_server {
		root /var/www/html/john
	}
}

3. The problem I’m having:

Currently I add the above config manually every time there is a new user.
How to make it dynamically so userX accessing userX.abc.com will get static file server in /var/www/html/userX ?

4. Error messages and/or full log output:

No error message but it didn’t work. I can’t access the site.

5. What I already tried:

*.abc.com {
	# Enable the static file server.
	file_server {
		root /var/www/html/{http.request.host.labels.2}
	}
}

6. Links to relevant resources:

1 Like

This should do it:

*.abc.com {
	root * /var/www/html/{labels.2}
	encode gzip
	file_server
}

But keep in mind, you need to use the ACME DNS challenge to get a wildcard certificate. This requires building Caddy with a plugin for your DNS provider, and giving Caddy the credentials to use your DNS provider’s APIs.

If this is too complicated for you, then you can just list out each subdomain in your config, like this:

john.abc.com, bob.abc.com, alice.abc.com {
	root * /var/www/html/{labels.2}
	encode gzip
	file_server
}
1 Like

Hi, thank you for your help.
I will try to find a way to use the ACME DNS challenge. I will update the thread when I am done.

1 Like