Append new adapted Caddyfile to existing config

Hello everyone.

I recently installed Caddy on one of my edge servers that previously worked as a reverse proxy for multiple websites using nginx. I managed to convert all the nginx templates (one for each site) in Caddyfile format (one .caddy file for each website) and imported all these files with a single centrale Caddyfile with a simple import directive (import *.caddy).

Problem is, any time I change one of the configuration files, I need to perform a /reload of the Caddy configuration, so it takes some time in order to adapt all configs to JSON format and load them. This takes relatively high amount of time and CPU usage.

Is there any way to instruct Caddy to just load one Caddyfile, then “append” another Caddyfile to the existing configuration and so on… so that whenever I need to modify a server (i.e., only ONE of the many Caddyfiles) I can just replace that specific config instead of all of them every time?

I don’t know if the functionality you are searching for is out there somewhere.

But what I can tell you is that with a JSON config structure it is very convenient to update parts of the config on-the-fly. And also really fast.

To do so, embed “@id” properties in the entries you want to update later. Then, update only that part of the config using a POST or PATCH request.

Caddy can accept config updates on a specific endpoint or on a Unix socket.

1 Like

could you try GitHub - zhangjiayin/caddy-mysql-adapter?
it can separate every hosts to MySQL row with key

config.apps.http.servers.srv0.routes
1 Like

Hi, thank you for the reply.

The real issue here is that I need to use Caddyfiles and can’t switch to JSON. At least not directly.

From the docs it seems that @id directive is missing for Caddyfile… Also, in my case one Caddyfile contains multiple snippets, server blocks and custom SSL certificates. I need some way to identify the full file with all blocks and snippets.

What I was thinking was to write a code that asks Caddy to adapt each Caddyfile to JSON format and put an @id for the whole JSON string. I don’t know if this is possible tho, from documentation it seems lile you can assign an ID only to some blocks, but I need one ID for one Caddyfile / JSON, is it feasible?

Hi, thank you

Ok I’ll give a look into that.

Adapting configs is very fast. I’m not sure what problem you’re encountering here. Please elaborate. Adapting config and reloading should be near-instant.

No. Caddy has one config (and it’s JSON under the hood). Any change to the config reloads the the entire config (which is fast and light-weight). See Architecture — Caddy Documentation

Hi,

I have more or less 50 Caddyfiles, esche one containing unique snippets and server blocks (they are quite complicated) automatically generated from user settings.

In my Ubuntu 20.04 VM, with 4vCPU and 4 GB of RAM, when I reload the config all cpus go to 90% usage and it takes at least 30/45 seconds to complete the operation.

What version of Caddy? What’s in the configs?

Caddy
v2.6.4 h1:2hwYqiRwk1tf3VruhMpLcYTg+11fCdr8S3jhNAdnPy8=

The configs have all the same structure, basically it is a template in which values are replaced for each website. One Caddyfile is composed by more or less 600 rows.

They are divided in this way:

  • First section → snippets → 400 rows of snippets, with 16 snippet blocks
  • Second section → server blocks (remaining 200 rows) that include the previously defined snippets

I’m using Caddy as a reverse proxy. Auto SSL is disabled (each website has its own custom cert managed elsewhere).

Additional plugins:

Ah that would explain part of the slowness, I think. Loading tons of certificates upfront is expensive.

Since you have such a complex usecase, I suggest you get a support plan https://caddyserver.com/business to get help with optimizing your setup.

I understand, yes it could be the case.

So there isn’t a way to put an @id flag on each Caddyfile? Or maybe first convert the single Caddyfile to JSON and put an @id to identify it?

Thank you for your help

No, because that’s only a mechanism for aiding with using the config API.

And like I said, any config change incurs a “full” reload. There’s no such thing as a partial reload with Caddy’s architecture.

Ok, thank you for your help :smiley:

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