Wildcard DNS with workers on a custom domain

I found a bunch of discussions, but still confused. I have a worker with a custom domain and a route defined:
route = { pattern = "*example.com/*", zone_name = "example.com" }
route = { pattern = "*example.com/*", zone_name = "example.com" }
And I have the DNS set for AAAA * 100:: Proxied Any request to anything.example.com now get routed to the worker and it responds. So that works. I am trying to make my app route to different handlers depending on the subdomain (my SaaS has subdomain per user, and the app is written in Hono). My idea was to check the Host of the request and make routing based on that. But the Host is always just example.com, without the subdomain, and my in-app routing fails. What am I missing? Developing locally it's a similar story: request to bla.localhost has a Host value of localhost, not bla.localhost. Running wrangler with --host argument just changes the host, but never fixes the missing subdomain.
6 Replies
Rakhim
RakhimOP4mo ago
(I also found info that custom domains don't support wildcard DNS, but I don't understand what it means... I have a custom domain and requests are routed to the worker. Is this "host" obfuscation precisely what that "no support" means?)
Hard@Work
Hard@Work4mo ago
What about based on the URL?
export default {
async fetch(req: Request) {
const { hostname } = new URL(req.url);
switch(hostname) {
case "one.example.com":
return something(req);
case "two.example.com":
return else(req);
default:
return new Response(null, { status: 404 });
}
}
}
export default {
async fetch(req: Request) {
const { hostname } = new URL(req.url);
switch(hostname) {
case "one.example.com":
return something(req);
case "two.example.com":
return else(req);
default:
return new Response(null, { status: 404 });
}
}
}
Rakhim
RakhimOP4mo ago
Same, req.url does not contain the actual request url. Here I am returning req.url:
No description
Rakhim
RakhimOP4mo ago
my wrangler.toml has route = { pattern = "*minifeed.local/*", zone_name = "minifeed.local" }
Hard@Work
Hard@Work4mo ago
When deployed too?
Rakhim
RakhimOP4mo ago
Huh, sorry! Deployed works fine! Thanks!
Want results from more Discord servers?
Add your server