pages.dev tunnel

I have a Cloudflare tunnel with ingress rules like:
- hostname: xyz.example.com
path: ^/api/
service: http://nodejs-server:8080
- hostname: xyz.example.com
service: https://xyz.pages.dev
- service: http_status:404
- hostname: xyz.example.com
path: ^/api/
service: http://nodejs-server:8080
- hostname: xyz.example.com
service: https://xyz.pages.dev
- service: http_status:404
essentially, i want /api/* traffic to go to the NodeJS server, then other traffic forwarded to Cloudflare pages. is this not supported? or is there a right way to do it?
11 Replies
aleclarson
aleclarsonOP5w ago
oh it just occurred to me that a Page Rule is probably the right way
Hard@Work
Hard@Work5w ago
I would probably go with Workers Assets instead of Pages You can deploy it as a route over xyz.example.com, then create a second route that covers xyz.example.com/api*, and set it to not have a Worker at all
aleclarson
aleclarsonOP5w ago
ooh nice! what's the benefit of the 2nd route? won't the first route be a catch all?
Hard@Work
Hard@Work5w ago
That's the point. The first route sends all requests to xyz.example.com to your Worker. Then, the second rule says that, if the URL is xyz.example.com/api*, bypass the Worker and send it to your backend
aleclarson
aleclarsonOP5w ago
you're interchanging "rule" and "route" so i'm a bit confused :)
Hard@Work
Hard@Work5w ago
Sorry about that. I meant Routes Basically, your Workers Routes for example.com would look like:
xyz.example.com -> YourWorker
xyz.example.com/api* -> No Worker
xyz.example.com -> YourWorker
xyz.example.com/api* -> No Worker
aleclarson
aleclarsonOP5w ago
gotcha. and i assume the cloudflare tunnel will take care of the rest?
Hard@Work
Hard@Work5w ago
Yep. Theoretically, you shouldn't even need the path matching in your config Also note if you want a visual editor, you can make changes to your Tunnel config via the Zero Trust Dashboard
aleclarson
aleclarsonOP5w ago
appreciate the tip. i'm using pulumi though :) it's a bit troublesome of a configuration, only because i use Vite for my web app in development, proxying any /api/* traffic to the wrangler dev process. whereas, your proposed setup inverts that relationship.. do you know if @cloudflare/vite-plugin would help "normalize" the dev setup to mirror the production one? if not, that's fine. i'm sure i can survive :) to clarify, is there a recommended setup for development whereby the Workers Assets are served by Vite
Hard@Work
Hard@Work5w ago
Wait, I'm confused. Wasn't it the other way around? That /* was wrangler dev, and /api/* is whatever secondary backend?
aleclarson
aleclarsonOP5w ago
i'm using a bit of an esoteric setup for a staging environment that i won't get into the details of... but yes, that's what you just helped with, and now i'm curious if i can do the opposite in development, since my web app is using Vite. but i think i can answer that myself, since my dev worker can simply redirect non-API traffic to the Vite dev server. this is what i get for being fancy

Did you find this page helpful?