Wildcard get subdomain name

Hi, I want to reverse_proxy wildcard subdomain to specific next.js route
But I don’t really know how to get subdomain name on Caddyfile.

So I have a initialization like this:

https://*.website.com {
      reverse_proxy 127.0.0.1:3000/{Subdomain name should be in here}
}

When I type example.website.com I want it this subdomain reverse proxied to 127.0.0.1:3000/example

I tried to search about how to get subdomain name but I wasn’t successful to find solution.

Thank you.

Alright after hard researching, I figured it out it could be done with {http.request.host.labels.2}

1 Like

The reverse_proxy module doesn’t do simultaneous rewrites, so that config wouldn’t work. You need to use the rewrite handler if you need to add a prefix before proxying.

Try this:

*.website.com {
	rewrite * /{labels.2}{uri}
	reverse_proxy 127.0.0.1:3000
}

Also, keep in mind that to use a wildcard certificate, you need to enable the DNS challenge (which involves building Caddy with a DNS plugin, etc):

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