RegExp capture group in reverse_proxy upstream

1. Caddy version (caddy version):


2. How I run Caddy:

Docker image 2.0.0-alpine in a local docker-compose.yml file. I’ve mounted a Docker volume to the /data directory. I’ve mounted my own Caddyfile to /etc/caddy/Caddyfile.

a. System environment:


b. Command:

docker-compose up -d

c. Service/unit/compose file:

version: '3.8'
        image: caddy:2.0.0-alpine
        restart: always
            - ./Caddyfile:/etc/caddy/Caddyfile
            - caddy_data:/data
            - apps

d. My complete Caddyfile or JSON config:

:80 {
    @extracthost {
        header_regexp default Host ^([a-zA-Z]+)\.127\.0\.0\.1\.xip\.io$
    reverse_proxy @extracthost http://{http.regexp.default.1}.apps

3. The problem I’m having:

I am attempting to use the value of a header_regexp matcher capture group in a reverse_proxy upstream. It does not appear to be using the RegExp capture group value in the upstream hostname.

4. Error messages and/or full log output:

{"level":"info","ts":1591716044.9300587,"msg":"using provided configuration","config_file":"/etc/caddy/Caddyfile","config_adapter":"caddyfile"}
caddy_1      | run: adapting config using caddyfile: parsing caddyfile tokens for 'reverse_proxy': /etc/caddy/Caddyfile:5 - Error during parsing: parsing upstream URL: parse "http://{http.regexp.default.1}.apps": invalid character "{" in host name

5. What I already tried:

I have tried to use the “srv+” prefix on the upstream hostname to defer to the Docker container’s DNS server. That fails with the same error message above.

6. Links to relevant resources:

I do not have any to provide at this time.

Solved. I just found this section in the reverse_proxy (Caddyfile directive) — Caddy Documentation docs:

If the address is not a URL (i.e. does not have a scheme), then placeholders can be used, but this makes the upstream dynamic.

I recommend highlighting that bit in a callout. It’s important :slight_smile:

Consider this resolved. Thanks!

1 Like

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