How to serve arbitrary subdomains from workers

For Workes & Pages, what is the name of the domain?

*.apimate.com

What is the error number?

522

What is the error message?

Connection timed out

What is the issue or error you’re encountering

Can’t serve CNAMEs from other domains with a worker

What steps have you taken to resolve the issue?

  1. Setup worker with *.apimate.com
  2. Setup cname DNS entry on another domain of mine for api.riverdb.com to riverdb.apimate.com
  3. worker servers requests on riverdb.apimate.com, but not through the cname at api.riverdb.com

I want to use the workers for a SAAS where customers bring their own subdomain via a CNAME in their DNS servers. That doesn’t seem to work with Cloudflare Workers. How can I do this?

Have you created a Worker Route for api.riverdb.com?

No, this is a non-starter for a couple reasons.

  1. you can’t create a route for a worker on one domain (apimate.com) for a different domain (api.riverdb.com).
  2. you can’t create routes or workers for domains not managed by Cloudflare. I can’t force customers to use Cloudflare.

I really thought the CNAME would work. There must be a way to support customer domains for SAAS services on Workers? Surely?

Some research shows there’s Cloudflare Workers Platform and Cloudflare for SAAS, but I don’t see how to solve this problem with those services. Workers for Platforms · Cloudflare for Platforms docs

Have you actually tried it? I just created a Worker Route for *.google.com with no problems, and I can assure you that google.com isn’t using Cloudflare or my domain.

1 Like

Oh, how did you set that up? In worker > settings > triggers > routes?

Did you have to add a zone for the domain?

You go into the zone that is the CNAME target and to the “Worker Routes” section, where you can add a Route. https://dash.cloudflare.com/?to=/:account/:zone/workers

If I go to apimate.com, which is the zone with the worker, and to workers routes and try to add a route for api.riverdb.com it gives the error “Route pattern must include zone name: apimate.com

If I go to the zone for riverdb.com, workers routes, I can add a route for api.riverdb.com and the worker executes!

So I can add zones for any external, third-party domain, and add a workers route for them?

I’m not sure what to tell you, it works for me.

If you want, you can create a CNAME record for routestest.apimate.com to routestest.laudian.de, I’ve added SaaS and Workers Routes for that subdomain to test.

And I can see the Route is working and the Hello World! Worker is now available under routestest.apimate.com.

image

Nice, this definately works then.

Please help me understand how to set this up correctly.
Did you create a zone for apimate.com?
On the page with that screenshot showing *apimate.com, where is that. When you click on the zone for laudian.de?

When I go to the zone for apimate.com and try to add a workers route, this is what I get (free account):

No.

Yes, that is in the laudian.de zone and then “Workers Routes”

Hmm, weird. It might be that this is an Business or Enterprise only feature, but using Workers as your fallback origin is explained in the docs, with no limitations mentioned:

I have exactly the same screen that you have when adding api.deferred.dev, but it works for me.

@sdayman, any idea or a hint who I could ask?

Hi Laudian,

I went digging and found that I have to enable Cloudflare SAAS in TLS/SSL > Custom Hostnames.

Once I did that, the restriction was lifted and I could add the route like you.

2 Likes

I had kind of assumed that you had already set up Cloudflare for SaaS, as that is required anyway when someone creates a CNAME to your zone.

But I had no idea this was related to the Hostname limitation in Workers Routes, though it makes sense I guess.

Anyway, glad you got it working.

1 Like

Hi Laudian,

I spoke too soon it seems, can you please double check my work. I have riverdb.apimate.com working, but the CNAME api.riverdb.com, which points at riverdb.apimate.com does not work. It returns a 522 error (load them in your browser to see it in action.)

Here’s how I have everything configured:



Are you sure the 522 error is not coming from the worker?

Did you also set up the Custom Hostname in Cloudflare for SaaS?

Yeah, the error is not from the worker.

I don’t have the custom hostnames setup. I need to setup a fallback origin. What do I use here, since I want all requests to be handled by workers. Make something up?

That’s all explained in Workers as your fallback origin · Cloudflare for Platforms docs .

2 Likes

That did it, thanks!

Both https://riverdb.apimate.com/ and https://api.riverdb.com/ are now working

2 Likes