On demand tls fails with json config

1. The problem I’m having:

1.1 cannot set on-dmand tls with json config
1.2 optionally allow for local https

2. Error messages and/or full log output:

docker logs ca

3. Caddy version:

v2.8.4 h1:q3pe0wpBj1OcHFZ3n/1nl4V4bxBrYoSoab7rL9BMYNk=

4. How I installed and ran Caddy:

docker run

a. System environment:

docker

b. Command:

 "Args": [
                        "caddy",
                        "run",
                        "--config",
                        "/etc/caddy/caddy.json"
                    ],

c. Service/unit/compose file:

d. My complete Caddy config:

{
    "admin": {
        "disabled": false,
        "listen": ":2019",
        "enforce_origin": false
    },
    "logging": {
        "logs": {
            "": {
                "level": "DEBUG"
            }
        }
    },
    "storage": {
        "module": "file_system",
        "root": "/data/caddy"
    },
    "apps": {
        "http": {
            "http_port": 80,
            "https_port": 443,
            "servers": {
                "example": {
                    "listen": [
                        ":80",
                        ":31080",
                        ":443",
                        ":31443"
                    ],
                    "routes": [
                        {
                            "handle": [
                                {
                                    "handler": "static_response",
                                    "body": "Hello, world 24!"
                                }
                            ]
                        }
                    ],
                    "automatic_https": {
                        "disable": false
                    }
                }
            }
        },
        "tls": {
            "automation": {
                "on_demand": {
                    "permission": {
                        "module": "http",
                        "endpoint": "http://192.168.29.201:4000/api/check-domain"
                    }
                },
                "policies": [
                    {
                        "issuers": [
                            {
                                "module": "acme",
                                "ca": "https://acme-v02.api.letsencrypt.org/directory"
                            }
                        ]
                    }
                ]
            }
        }
    }
}

5. Links to relevant resources:

i have http://192.168.29.201:4000/api/check-domain

const express = require('express');
const app = express();
 app.get('/api/check-domain', (req, res) => {
    const domain = req.query.domain;
    
    console.log('WORKING ', domain);

    // Implement your logic to check if the domain should be allowed
    res.status(200).send('OK');

    if (isValidDomain(domain)) {
    } else {
        res.status(403).send('Forbidden');
    }
});

function isValidDomain(domain) {
    // Your domain validation logic here
    // For example, you can check against a database of allowed domains
    return true; // Placeholder for actual logic
}

app.listen(4000, () => {
    console.log('Server is running on port 4000');
});

// http://192.168.29.201:3000/api/check-domain

which simply returns 200 OK

The caddy json config is:

  1. not redirecting http to https
  2. not generating ssl certificates for wildcard domain

Do you actually have a reason to use JSON config? Why are you trying that? Why not just use a Caddyfile?

The best way to learn to write JSON config is to write it as a Caddyfile first, then adapt it to JSON.

{
	on_demand_tls {
		ask http://192.168.29.201:4000/api/check-domain
	}
}

https:// {
	tls {
		on_demand
	}

	respond "Hello world!"
}

Then run caddy adapt -c Caddyfile -p to see what it looks like as JSON.

You should only have :443 here. Let Caddy automatically provision the :80 server (Automatic HTTPS).

Don’t set this, let Caddy use its default.

2 Likes

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