Fetching origin from Worker without having the origin's URL in the Worker

Hi everybody!

In order for a Worker to respond to requests, there need to be two things:

  • a host with Cloudflare in proxy mode, e.g. somehost.xyz ⇾ ghs.googlehosted.com
  • one or more routes on that host pointing to a Worker

Can the proxy settings, primarily the backend/upstream/origin of the proxied host, be accessed/used by the Worker?

Thank you very much in advance and my apologies if the answer to my question is somewhere in the docs.

If I’m understanding correctly, you have example.com pointing to ghs.googlehosted.com and a Worker also on example.com/* but you still want to access ghs.googlehosted.com from the Worker.

In that case, the Worker just ‘fetches itself’ - fetching the URL the Worker is on will hit the origin. i.e you’ll be passed a request object in your FetchEvent handler, you can just do fetch(request) to pass through the user’s request to the origin.

It’s somewhat documented here: https://developers.cloudflare.com/workers/runtime-apis/fetch/

It is not currently (January 2022) possible to send fetch requests to other Workers (Worker to Worker) within the same zone. The origin server, if any, will receive the request instead.

Fetching itself is still same zone so it goes to the origin.

1 Like

Thank you, @KianNH! :pray:

I have rephrased the title and hope it is easier to understand now: I need to call the origin from a Worker without the Worker knowing anything about the origin, most importantly the URL.

It looks like the self-calling behavior makes that possible. :slight_smile:

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.