Wildcard domains

1. The problem I’m having:

I am building a multi-tenant web application. Clients have a default domain “UUID.cloudwebsitebuilder.cloud” but they also have the ability to add their own custom domain (default domain then redirects to the client domain). Caddy is running smoothly for me and everything works great! But! It occurred to me that I should be using a wildcard certificate for “*.cloudwebsitebuilder.cloud”. I’ve seen a few similar requests but none using the JSON config. I understand I need to get the DNS provider working for solving the ACME challenges, but that aside, how must my routes be adjusted?

3. Caddy version:

v2.7.6 h1:w0NymbG2m9PcvKWsrXO6EEkY9Ru4FJK8uQbYcev1p3A=

4. How I installed and ran Caddy:

Caddy is installed as a service and I’m administering it through the API only.

a. System environment:

AWS EC2 instance running Amazon Linux

d. My complete Caddy config:

{
    "apps": {
        "http": {
            "servers": {
                "clients": {
                    "listen": [
                        ":443"
                    ],
                    "routes": [
                        {
                            "@id": "7025329f-2226-4406-8300-c405ea3ba84f",
                            "handle": [
                                {
                                    "handler": "subroute",
                                    "routes": [
                                        {
                                            "handle": [
                                                {
                                                    "handler": "vars",
                                                    "site_id": "7025329f-2226-4406-8300-c405ea3ba84f"
                                                }
                                            ]
                                        },
                                        {
                                            "handle": [
                                                {
                                                    "handler": "static_response",
                                                    "headers": {
                                                        "Location": [
                                                            "https://dewpoint.com{http.request.uri.path}"
                                                        ]
                                                    },
                                                    "status_code": 301
                                                }
                                            ],
                                            "match": [
                                                {
                                                    "host": [
                                                        "www.dewpoint.com",
                                                        "7025329f-2226-4406-8300-c405ea3ba84f.cloudwebsitebuilder.cloud"
                                                    ]
                                                }
                                            ]
                                        }
                                    ]
                                }
                            ],
                            "match": [
                                {
                                    "host": [
                                        "dewpoint.com",
                                        "www.dewpoint.com",
                                        "7025329f-2226-4406-8300-c405ea3ba84f.cloudwebsitebuilder.cloud"
                                    ]
                                }
                            ]
                        },
                        {
                            "@id": "01164256-4c2c-4d62-922f-31bcc7456c46",
                            "handle": [
                                {
                                    "handler": "subroute",
                                    "routes": [
                                        {
                                            "handle": [
                                                {
                                                    "handler": "vars",
                                                    "site_id": "01164256-4c2c-4d62-922f-31bcc7456c46"
                                                }
                                            ]
                                        },
                                        {
                                            "handle": [
                                                {
                                                    "handler": "static_response",
                                                    "headers": {
                                                        "Location": [
                                                            "https://01164256-4c2c-4d62-922f-31bcc7456c46.cloudwebsitebuilder.cloud{http.request.uri.path}"
                                                        ]
                                                    },
                                                    "status_code": 301
                                                }
                                            ],
                                            "match": [
                                                {
                                                    "host": [
                                                        "www.01164256-4c2c-4d62-922f-31bcc7456c46.cloudwebsitebuilder.cloud"
                                                    ]
                                                }
                                            ]
                                        }
                                    ]
                                }
                            ],
                            "match": [
                                {
                                    "host": [
                                        "01164256-4c2c-4d62-922f-31bcc7456c46.cloudwebsitebuilder.cloud",
                                        "www.01164256-4c2c-4d62-922f-31bcc7456c46.cloudwebsitebuilder.cloud"
                                    ]
                                }
                            ]
                        },
                        {
                            "@id": "default_handler",
                            "handle": [
                                {
                                    "handler": "vars",
                                    "original_path": "{http.request.uri.path}"
                                },
                                {
                                    "encodings": {
                                        "gzip": {}
                                    },
                                    "handler": "encode",
                                    "minimum_length": 0
                                },
                                {
                                    "handler": "subroute",
                                    "routes": [
                                        {
                                            "handle": [
                                                {
                                                    "handler": "rewrite",
                                                    "uri": "/sites/{http.vars.site_id}/live/html{http.vars.original_path}index.html"
                                                },
                                                {
                                                    "handle_response": [
                                                        {
                                                            "match": {
                                                                "status_code": [
                                                                    403
                                                                ]
                                                            },
                                                            "routes": [
                                                                {
                                                                    "handle": [
                                                                        {
                                                                            "handler": "rewrite",
                                                                            "uri": "/api/publish-page?from_proxy=true&site_id={http.vars.site_id}&path={http.request.orig_uri}"
                                                                        },
                                                                        {
                                                                            "handler": "reverse_proxy",
                                                                            "headers": {
                                                                                "request": {
                                                                                    "set": {
                                                                                        "Host": [
                                                                                            "{http.reverse_proxy.upstream.hostport}"
                                                                                        ]
                                                                                    }
                                                                                }
                                                                            },
                                                                            "transport": {
                                                                                "protocol": "http",
                                                                                "tls": {}
                                                                            },
                                                                            "upstreams": [
                                                                                {
                                                                                    "dial": "dashboard.cloudwebsitebuilder.cloud:443"
                                                                                }
                                                                            ]
                                                                        }
                                                                    ]
                                                                }
                                                            ]
                                                        }
                                                    ],
                                                    "handler": "reverse_proxy",
                                                    "headers": {
                                                        "request": {
                                                            "set": {
                                                                "Host": [
                                                                    "cdn.cloudwebsitebuilder.cloud"
                                                                ]
                                                            }
                                                        }
                                                    },
                                                    "upstreams": [
                                                        {
                                                            "dial": "cdn.cloudwebsitebuilder.cloud:80"
                                                        }
                                                    ]
                                                }
                                            ]
                                        }
                                    ]
                                }
                            ]
                        }
                    ]
                }
            }
        }
    }
}

Please use the latest version, v2.8.4

The best way to get a working JSON config example is to write the Caddyfile first, then run caddy adapt -p which gives you the equivalent JSON.

1 Like

The best way to get a working JSON config example is to write the Caddyfile first, then run caddy adapt -p which gives you the equivalent JSON.

For those of us that work entirely with the JSON config, this isn’t realistic. I’m not going to also maintain a complex Caddyfile just so I can use examples off the web to patch my JSON config.

More JSON config examples in the documentation would be helpful.

I’m not saying you should reproduce your entire JSON config as Caddyfile, just do the one part you want to learn about and adapt that, then you’ll see what that should look like in JSON. It’s the fastest way to learn how to write idiomatic config.

2 Likes

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