Multiple Domains Caddyfile

Hi:

I’m trying to setup my Caddy container to host 2 websites (eventually 4) and was wondering if there was a better way to go about it.

Caddyfile

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

exampleone.com {
  root /srv/exampleone.com
}

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

exampletwo.com {
  root /srv/exampletwo.com
}
tls myemail@email.com

Docker command

docker run -d -e “CADDYPATH=/etc/caddycerts” \
-e “ACME_AGREE=true” -v
$HOME/.caddy:/etc/caddycerts \
-v $HOME/Caddyfile:/etc/Caddyfile \
-v /etc/www/exampleone.com:/srv/exampleone.com \
-v /etc/www/exampletwo.com:/srv/exampletwo.com \
-p 80:80 -p 443:443 \
--name=“website_container” abiosoft/caddy

I originally only had exampleone.com running for a while, and am changing it to run exampletwo.com as well. I’m still fairly new to both Docker and Caddy, what kind of improvements can I make? Is this a weird way of setting up the Caddyfile?

Thank you!

Seems pretty straightforward, actually!

With the exception of that rogue tls command. It’s going to cause issues if you put it outside of a site block. Caddy will think you’re trying to serve a site with the labels tls and myemail@email.com and error out because those don’t look like website hostnames.

Also - currently, Caddy takes your www.exampleone.com, and splits it into two listeners: http://www.exampleone.com, which redirects to HTTPS, and https://www.exampleone.com/, which serves the site as you’ve specified (redirects to the bare domain).

That means that people visiting www over HTTP get redirected twice. You could optimise this:

http://www.exampleone.com, https://www.exammpleone.com {
  redir https://exampleone.com{uri}
}

exampleone.com {
  root /srv/exampleone.com
}

Now HTTP and HTTPS visitors to www get one redirect to your main site.

On the docker side, you could probably reduce down to one volume mount: /etc/www:/srv. Then you can add or remove sites in that folder without having to much around with the Docker volumes.

1 Like

Thank you for the feedback! I’ve moved the tls command into both sections and made changes according to your example. Everything works great!

1 Like

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