Workers and other questions

I am running a content management system for quite a few clients.
I have a set of static sites which are edited within my CMS, that are automatically published / deployed at specific domain names per site.

I am running all the devops for this in a containerised environment, where I am basically copying the ‘working directory’ over into a production container and the site is live.

Each of these sites includes contact forms and also plugins pulling data from some APIs (think TripAdvisor, or Yelp, etc.) which communicate back to the cms root server, secured by CSRF, a CSP and CORS.

I am contemplating moving part of my infrastructure to CloudFlare, because of the load on my infrastructure from spam-bots, automated form submissions etc. becoming a bit too much for me to handle within my setup.

What I’d like to be able to do is deploy static sites to specific domain names and have (captcha verified) form submissions sent back to my CMS, where I can then handle storing this and e-mailing it to customers.

My question for you is, whether it sounds feasible for me to have a fully automated workflow, where I can push sites live (seems like workers sites might allow for this?), which include forms, which are sent to a specific origin?

I know that I will have to drop the CSRF protection, as half the point of doing this would be to leverage CF’s network for performance reasons. But assuming I created a specific end-point within my CMS to receive form submissions, would I then be able to deploy sites to CloudFlare, which then acts as a CDN, with CF workers submitting forms back to my CMS? I saw a restriction on the number of CF workers (30), even on the unlimited plan. Is that restriction enforced on a per-domain basis, or could I have a CloudFlare worker which handles hundreds of sites all with their own forms?

I am currently with a different domain registrar. Would I have to move all my DNS handling to CloudFlare? Could I also connect new domains to CloudFlare and would there be any limits on the number of domains I can connect?

To summarise:

  1. Are CF workers limited to a per-domain setup? So, if I had hundreds of websites, would I be able to use one worker to handle form submissions from all of these websites?
  2. Can I define CORS headers, etc. to secure requests coming from this worker?
  3. Will I need to move all my DNS handling to CloudFlare?
  4. Can I use the CF API to connect websites launched via Workers Sites to their respective domains?
  5. If I couldn’t use workers for this scenario, would it still be possible to deploy sites to CF Workers Sites, but include my own JS to fetch a CSRF token from origin and have that JS send the request straight back to the CMS (and therefore not really using any advanced CF worker functionality, but still having the deploy and hosting and serving up of the site handled by CF?) I could foresee issues with CORS headers here…

Thank you for any assistance.

First of all, sorry for the late reply, saw it now :frowning:

As far as I know, without going with the Enterprise plan route there is a 30 worker per account limit. It may have increased since I can’t find a number on the documentation or maybe they can increase it on a case-by-case basis. @cloonan do you know something about this?

Form submissions yes, you can have a single worker at multiple specific routes on multiple domains. There doesn’t seem to be limits on this.

It’s a V8 JS environment, so I would imagine yes. There are a couple of security limits in place, like the missing ability to change the Host header unless present in your account or timing granularity, but nothing preventing CORS.

DNS yes, sort-of. If the domain is on a Business plan or higher you can use a CNAME set-up for specific activation. Moving the whole DNS is the easier (and cheaper) option.

Would you mind explaining this more? Let me see if I understood though. You can deploy a Worker and assign it via the API to a path, yes.

https://api.cloudflare.com/#worker-routes-properties

Worker Sites is a static file hosting, just like Firebase Hosting, Netlify or GitHub Pages are at the core, you can upload whatever you want (within the 10MB per file limit) with the script sending a request to a second domain identical for all Worker Sites (or an always identical path) which forwards the request to whatever server, API, etc. you want.

Thank you for the great response, Matteo.
This clears up quite a lot of questions for me. I have since done a fair bit of experimenting and found most answers I need.

I also had some discussions with CF support and it seems that the best way of going about this would be to have one worker, which does a KV lookup to serve multiple sites.

The last remaining question is how I might implement such a thing. Do you have any pointers? I have read all the workers documentation and also reviewed the templates (I even went through the Github repos that CF makes public), but I can’t quite figure out where one might create the connection between a worker and multiple KV namespaces (each presumably representing a site).

Thank you again!

2 Likes

No worries, Support would have been something to recommend for sure.

As far as the different KV namespaces I know it can be done in the UI (when you go into the Worker editor), but there is a limit of 20 per worker if I recall correctly. There should be a way to do the same via Wrangler, but I can’t recall it right now…

PS: @sklabnik might know a thing or two about KV for this specific use cases more than I ever could.

Workers Sites uses one namespace for all sites, but does use one worker per site. Also, we upped the limit from 20 namespaces to 100 recently :slight_smile: That said, you’d run into that 30 worker limit not the namespace limit.

If you were implementing this on your own, and wanted to have one worker for all sites, you can bind in multiple namespaces either via the UI or via wrangler’s config, and then each would be accessible via the binding name in your script.

1 Like

Gotcha, didn’t know this!

As I suspected, so the only solution in this case would be either one account every 30 sites or Enterprise?

Hello both,

I really appreciate your help. Many thanks!

So, I spoke to a few people at CF and one of them (with probably a lot of technical knowledge) said this:

“Workers Sites works by uploading your site content to Workers KV (a data store) and then deploying a Worker (JavaScript code) that serves that content. The JavaScript code is just a template – you are free to modify it all you want. So, you could change the code a bit so that the same Worker code handles multiple sites, by extracting the domain name from the URL and including that in the KV lookup. The limit of 30 is on the number of different Workers you can deploy, not on sites, so if you implemented a Worker this way, you could use it to handle as many sites as you want.”

Therefore, my question about how to accomplish this remains. Do you think this could be possible? I don’t see why it shouldn’t be possible, but it’s hard to know from here.

If true, then the limit would be 100 namespaces (ie. Sites) per 30 workers, so 300 sites?

What do you think, any idea how to extract that URL and change the kv lookup?

Thanks so much!

From what @sklabnik said above it seems like each upload for a Worker Site uses a single KV for all uploads… I am not sure though, since my 2 KV namespaces (one preview and one not I presume from the names) for a single site are named according to the site being uploaded.

If it is like he said the you could have worker handling all sites in a single KV namespace, otherwise it would be 100 KV namespaces per account.

I don’t know your budget for this, but maybe try contact their sales department for an Enterprise quote.

Many thanks again :slight_smile:
I should probably note that an enterprise account is out of the question for me until I can raise the funds to make that worthwhile.

Re Workers Sites, I think the confusion here is that I am not actually talking about Workers Sites anymore.

I think the method I was recommended specifically talks about using a normal worker and more or less emulating the behaviour of Workers Sites in some way.

So I would need help with the best way of creating a namespace per site and then having one worker act as a sort of router for requests to these namespaces / sites.

@adrian1

Did you ever figure this out - i.e how to create a KV pair or namespace for a site? I’m looking at something similar too.

Afraid I have not had the time yet. Will update if I do!

This topic was automatically closed after 31 days. New replies are no longer allowed.