Caddyfile Syntax For Multiple Domains

I’m finally biting the bullet here and updating from Caddy to Caddy 2 and wresting with the new format Caddyfile. After much wailing and gnashing of teeth, I’ve managed to suss out all the difficult stuff like reverse proxying,etc. But I’m being completely stymied by the syntax for adding multiple domains.

Here’s my first domain:

#||||||||||| madra.net ||||||||||||||

#redirect www
www.madra.net {
  	redir https://madra.net{uri}
}

# the main wan
madra.net


    root * /www/madra.net/public

# logging
log {
	output file     /www/loganna/madra.net.access.log
}

# error handling
handle_errors {
	rewrite * /{http.error.status_code}.html
	file_server
}

# gzip encoding
encode gzip


# calibre-web
handle_path /calibre* {
reverse_proxy localhost:33336 {
    header_up X-Script-Name /calibre
}
}

# run as static file server

file_server

#||||||||||| madra.net ||||||||||||||

That works fine. But if I add a second domain to the file:

#||||||||||| stiobhart.net ||||||||||||||

#redirect www
www.stiobhart.net {
  	redir https://stiobhart.net{uri}
}

# the main wan
stiobhart.net

    root * /www/stiobhart.net/public

# logging
log {
	output file     /www/loganna/stiobhart.net.access.log
}

# error handling
handle_errors {
	rewrite * /{http.error.status_code}.html
	file_server
}

# gzip encoding
encode gzip


# isso comments
handle_path /isso* {
reverse_proxy localhost:8080 {
    header_up X-Script-Name /isso
}
}

# run as static file server

file_server

#||||||||||| stiobhart.net ||||||||||||||

I get the following error:

ERROR	watcher	unable to load latest config	{"config_file": "Caddyfile", "error": "adapting config using caddyfile: Caddyfile:48: unrecognized directive: www.stiobhart.net"}

“OK”. I thought. “Looks like Caddy thinks the second domain is a directive of the first. Maybe I need to bracket both domains to keep them separate?” So I replaced:

#redirect www
www.madra.net {
  	redir https://madra.net{uri}
}

# the main wan
madra.net

<snip>

with

#redirect www
www.madra.net {
  	redir https://madra.net{uri}
}

# the main wan
madra.net {

<snip>

}

and likewise for the second domain. But now I get the following error:

ERROR	watcher	unable to load latest config	{"config_file": "Caddyfile", "error": "adapting config using caddyfile: subject does not qualify for certificate: 'stiobhart.net{'"}

If I add any further domains that error changes to refer to whichever is the last one in the Caddyfile. So how do I handle multiple domains in this new style Caddyfile syntax?

You must use braces when configuring multiple sites, and you must have a space before the { for sites. Whitespace is significant in the Caddyfile.

Also, please use the included caddy fmt command to clean up the syntax of your config.

1 Like

Wow! --cheers. That was all there was to it. I hadn’t put a space between domain.name and {

Pity the importance of the whitespace wasn’t mentioned in the docs. Might have saved me a lot of hair tearing out!

It is mentioned actually:

The Caddyfile is lexed into tokens before being parsed. Whitespace is significant in the Caddyfile, because tokens are separated by whitespace.

OK. But, to be fair. That’s mentioned under Tokens and quotes which is the third heading down on that page.

Under Blocks which is the top heading, it just says:

Blocks🔗
Opening and closing a block is done with curly braces:

… {

}
The open curly brace { must be at the end of its line.
The close curly brace } must be on its own line.
When there is only one site block, the curly braces (and indentation) are optional.

No mention of whitespace there. Only that the opening bracket needs to be at the end of its line.

You can’t just half read the docs, you need to read it as a whole. Blocks and tokens are separate concepts. Blocks follow from the concept of tokens.

1 Like

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