Wildcard get subdomain name to reverse proxy

1. The problem I’m having:

my caddy config is like below. I want to replace hardcoded store4 subdomain part with dynamic one where wildcard * refers to it.

I use caddy in my local machine for testing httpOnly cookies.

*.admin.localhost {
        reverse_proxy /api/* https://store4.example.com {
                # request header going upstream to the backend.
                header_up host store4.example.com

                header_down host store4.admin.localhost 
                header_down set-cookie "example.com" "admin.localhost"
        }

        reverse_proxy * http://localhost:5173
}

2. Error messages and/or full log output:

I faced with various syntax errors

3. Caddy version:

v2.6.2

4. How I installed and ran Caddy:

a. System environment:

Windows 11

b. Command:

caddy run

d. My complete Caddy config:

 *.admin.localhost {
        reverse_proxy /api/* https://store4.example.com {
                # request header going upstream to the backend.
                header_up host store4.example.com

                header_down host store4.admin.localhost 
                header_down set-cookie "example.com" "admin.localhost"
        }

        reverse_proxy * http://localhost:5173
}

5. Links to relevant resources:

similar questions

Please use the latest version, v2.6.4

And what are those errors? We can’t help unless you’re specific about the behaviour you’re seeing.

Thanks for your help!

The goal is to replace the hardcoded “store4” subdomain in the path with a dynamic alternative that can be determined at runtime.

I use Caddy as a proxy server locally to test a frontend application that requires HTTPS, subdomains, and HTTP-only cookies for proper browser functionality.

As a frontend engineer, I do not have sufficient knowledge about Caddy to accomplish the task.
I updated caddy to latest version. v2.6.4.
Code in screenshot works correctly.

but the code I tried below gives error

*.admin.localhost {
        reverse_proxy /api/* https://{labels.2}.example.com {
                header_up host {labels.2}.example.com
                header_down host {labels.2}.admin.localhost 
                header_down set-cookie "example.com" "admin.localhost"
        }

        reverse_proxy * http://localhost:5173
}

Errors:

2023/04/14 07:04:57.707 INFO    using adjacent Caddyfile
Error: adapting config using caddyfile: parsing caddyfile tokens for 'reverse_proxy': Caddyfile:2 - Error during parsing: due to parsing difficulties, placeholders are not allowed when an upstream address contains a scheme

See reverse_proxy (Caddyfile directive) — Caddy Documentation

Using https:// is a shortcut for transport http with the tls option enabled. The scheme is only parsed at Caddyfile adapt time, so if there’s anything dynamic in the upstream address (i.e. replaced at runtime), that shortcut can’t be used.

reverse_proxy /api/* {labels.2}.example.com {
	header_up Host {upstream_hostport}
	header_down Set-Cookie "example.com" "admin.localhost"
	transport http {
		tls
	}
}
1 Like

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