Detecting Forwarded URL with Custom Hostname Setup

Hey folks, I use custom hostname to redirect my customers to a worker. They put CNAME records and use their domains. Each record has a unique id in the subdomain like uniqueid.mysaas.com and I want to get this ID from the request object when a user land on the custom domain. Its not in the header and I tried custom rules etc.
8 Replies
Chaika
Chaika•4mo ago
Unless you've got another layer in the middle, this should be as simple as reading the requested url, ex: var url = new URL(request.url) var uniqueId = url.host.split('.')[0]
makrdev
makrdevOP•4mo ago
User puts their CNAME: Name: subdomain Value: uniqueid.mysaas.com And visit their domain: subdomain.userdomain.com I get the url of the user from request URL: subdomain.userdomain.com What I want to get is: uniqueid.mysaas.com
Chaika
Chaika•4mo ago
ahh ok my bad, did not understand that you were using it as a target. I know Enterprise has custom metadata where you can set custom data per hostname and use in a Worker: >https://developers.cloudflare.com/cloudflare-for-platforms/cloudflare-for-saas/domain-support/custom-metadata/>, guessing you're not Enterprise though?
makrdev
makrdevOP•4mo ago
Yeah, unfortunately I'm not 🙂 So there is no way to achieve this without being enterprise?
Chaika
Chaika•4mo ago
Don't think so. You usually just match off the customer's domain itself. Worth pointing out, the client resolves the CNAME down to the IP (A/AAAA) records to connect to. The only way for Cloudflare to have any idea which specific location the CNAME is pointing to, would be to look it up on each request separately or on setup. If the client was using external dns, that would be relatively expensive
makrdev
makrdevOP•4mo ago
Do you think that adding KV lookup for this on every request would introduce too much latency and cost?
Chaika
Chaika•4mo ago
Depends on frequency. If it's super popular and small in size (and you have a high/decent cacheTtl), you can get relatively fast cached responses, at about 4ms p50 and 8ms p99. The trouble would come with if it was infrequent and not in cached, could have spikes up to 200-300ms (or more) for getting it Cost wise, on every request it'd be a kv request. So you'd be paying $0.50/per million requests extra. You could leverage cache api, which libraries like betterkv can help you with: https://flareutils.pages.dev/betterkv/old, and any cache api hit would be free, but that'd add more latency to the request, maybe ~10ms or so. eh, there's a reason why custom metadata exists (and it's stored in their magical Quicksilver/distributed to every machine storage without a doubt) In the end, I would say it depends on how latency sensitive/popular each of your customer websites is
makrdev
makrdevOP•4mo ago
Thank you so much for detailed explanation!

Did you find this page helpful?