Caddy not applying changes via api

1. The problem I’m having:

Okay, so, I sent a request to my Caddy api to load a new config file. I didn’t restart Caddy, as that would cause things to go down. The issue is that, if I go to the domain, it doesn’t work, but, if I restart Caddy, it works.

2. Error messages and/or full log output:

There are no errors.

3. Caddy version:

v2.6.4

4. How I installed and ran Caddy:

a. System environment:

Ubuntu 20.04 - not from APT.

b. Command:


caddy run --config caddy.json

d. My complete Caddy config:

This is the Caddy config I post to the server.

{
    "apps": {
        "http": {
            "servers": {
                "id": {
                    "listen": [
                        ":443"
                    ],
                    "routes": [
                        {
                            "match": [
                                {
                                    "host": [
                                        "830008x.com"
                                    ]
                                }
                            ],
                            "handle": [
                                {
                                    "handler": "subroute",
                                    "routes": [
                                        {
                                            "handle": [
                                                {
                                                    "handler": "reverse_proxy",
                                                    "upstreams": [
                                                        {
                                                            "dial": "127.0.0.1:3000"
                                                        }
                                                    ]
                                                }
                                            ]
                                        }
                                    ]
                                }
                            ],
                            "terminal": true
                        },
                        {
                            "match": [
                                {
                                    "host": [
                                        "timeout",
                                        "idle",
                                        "5s"
                                    ]
                                }
                            ],
                            "terminal": true
                        }
                    ]
                }
            }
        }
    }
}

What are the steps you are taking so that we can reproduce the behavior?

It’s impossible to help without seeing your logs, or seeing how you’re pushing the config to the server (show an example with curl -v).

So, I make the request with NodeJS.

async function addServer(id, port, domain){
    let OldServer = CaddyServerEx
    return new Promise(async (resolve, reject) => {
        let newServer = {
            id: {
                "listen": [
                        ":443"
                ],
                "routes": [
                        {
                                "match": [
                                        {
                                                "host": [
                                                        domain
                                                ]
                                        }
                                ],
                                "handle": [
                                        {
                                                "handler": "subroute",
                                                "routes": [
                                                        {
                                                                "handle": [
                                                                        {
                                                                                "handler": "reverse_proxy",
                                                                                "upstreams": [
                                                                                        {
                                                                                                "dial": "127.0.0.1:" + port    
                                                                                        }
                                                                                ]
                                                                        }
                                                                ]
                                                        }
                                                ]
                                        }
                                ],
                                "terminal": true
                        },
                        {
                                "match": [
                                        {
                                                "host": [
                                                        "timeout",
                                                        "idle",
                                                        "5s"
                                                ]
                                        }
                                ],
                                "terminal": true
                        }
                ]
        }
           }
        let CaddyJson = JSON.parse(await fs.readFile('./caddy/caddy.json', 'utf8'))
        CaddyJson.apps.http.servers.id = newServer.id
        await fs.writeFile('./caddy/caddy.json', JSON.stringify(CaddyJson, null, 4), 'utf8')
        resolve(await reloadCaddy())
        
    })
}

There are no errors though, and Caddy gets the request.

Sorry for the late response.

That code looks like it just writes a file. What does reloadCaddy() do?

Oops, I sent the wrong snippet.

async function reloadCaddy(){
    return new Promise(async (resolve, reject) => {
        fetch('http://localhost:2019/load', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
                'Cache-Control': 'must-revalidate'
            },
            body: JSON.stringify(await fs.readFile('./caddy/caddy.json', 'utf8'))
        }).then(res => res.json()).then(json => {
            resolve(json)
        }).catch(err => {
            reject(err)
        })
    })
    
}

Thanks. So what happens when you go to the domain? You say “doesn’t work” but we’ll need more details.

What happens is there is no ssl certificate.

What’s the errors? There will be two: one on the client side and one on the server.

Use curl -v and paste the full command and output here please, along with server logs. Otherwise we cannot help you.

This doesn’t look right, is it? :face_with_monocle:

1 Like

I’m not sure, it’s what happened when I converted my original CaddyFile to JSON.

Ok. Well, we can’t do anything more to help you until you answer our questions and provide more information.

Good luck!

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