Config for multiple domains and apps via environment-variable

1. The problem I’m having:

I have multiple configs for each app (subdomain). My goal is not touching the config file for multiple domains. I want to define the different domain names via an environment variable.

At the moment i define my apps for a domain like this. So i can specify the domain name via environment var.

app1.{$BASE_DOMAIN} {
 ...
}

This is working fine, but i have several host that have multiple (alternative) domain-names that should serve the same apps.

app1.domain-a.com, app1.domain-a.com {
...
}

app2.domain-a.com, app2.domain-b.com {
...
}

so it would be nice to have something like this possible.

app1.{$DOMAINS} {
...
}

app2.{$DOMAINS} {
...
}

where environment var $DOMAINS can be for example “localhost | domain-a | domain-b”

Is it possible to use dynamic (environment-based) base-domain-names, so that i did not need to touch the caddy-config for multiple domains?

Thank you for any hint on this.

Tobias

At this point, I would probably look at something like dockerize to see if it can template the Caddyfile the way you want. Then run dockerize shortly before starting Caddy so it generates the final Caddyfile from a template.

Dockerize is meant for templating inside Docker containers, but you can also use it outside containers. It is a relatively small Golang app.

Another option is to write a simple shell script that generates the final Caddyfile based on your criteria before launching Caddy.

Hi timelordx,

thank you for your feedback. Sorry docker is no option for me. but i finally got it working :flexed_biceps:

Solution:

i have a 2 config files (simple text) that are read in the envionment var that contains for example.

in Caddyfile for the base domain(s) (allways on):

import conf.d/basedomains.conf

and in conf.d/basedomains.conf

{$DOMAINS:localhost} {
  import default_log  
  ...
}

For the subdomains as global block in Caddyfile:

{$SUB_DOMAINS:localhost} {
   import securityheader  
   #auto active all apps
   import conf.d.apps/*.conf
}

In conf.d.apps/app1.conf

@app1_app header_regexp sub host ^app1.(.*)
handle @app1_app {
  root * {$APP1_WEB_PATH}\public
  #special configs...
  …
}

In conf.d.apps/app2.conf

@app2_app header_regexp sub host ^app2.(.*)
  handle @app2_app {
  root * {$APP2_WEB_PATH}\public
  #speical configs
  …
}

The $SUB_DOMAINS - ENV-Data can also be generated via the active apps in “conf.d.apps/*.conf” and the $DOMAINS

So i can easily add a new base-domain-name without touching the default/fixed config files that must be maintained on many servers.

If i want to active a prepared additional app, i only need to create a link in conf.d.apps/<appname>.conf

:slight_smile:

Any Problems to expect with my solution?

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