Routing not working on root domain

I'm setting up a worker on a brand new cloudflare account. I have tested that it works. I also set up a test subdomain and router for that and found that the worker works properly. However, when I set up routing for the root domain, the routing doesn't seem to be working. I confirmed that the worker is not getting called in the metrics and logs. Here is the current setup: DNS (for example.com) : * A @ <ip address> :cloudflare: (proxy enabled) * A www <ip address> :cloudflare: (proxy enabled) * CNAME test test.com :cloudflare: (proxy enabled) Routes: * *example.com/blog* -> worker * example.com/blog* -> worker I have tried many different routing patters (such as *example.com/* and others). I tried deleting the A DNS records and re adding them. I'm really confused about why this isn't routing to my worker. Any help would be greatly appriciated.
16 Replies
shellsort
shellsort•3mo ago
So you want to route a subdomain to a url with cf rules? Or are you talking about a worker router? What's the goal?
Joe
JoeOP•3mo ago
The goal is to route the root domain through the worker to reverse proxy a blog I just tested with the subdomain to make sure the worker was working as expected
shellsort
shellsort•3mo ago
What's the proxy supposed to do? Or what's your goal with it? Aaaaah I think I understand
Joe
JoeOP•3mo ago
Its supposed to make it so that if you got to example.com/blog you get content from a wordpress server
shellsort
shellsort•3mo ago
You want to apply worker responses only to the /blog path and let everything else be served by the main domain So you have an IP A with some content and an IP B with your wordpress blog, right?
Joe
JoeOP•3mo ago
right I have run this setup before with other cloudflare accounts too, and its worked fine. But for some reason the routing isn't going into effect for the root domain (example.com)
shellsort
shellsort•3mo ago
Can you show us the redirect rule you tried?
Joe
JoeOP•3mo ago
Its not a redirect rule, its a worker route
No description
shellsort
shellsort•3mo ago
No that should work. 🤔 I have the same issue interestingly when testing. Have you considered just having one worker that redirects instead of using routes instead? Could be a workaround
Joe
JoeOP•3mo ago
How would that work?
shellsort
shellsort•3mo ago
Sth like this:
addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request))
})

async function handleRequest(request) {
const url = new URL(request.url)
if (url.pathname.startsWith('/blog')) {
// Proxy to Server B
url.hostname = 'www.example.com'
return fetch(url.toString(), request)
} else {
// Proxy to Server A
url.hostname = '[some other ip here]'
return fetch(url.toString(), request)
}
}
addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request))
})

async function handleRequest(request) {
const url = new URL(request.url)
if (url.pathname.startsWith('/blog')) {
// Proxy to Server B
url.hostname = 'www.example.com'
return fetch(url.toString(), request)
} else {
// Proxy to Server A
url.hostname = '[some other ip here]'
return fetch(url.toString(), request)
}
}
Or of course by calling the worker itsself. And I think there should also be a way to get page rules to work for this.
okHadi (parhlai.com)
okHadi (parhlai.com)•3mo ago
hey @Joe @shellsort , did you manage to get it to work? I have my root domain "example.com" setup to serve a worker. On a specific path "example.com/api/*" I want to serve another worker. I have tried it with trace and even in the logs it shows that my second worker is accessed when /api/ route it hit, but it shows a 404 error based on the first worker. What's going wrong here?
Joe
JoeOP•3mo ago
No I never got it to work
okHadi (parhlai.com)
okHadi (parhlai.com)•3mo ago
nvm looks like the DNS needed to clear up. I got it working now. Just added this to wrangler.json:
"route": "example.com/api/*", and waiter for a bit. I guess it was a caching issue. I can access my other worker now.
cubby
cubby•4w ago
@okHadi (parhlai.com) Did you use a custom domain on the site worker or just a route?
okHadi (parhlai.com)
I think it was a custom domain or a route on a custom domain, I'll have to check and confirm haven't touched this in a while

Did you find this page helpful?