Worker throw 522 Error While redirect to the same domain

Workers

I have domain managed by cloudflare,

example.com

Here are two workers use domain:

 a.example.com
 b.example.com

if I use following code to redirect b to a, worker throws 522.

export default {
  async fetch(request, env) {
    return await handleRequest(request)
  }
}

async function handleRequest(request) {
  const {searchParams, pathname} = new URL(request.url)

  // return new Response("Test")

  return fetch("https://a.example.com/test" + pathname)
}

Is this a Worker BUG?

Thank you

And if I use a different domain in the code:

export default {
  async fetch(request, env) {
    return await handleRequest(request)
  }
}

async function handleRequest(request) {
  const {searchParams, pathname} = new URL(request.url)

  // return new Response("Test")

  return fetch("https://a.worker.dev/test" + pathname)
}

the code worked.

This is expected, here’s a full explanation: Issue With Worker-To-Worker HTTPS request - #8 by harris
TLDR: Same zone (domain) Worker Requests will bypass the other worker and try to use the non-existent origin. If they are on a different zone, the worker request will invoke the other worker properly.

If you want to have these workers use each other, use Service Bindings: About Service bindings · Cloudflare Workers docs, which has lower latency and such as well.

1 Like

Thanks.

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