How to add custom hostname for multi tenant application with cloudflare pages ?

In order to have multi tenant support for cloudflare pages I followed this steps. Step 1: Deploy the application to cloudflare pages and set custom domain app.mycompany.com Step 2: Wrote a simple cloudflare worker proxy
// src/index.ts
var src_default = {
async fetch(request, env, ctx) {

const newRequest = new Request(request);
let url = new URL(request.url);

newRequest.headers.set("x-company-id", url.hostname)

url.hostname = 'app.mycompany.com';


return fetch(url.toString(), newRequest)
}
};
export {
src_default as default
};
//# sourceMappingURL=index.js.map
// src/index.ts
var src_default = {
async fetch(request, env, ctx) {

const newRequest = new Request(request);
let url = new URL(request.url);

newRequest.headers.set("x-company-id", url.hostname)

url.hostname = 'app.mycompany.com';


return fetch(url.toString(), newRequest)
}
};
export {
src_default as default
};
//# sourceMappingURL=index.js.map
Step 3: Added the worker route as on different domain mycompany.site *.mycompany.site/* I chose a seperate domain for multi tenant apps, so that its easier to maintain the CNAME if later on I wanted to add subdomains to my primary domain mycompany.com Another reason was that having a seperate domain makes it easier to have a proxy worker route for only purpose of multitenant. ( Please do suggest if there is a better approach, if I could use the existing domain for this) Now From this step onwards, things are getting a little of confusing to me Step 4: I added a wildcard record to mycompany.site DNS AAAA * 100:: Proxied Step 5: Enabled Custom Hostnames for mycompany.site Step 6: Added custom hostname example.com Step 7: I didnt understand this part
No description
13 Replies
Isaac McFadyen
Isaac McFadyen7mo ago
So Cloudflare needs to be able to issue certificates for your custom domain. Basically, you add a custom hostname, then add that DNS record (the _acme-challenge one) for your hostname to allow it to issue a certificate, and then finally add the CNAME for your custom hostname
w3bcode
w3bcodeOP7mo ago
yes tried to follow this steps, but was getting error of 522 / 1016
w3bcode
w3bcodeOP7mo ago
The flow diagram was something like this
No description
w3bcode
w3bcodeOP7mo ago
After a bit of of time, The proxied worker didnt work as well
Chaika
Chaika7mo ago
how do you go from a proxied fallback record to your app record pointing at the pages.dev? needs to be something in the middle lol, there's nothing connecting the fallback 192.2.0.1 to the app cname oh I see the bottom part now, you have *.saas.com/* You need */* or specific routes for each of the customer domains, Worker Routes see the customers hostname (example.com) and therefor wouldn't match on saas.com
w3bcode
w3bcodeOP7mo ago
haha i knew that I definitely did something silly. can you suggest a better way to achieve this ? In a gist, my multi tenant application is at app.saas.com, i want users to access saas at username.saas.com while also being able to set custom domain if they want to.
Chaika
Chaika7mo ago
Well Pages can't use wildcards or anything like that, so a Worker is going to have to sit in front of both ways
w3bcode
w3bcodeOP7mo ago
as for worker as origin i think having a seperate domain for custom hosting would be perfect choice like customer.saas.cloud or something
Chaika
Chaika7mo ago
You could have different workers (one for *.saas.com/*, and one for */* handling all the custom domains, the more specific one wins), or use the same one for both and a singular wildcard, and then just route smartly based off the hostname
w3bcode
w3bcodeOP7mo ago
where / worker route would be on saas.cloud zone
Chaika
Chaika7mo ago
might make some of it cleaner if you had a separate domain for custom hostnames yea
w3bcode
w3bcodeOP7mo ago
now i got it. thanks
Want results from more Discord servers?
Add your server