Running multiple subdomain static websites on one Caddy server? (noob question)

1. Caddy version (caddy version):

v2.5.1

2. How I run Caddy:

I run it on a Debian server as a very basic static website with HTML and CSS only.

My folder structure is as follows:

home
-- /www
---- /html
------ /files
-------- image1.png
-------- image2.png 
------ index.html
------ robots.txt
------ style.css
---- /html2
------ /files
-------- image3.png
-------- image4.png 
------ index.html
------ robots.txt
------ style2.css

a. System environment:

Debian 11, installed v2.5.1 via apt. Default HTTP/HTTPS ports with default SSL certificate.

b. Command:

sudo systemctl reload caddy

c. Service/unit/compose file:

N/A

d. My complete Caddyfile or JSON config:

I know I ignored the “leave domain names intact” rule, but I insist it’s not important to my noob question.

example.com {
	# Set this path to your site's directory.
	root * /home/www/html

	# Enable the static file server.
	file_server

	# Disable HTML caching because static content changes frequently
	header *.html Cache-Control "no-store, must-revalidate"

	# Remove .html from URL endings
	try_files {path}.html {path}

	@notAssets {
		not path /files/*
	}

	# Smaller files (at cost of CPU power, excluding images)
	encode @notAssets gzip
}

www.example.com { redir https://example.com{uri}
}

sub1.example.com {
	root * /home/www/html2
	file_server
	header *.html Cache-Control "no-store, must-revalidate"
	try_files {path}.html {path}
	@notAssets {
		not path /files/*
	}
	encode @notAssets gzip
}

www.sub1.example.com { redir https://sub1.example.com{uri}
}

3. The problem I’m having:

Hello! I have a functioning static site at some domain (let’s say example.com) using files located at /home/www/html, and I want to run other static sites on the same domain, but with subdomains (such as sub1.example.com) and in a different folder like /home/www/html2. Is it possible to do this? I tried to find helpful forum posts but they keep mentioning running different sites on different ports and I want to just use the standard HTTPS port (443) for everything. This is my first time using server tech like Caddy/nginx/etc so I have no idea if I’m way over my head for thinking I can have multiple subdomains without configuring multiple ports, so please fill me in on how I should actually approach this. Thanks!

4. Error messages and/or full log output:

N/A

5. What I already tried:

  • Cloning the settings I have to a new subdomain rule in the Caddyfile
  • Removing the file_server line from the subdomain config
  • Looking for similar questions on the forums (all mentioning multiple ports or reverse proxies?)

6. Links to relevant resources:

Please help I am stupid

Howdy @gs1!

It looks like you’ve already got a Caddyfile that should be able to handle this. The important parts of the pattern you probably want look like:

sub1.example.com {
  root * /home/www/html1
  file_server
  # ...
}

sub2.example.com {
  root * /home/www/html2
  file_server
}

The above simple snippet would run two HTTPS sites, sub1 and sub2, both on the standard ports.

3 Likes

Hi Matthew! Thanks so much for confirming that my configuration was actually correct, it turns out that:

  1. I forgot to set my registrar’s DNS settings to point all subdomains (*.example.com) to my server

  2. I just had to wait an hour for the SSL certificates to generate, I was getting “incorrect response” errors in my browser when I tried this last time without waiting first

Thank you again!!

2 Likes

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