How to handle lots of identical entries (aka templating)

1. Caddy version (caddy version):

v2

3. The problem I’m having:

I have lots of sites which are defined exactly the same way:

https://siteA.mydomain {
    reverse_proxy endpointA:portA
}

https://siteB.mydomain {
    reverse_proxy endpointB:portB
}

I would like, ideally to configure them in one shot (pseudocode follows):

for site in [
    ('siteA', 'endpointA:portA'), 
    ('siteB', 'endpointB:portB')
]:
    output_config('https://{{site[0]}}.mydomain {  reverse_proxy {{site[1]}} }')

It would be also wonderful to have some conditional actions in such a template.

Is this available in Caddy? (probably no)
Is there something close to that (probably no, except snippets)
Is this part of a long-term strategy?

The closest thing is snippets with arguments:

(proxy_template) {
	https://{args.0}.example.com {
		reverse_proxy {args.1}
	}
}

import proxy_template siteA endpointA:portA
import proxy_template siteB endpointB:portB

I can’t say we have plans to expand on Caddyfile templating, it’s not a common concern and it would take a lot of effort to implement in a flexible way.

You could use some tool like https://docs.gomplate.ca/ from one of our contributors @hairyhenderson to generate your config from a template.

You could also look into GitHub - lucaslorentz/caddy-docker-proxy: Caddy as a reverse proxy for Docker if you’re running in Docker, to have Caddy auto-configure based on docker container labels

1 Like

Oh yes - thanks! I have several nested snippets so this is a natural extension. Great idea

I’ve written my own at some point but wanted something native to caddy.

I’ve used this for quite a long time but it probbaly not going to be maintained for caddy v2 andthe overload on the docker-compose file is not worth the effort. Caddyfiles are nice too.

I wrote in the past a tool that was talking with the docker engine and building up a JSON caddy configuration. But again - not worth the effort vs. a Caddyfile which I find more elegant.

Not true, it was rebuilt for v2 and it works great! Take another look!

Ah, good to know.

I stopped isn’t it when I read in one of the big reports a discussion about its future where Lucas mentioned that with Traefik he did not see much sense in going forwards.

Happy to hear this changed, it is a great piece of work.

I used that, then Traefik (1 and 2), then my own tool that was making use of the caddy API to finally end up with a plain Caddyfile

I proxy about 30 containers, they change sometimes but not often enough to be forced to use an edge router for them.

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