Routing wildcard subdomains to appropriate publicly accessible S3 buckets

1. The problem I’m having:

I’m trying to set up a Caddy server that handles both wildcard subdomains and root domain requests, serving static files from appropriate publicly accessible S3 buckets

Example Scenario:

If someone visits https://blog.example.com, it should serve files from https://mys3bucket.com/blog/ If someone visits https://example.com, it should serve from a mys3bucket.com/home/ bucket

2. Error messages and/or full log output:

/

3. Caddy version:

v2.8.4 h1:q3pe0wpBj1OcHFZ3n/1nl4V4bxBrYoSoab7rL9BMYNk=

4. How I installed and ran Caddy:

a. System environment:

Fedora 40

b. Command:

dnf install 'dnf-command(copr)'
dnf copr enable @caddy/caddy
dnf install caddy

c. My complete Caddy config:

{
    admin off
}

http://*.example.com {
    reverse_proxy https://myS3bucket.com/{labels.2}
}

5. Links to relevant resources:

/

reverse_proxy doesn’t take a path as the upstream address. It’s only a host and port, with an optional scheme as a shortcut for transport config. See reverse_proxy (Caddyfile directive) — Caddy Documentation

You need to rewrite the URL before proxying.

Also if you’re proxying over HTTPS, you likely need to override the Host header to match the domain the upstream expects reverse_proxy (Caddyfile directive) — Caddy Documentation

http://*.example.com {
	rewrite * /{labels.2}{uri}
	reverse_proxy https://myS3bucket.com {
		header_up Host {upstream_hostport}
	}
}
1 Like

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