How to configure the API interface via a Caddyfile?

1. My Caddy version (caddy version):

~ # ./caddy version                                                                                                                                                            root@srv
v2.0.0-rc.3 h1:z2H/QnaRscip6aZJxwTbghu3zhC88Vo8l/K57WUce4Q=

2. How I run Caddy:

~ # ./caddy run                                                                                                                                                                root@srv
2020/04/23 07:43:07.316 INFO    admin   admin endpoint started  {"address": "tcp/localhost:2019", "enforce_origin": false, "origins": ["localhost:2019", "[::1]:2019", ""]}
2020/04/23 07:43:07.316 INFO    serving initial configuration

a. System environment:

Ubuntu 18.04

d. My complete Caddyfile or JSON config:

No configuration file (but I would like to have one, please read “what I tried” below)

3. The problem I’m having:

I would like to start caddy without any configuration (it will be provided later via the API), but only with the API listening on all interfaces beside localhost.

5. What I already tried:

The best solution for me would be to have a minimalist Caddyfile which would just configure the API interface and port. I hoped that

  admin {
    listen :2020

would do the job, but I get a

~ # ./caddy run                                                                                                                                                                
2020/04/23 07:47:39.051 INFO    using adjacent Caddyfile
run: adapting config using caddyfile: admin: Caddyfile:2 - Error during parsing: Wrong argument count or unexpected line ending after 'admin'

I then tried to push via the API the following config (derived from the Caddyfile above) and it worked

"admin": {
    "listen": ":2020"

OK, so maybe that would be OK to have in the Caddyfile then? But no:

~ # ./caddy run                                                                                                                                                                
2020/04/23 07:52:29.717 INFO    using adjacent Caddyfile
run: loading initial config: loading new config: starting caddy administration endpoint: parsing admin listener address: invalid start port: strconv.ParseUint: parsing "": invalid syntax

The docs for global options in the Caddyfile are here:

You’re looking for:

    admin :2020

Thanks, would you know how to enforce the allowed hosts in such a config, a non-JSON version? (see "Host not allowed" when calling the API remotely for the problem and solution for the JSON one, I think that it is even you who marked that as a solution)

That’s not currently supported via Caddyfile, because to be honest, if you plan to use the API for JSON config, you should probably start from a JSON config in the first place. It could be added eventually, but it’s just not supported in Caddyfile yet.

OK thanks. Since I managed to start Caddy with a JSON file which allows me to expose the API remotely I will go for that anyway. This question was an earlier attempt. I will keep it for reference though in case someone has the same problem.

Thanks again.

1 Like

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