I am working on a website builder saas platform, like wordpress, everything works great, starting from the r2 buckets to the kv namespaces, where users export their project into a subdomain url, but now I am trying to add a custom hostname feature where users will be able to map their own business domain with the exported subdomain url. So I used cloudflare for saas, cloudflare custom hostname api. It looks fine, the custom hostname status and the certifictate status are all active and green after user verified their domain, but when they visit their domain, it says 404 not found, even though every other thing works great, I am really confused of how to pull this off, as I am working with this for the first time, any comments on this would be really helpful, hope you understand what I am trying to say, if you need any more details, please let me know.
#Need help with custom hostname api
121 messages · Page 1 of 1 (latest)
I understand your concern. Based on the provided documentation, I'd like to offer some insights.
When adding a custom domain, the domain will not verify if Cloudflare cannot validate a request for an SSL certificate on that hostname. In order for the SSL to validate, ensure Cloudflare Access or a Cloudflare Worker is allowing requests to the validation path: http://{domain_name}/.well-known/acme-challenge/*. (Source: https://developers.cloudflare.com/pages/platform/known-issues/)
Additionally, it's essential to review the Hostname prioritization guidelines as wildcard custom hostnames behave differently than an exact hostname match. (Source: https://developers.cloudflare.com/cloudflare-for-platforms/cloudflare-for-saas/start/advanced-settings/apex-proxying/setup/)
To troubleshoot the 404 error, you may want to check the DNS settings and ensure that the custom hostname is correctly configured. You can also try checking the Cloudflare API response for any errors or warnings related to the custom hostname.
If you're still unsure, please provide more details about your setup, such as the API response and the DNS settings, and I'll try to help you further.
Did you configure the custom hostname on your server as well?
Can you please explain??
Is your server configured to handle the custom domain or only your own subdomain?
Yeah it is configured, my users can already add the cname records to their own business domain and can also successfully verify those from my saas itself, through the cloudflare custom hostname api, I can also see the status at the selection and tls section in my cloudflare dashboard that the domain aaded for that user is green and active but, when the user visits their domain, it says 404, example: my saas is brivovt.com, so the user exported project url is "anything.brivovt.com" which loads without any problem, then user adds this exported subdomain in his own business(business.com) dns panel like "cname: project.business.com, value: anything.brivovt.com" but when he visits project.business.com, it says 404 not found while anything.brivovt.com works!!
Do you have any tutorial or something where I can get the idea that how is it possible to build this multi tenant feature with cloudflare custom hosname??
Ok, can you just confirm for me where the users websites are hosted? On your own server running apache/nginx? Or completely on Cloudflare?
Users website are outside cloudflare
It is in godaddy
I am using my own domain "thebhargab.com" for testing
And on GoDaddy, did you add the custom hostname (project.business.com)?
And the backend of my saas is in gcp
Yeah I did
In the Cloudflare Custom Hostnames, do you use the fallback Origin or a Custom Origin for every custom hostname?
Yeah it am using a custom origin hostname as edge.brivovt.com
Which I even configured in by backend also
ok, so using a custom origin means that the SNI is changed to the custom origin
So Cloudflare will send the custom hostname as the HOST header, but the custom origin as the SNI
Is it the same origin for all sites?
Yeah
Then I'd consider using the fallback origin rather than custom origins
On GoDaddy, do you use nginx/apache or something like that?
No I am not using nginx/apache
I have never done network engineer before
Can I ask you a favour?
Is it possible for you to get on a call with me so that can show my screen on a Google meet?
Or should I drop all my screnshots here?
Ok
Can you share a domain first where you see the 404 right now?
and also show what settings you made for that domain on Cloudflare?
Give me a few minutes, I am dropping them in
sure
Yeah sure, I am giving you everything, give me 10 minutes
here is how I am adding the records from my saas to my domain "thebhargab.com" at godaddy
here is how I have added these records in godaddy
here is my ssl status at cloudflare dashboard
here is my entire dns table of my saas at cloudflare
when I visit book.brivovt.com, it works and I can see the project, but when I visit book.thebhargab.com, it says 404
Yeah
when the website is done, you deploy it on godaddy
ahh ok, i misunderstood that part
It's the website builder which is on gcp and the dns of that website builder is on cloudflare. All the exported subdomain url that gets creates appears there only, just for testing, I bought a domain at godaddy to see if it works....
ok, so on GCP, what settings do you make for a) your own subdomain and b) the custom hostname
I hosted my backend as a webservice on gcp, thats it, from there I got an url, and as you can see in my dns table screenshot, I have mapped those webserive url, in origin and edge cnames
The subdomains gets created on cloudflare itself, via kv namespace
How does your service decide which website to serve for which url?
say I have book.brivovt.com and table.brivovt.com
Where do you make the decision which website to serve?
After user is done making his website, it gets saved as a index.html file in cloudflare r2 bucket. Then I use worker and kv namespace and set that subdomain -key (book.brivovt.com) to r2publicURL(of that website) -variable.
Both are same thing
and have you also added the custom hostname to that configuration?
Yeah I also have it in the same kv namespace where it stores the kv pairs as (key: book.thebhargab.com, value:ther2publicurl of the website)
Ok, so it looks to me like the Worker isn't called for the custom hostname and traffic is sent to GCP
Did you add the Worker via Workers Routes?
In summary: the kvnamespace has two things ( key: book.brivovt.com, value: r2publicurl) and (key:book.thebhargab.com, value:that same r2public url)
Yeah I did
what is the route?
tour-router.bhargabdutta.workers.dev
you can see that in the dns table screenshot too
here is the worker screenshot
yeah, ok
the worker route is *.brivovt.com
so it doesn't work on book.whatever
you need a */* route
and then you need to make exceptions for your own domain that's not supposed to go to the worker
in the dashboard, go to your domain and select workers routes
Then you create exceptions for urls that are not supposed to go to the worker:
And then you create a route for everything else to go to the worker
Or alternatively, you can create a new route for every custom hostname that you add with the specific domain
Alright can you clarify? What do you mean by "it doesn't work for book.whatever" because book.brivovt.com(subdomain of brivovt.com) works but the book.thebhargab.com(custom hostname of another domain) doent work
You have a route for *.brivovt.com/*
But I make a request for book.thebhargab.com
Which doesn't match the Route, so the Worker is not invoked
The route works on the url the client is using, not on the origin you have specified
So option a) add a route for book.thebhargab.com
I would recommend you do that to see if it works
yeah, for example
Long term, you want to use a */* route so you don't have to add a new route for every custom hostname
But for trying, this should work
And it looks like it does work now
ok now this is what I have now
Yeah yeah
Thank you soooo much, let me see and test with / with multiple domains of other people
I'll me right back within a few hours
Thank you so much @mossy bronze
Good luck 😉
I got into a problem, when I add / route, my saas itslef stops working, as it is on brivovt.com
You need to make Worker=None Routes for the things that aren't supposed to go to the worker
It's usually easier that way than adding a new route for every new custom hostname
Alright I restricted all the domains that I don't want in my worker
It's working
I even tested adding records to other domains
These are working too
Thanks a lol @mossy bronze
Just one last thing I want to ask
How to learn these things??
I am just a frontend dev, I started learning backend dev and I have no idea how I got myself into this mess
Please suggest me any learning materials, book name? Courses? Anything
I am desperate to learn
For product specific knowldge like this, you just learn it by spending a lot of time trying things like this.
Cloudflare has excellent documentation for most things, so it's always worth looking at it: