Caddy completly crashes after api request

1. The problem I’m having:

I’m quite new to the caddy api and came across the following problem. While trying to create a new host via the api, caddy becomes completly unresponsive. This is my commands and config for the command:

curl "http://localhost:2019/load" \
	-H "Content-Type: application/json" \
	-d @caddy.json

With caddy.json as:

{
    "apps": {
        "http": {
            "servers": {
                "site_2": {
                    "listen": [":4444"],
                    "routes": [
                        {
                            "handle": [{
                                    "handler": "file_server",
                                    "root": "/sites/2"
                                }
                            ]
                        }
                    ]
                }
            }
        }
    }
}

2. Error messages and/or full log output:

2024-07-18 15:43:22 {"level":"debug","ts":1721310202.3903756,"logger":"tls.handshake","msg":"choosing certificate","identifier":"localhost","num_choices":1}
2024-07-18 15:43:22 {"level":"debug","ts":1721310202.3903892,"logger":"tls.handshake","msg":"default certificate selection results","identifier":"localhost","subjects":["localhost"],"managed":true,"issuer_key":"local","hash":"3f529c3e0b5743be300cf4709fa91170877c41e7b8074bebb1af2d7db31acc2f"}
2024-07-18 15:43:22 {"level":"debug","ts":1721310202.390395,"logger":"tls.handshake","msg":"matched certificate in cache","remote_ip":"172.19.0.1","remote_port":"35156","subjects":["localhost"],"managed":true,"expiration":1721349485,"hash":"3f529c3e0b5743be300cf4709fa91170877c41e7b8074bebb1af2d7db31acc2f"}
2024-07-18 15:43:22 {"level":"debug","ts":1721310202.4001575,"logger":"http.handlers.file_server","msg":"sanitized path join","site_root":"/sites/1","fs":"","request_path":"/","result":"/sites/1"}
2024-07-18 15:43:22 {"level":"debug","ts":1721310202.4002345,"logger":"http.handlers.file_server","msg":"located index file","filename":"/sites/1/index.html"}
2024-07-18 15:43:22 {"level":"debug","ts":1721310202.4002535,"logger":"http.handlers.file_server","msg":"opening file","filename":"/sites/1/index.html"}
2024-07-18 15:43:43 {"level":"info","ts":1721310223.0337226,"logger":"admin.api","msg":"received request","method":"POST","host":"localhost:2019","uri":"/load","remote_ip":"172.19.0.1","remote_port":"57576","headers":{"Accept":["*/*"],"Accept-Encoding":["gzip, deflate, br"],"Cache-Control":["no-cache"],"Connection":["keep-alive"],"Content-Length":["558"],"Content-Type":["application/json"],"Postman-Token":["7db67013-f04d-4e96-9c67-e421c436eb07"],"User-Agent":["PostmanRuntime/7.36.3"]}}
2024-07-18 15:43:43 {"level":"info","ts":1721310223.0343351,"logger":"admin","msg":"admin endpoint started","address":"localhost:2019","enforce_origin":false,"origins":["//127.0.0.1:2019","//localhost:2019","//[::1]:2019"]}
2024-07-18 15:43:43 {"level":"info","ts":1721310223.034558,"logger":"http.log","msg":"server running","name":"site_2","protocols":["h1","h2","h3"]}
2024-07-18 15:43:43 {"level":"info","ts":1721310223.0345922,"logger":"http","msg":"servers shutting down with eternal grace period"}
2024-07-18 15:43:43 {"level":"debug","ts":1721310223.0346992,"logger":"tls.cache","msg":"removed certificate from cache","subjects":["localhost"],"expiration":1721349485,"managed":true,"issuer_key":"local","hash":"3f529c3e0b5743be300cf4709fa91170877c41e7b8074bebb1af2d7db31acc2f","cache_size":0,"cache_capacity":10000}
2024-07-18 15:43:43 {"level":"info","ts":1721310223.0348349,"msg":"autosaved config (load with --resume flag)","file":"/config/caddy/autosave.json"}
2024-07-18 15:43:43 {"level":"info","ts":1721310223.0348427,"logger":"admin.api","msg":"load complete"}
2024-07-18 15:43:43 {"level":"info","ts":1721310223.0358942,"logger":"admin","msg":"stopped previous server","address":"0.0.0.0:2019"}

3. Caddy version:

Caddy 2.8.4

4. How I installed and ran Caddy:

a. System environment:

Windows 10
Docker desktop v4.32.0

b. Command:

docker compose up -d

c. Service/unit/compose file:

version: '3.9'

volumes:
   web-data:

services:
  caddy:
    image: caddy:2.8.4
    ports:
      - 80:80
      - 443:443
      - 127.0.0.1:2019:2019
    volumes:
      - web-data:/sites # Populated from another container
      - ./Caddyfile:/etc/caddy/Caddyfile 

d. My complete Caddy config:

{
    debug
    admin 0.0.0.0:2019
}

localhost {
    file_server {
        root /sites/1
        browse
    }
}

5. Links to relevant resources:

Nevermind, turns out that the /load endpoint overwrites all config. Switched to the /config put, and it all works.

FYI, this is potentially insecure, as it allows any interface to be able to access your admin endpoint. By default, only the loopback interface can access it.

To make sure we understand what was going on, can you elaborate on “becomes completely unresponsive”? Do you mean the request never got a response? Does a follow-up request not connect? What is the full output of curl -v?

1 Like

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