Caddy: Env var for domain names not supported?

Created on 3 Jun 2019  路  16Comments  路  Source: caddyserver/caddy

Hello! First of all, thanks so much for Caddy... it has been a lifesaver :)

1. Which version of Caddy are you using (caddy -version)?

Caddy 0.11.5 (unofficial)

2. What are you trying to do?

Pass {$CADDY_DOMAIN} to our Caddyfile for multiple domains

3. What is your Caddyfile?

paste entire Caddyfile here - DO NOT REDACT ANYTHING (except credentials)


{$CADDY_DOMAIN} {
    gzip
    root /var/www/testing
    proxy / web:8000 {
        transparent
        except /static
    }

    push
}

The above fails the following ways with the following inputs:

# CADDY_DOMAIN=http://localhost, https://testing.serveo.net
2019/06/03 17:33:52 error inspecting server blocks: parse http://localhost, https://testing.serveo.net: invalid character " " in host name
# CADDY_DOMAIN=http://localhost,https://testing.serveo.net
2019/06/03 17:41:17 [INFO] Serving http://localhost,https//testing.serveo.net

# (note the domain is wrong, smashed both together)

However when I put the text straight in the Caddyfile, it works!

# This works!
http://localhost,https://testing.serveo.net {
    gzip
    root /var/www/testing
    proxy / web:8000 {
        transparent
        except /static
    }

    push
}

4. How did you run Caddy (give the full command and describe the execution environment)?

From a docker container, defined like so....

  caddy:
    image: abiosoft/caddy:latest
    restart: unless-stopped
    env_file: .env
    volumes:
      - ./conf/Caddyfile:/etc/Caddyfile
      - ./testing/static:/var/www/testing/static
      - ./docker_storage/certs/caddy:/etc/caddycerts
    ports:
      - 80:2015
      - 443:443
    depends_on:
      - web

Bonus: What do you use Caddy for? Why did you choose Caddy?

For the easy HTTP/2 and SSL :)

feature request

Most helpful comment

Ah, yeah, in that case then seems like 2 birds with one stone :)

In any event, we've worked around this "problem" and love using Caddy, thanks so much! Excited to try out v2.

All 16 comments

What happens if you pass it without the , bit with a space

CADDY_DOMAIN=http://localhost https://testing.serveo.net

That might be because environment variables expand to only a single token, rather than potentially multiple.

@tobya

2019/06/04 01:11:43 error inspecting server blocks: parse http://localhost https://testing.serveo.net: invalid character " " in host name

@mholt ah, is there a potential fix or a nicer way to do what I'm trying to do?

I think we can make this work in Caddy 2.

Wahoo! Thanks :)

Can you use a different env var for each domain name?

In our use case, I don't think so? It's an unknown length dynamic list separated by comma

Is there a reason you're using env variables for this specifically? Is this a scripted/automated process that is deploying or generating the config?

Yeah, across many dev/server environments we'll have different numbers of domains -- automated process via Docker voluming in our Caddyfile with this environment variable.

Hmm, I'm wondering if this'll even be relevant in Caddy 2 since you can just generate a JSON array with the hostnames in them...

Ah, yeah, in that case then seems like 2 birds with one stone :)

In any event, we've worked around this "problem" and love using Caddy, thanks so much! Excited to try out v2.

Okay great! I will not spend time on this right now then. (Expansion of placeholders in the _lexer_ is possible but not awesome, I'd rather not for now if we can avoid it.) Instead, if you have a chance to try out Caddy 2, please take a look at the v2 branch and the wiki -- it should have all you need to get started!

(The reverse proxy in v2 is still pretty basic but _should_ be able to do what your Caddyfile above does.)

Hey @mholt ! First of all thank you for making ssl so easy. I spent hours working out automated process with openresty, lua scripts, bash scripts, stack overflow and too much coffee.

I do see this issue got closed with a remark that Caddy 2.0 will just work but I tested and if I set
CADDY_DOMAIN=domain.one, domain.two it doesn't expand {$CADDY_DOMAIN} in Caddyfile properly. It works for just one domain and it does work when I put the domain names there manually. My use case is that I do have multiple servers that host the same site behind the load balancer but any specific server is also accessible through each server's subdomain so I need both example.com and foo.example.com assigned to one block in Caddyfile. I am fine doing it manually but env variable does sound super awesome and flexible.

The Caddyfile parser doesn't expand one variable into more than one token - if you need more than one domain, put two env vars in your Caddyfile and set each of them.

There's also this bit of info...

_Hmm, I'm wondering if this'll even be relevant in Caddy 2 since you can just generate a JSON array with the hostnames in them..._

EDIT: I think this is the relevant configuration key?
https://caddyserver.com/docs/json/admin/origins/

@ckcollab not exactly. That's for the admin endpoint, not for sites you want to serve. For example, the following Caddyfile adapts to this JSON:

example.com, foo.example.com
respond "Hello World"
{
    "apps": {
        "http": {
            "servers": {
                "srv0": {
                    "listen": [
                        ":443"
                    ],
                    "routes": [
                        {
                            "match": [
                                {
                                    "host": [
                                        "example.com",
                                        "foo.example.com"
                                    ]
                                }
                            ],
                            "handle": [
                                {
                                    "handler": "subroute",
                                    "routes": [
                                        {
                                            "handle": [
                                                {
                                                    "body": "Hello World",
                                                    "handler": "static_response"
                                                }
                                            ]
                                        }
                                    ]
                                }
                            ],
                            "terminal": true
                        }
                    ]
                }
            }
        }
    }
}

Note the route matcher.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

PhilmacFLy picture PhilmacFLy  路  3Comments

la0wei picture la0wei  路  3Comments

jgsqware picture jgsqware  路  3Comments

dafanasiev picture dafanasiev  路  3Comments

muhammadmuzzammil1998 picture muhammadmuzzammil1998  路  3Comments