Two Workers + Same Domain + Route Conflicts
I have two Workers that need to share the same domain with path-based routing:
- Site Worker: Should handle sub.domain.com/* (everything except API)
- API Worker: Should handle sub.domain.com/api/* (API endpoints only)
Current wrangler configs:
Site worker:
"routes": [
{ "pattern": "sub.domain.com", "zone_name": "domain.com" },
{ "pattern": "sub.domain.com/", "zone_name": "domain.com" },
{ "pattern": "sub.domain.com/!(api)/", "zone_name": "domain.com" }
]
API worker:
"routes": [
{ "pattern": "sub.domain.com/api/", "zone_name": "domain.com" }
]
Problem:
- sub.domain.com/ → ✅ Works (site worker)
- sub.domain.com/api/ → ❌ 404 (should hit API worker)
What we've tried:
- ✅ Both workers deploy successfully
- ✅ Individual subdomains work (api.sub.domain.com)
- ❌ Adding API routes breaks subdomain routing entirely
- ❌ Removing routes from API worker temporarily fixes subdomain but no path routing
- ❌ Different route pattern combinations
Both workers deploy successfully, but API routes return 404. Do I need to remove custom domains from one worker? Is the route pattern syntax correct for excluding /api/* from the site worker?
Any guidance on proper multi-worker routing appreciated! 🙏
8 Replies
Is there an origin behind
sub.domain.com
, or is the Worker the origin? If not, I would go with a custom domain route, that just covers the entirety of sub.domain.com
. Since the API Worker's route is more explicit it should take precedence over the custom domainThe worker is the origin. I think I started with the combination you're describing (custom domain on site worker and route to api worker) and unfortunately it didn't work.
Should I make a router worker with the subdomain and then route traffic to the other workers from there?
Also, when changing the custom domain between workers do I actually need to wait 24 hours for propagation since it is all still Cloudflare?
You shouldn't need to? This should work:
Site Worker:
API Worker:
It should happen pretty fast, though may take a few minutes to issue a certificate
the zone name is sub.domain.com not domain.com?
yeah, sub.domain.com doesn't work for the zone name
and unfortunately the overall solution doesn't work 🙁
Oh yeah, good catch
By "doesn't work", do you mean requests to sub.domain.com/api/foo still hit the site Worker?
404 not found
I mean, requests to the api give 404 not found
Do you see the request in Workers Observability(you need to enable it if you haven't yet)?
I took the path of least resistance. Instead of using a do.main.com hack, I used a proper domain.com so that it doesn't look silly using api.domain.com. Still no idea how to solve that problem I was trying to solve.