522 when worker proxies to another worker

I’m attempting to create a proxy worker that adds a header to all requests and proxies to the original request url. I’ll use example.com in this case, but I’m using a domain whose DNS is managed trough Cloudflare. This proxy worker is bound to two routes: static.example.com which resolves to a site hosted off Cloudflare and worker.example.com which resolves to a worker bound to a custom domain.

Below is the implementation and configuration for the proxy

export interface Env {
}

export default {
	async fetch(request: Request, env: Env, ctx: ExecutionContext): Promise<Response> {
		ctx.passThroughOnException();
		const response = await fetch(request);

		const responseWithCookies = new Response(response.body, response);
		responseWithCookies.headers.append("Proxied-By", "A worker proxy")

		return responseWithCookies;
	},
};
name = "proxy"
main = "src/index.ts"
compatibility_date = "2023-10-10"

routes = [
    { pattern = "worker.example.com/*", zone_name = "example.com" },
    { pattern = "static.example.com/*", zone_name = "example.com" },
]

and for the destination worker

export interface Env {
}

export default {
	async fetch(request: Request, env: Env, ctx: ExecutionContext): Promise<Response> {
		return new Response('Hello World!');
	},
};
name = "worker"
main = "src/index.ts"
compatibility_date = "2023-10-10"

routes = [
    { pattern = "worker.example.com", custom_domain = true },
    { pattern = "plain.example.com", custom_domain = true },
]

When deployed I see all calls to worker.example.com logged in the proxy’s logs, but nothing in the destination worker’s logs. All calls come back with a 522 error.

Any idea’s what I’m doing wrong? I’m trying to follow documentation here https://developers.cloudflare.com/workers/configuration/routing/custom-domains/#interaction-with-routes and don’t see what I’m doing incorrectly.

Has anyone else run into this issue? I’m still seeing it.

Workers can’t fetch Workers, even if the other Worker is on a Custom Domain, despite what the docs say. It’s a known bug. You could try this workaround here: 🐛 BUG: Worker <-> Worker request over `custom_domain` returns instant 522 timeout response · Issue #787 · cloudflare/workerd · GitHub, but the more preferable thing to use would be Service Bindings (But more messy in your use case): Service bindings - Configuration · Cloudflare Workers docs

Thanks Chaika, I appreciate your response and the link to the Github issue. I’ll use service bindings for now, but as you say, it’s not as nice of a solution.

If anyone from Cloudflare is watching to the thread, I’d suggest updating the documentation (Custom Domains · Cloudflare Workers docs) to match reality, since hte bug Chaika links to has been around for over a year.

+1 on updating the docs. Cmon’ Cloudflare, you wasted my time on this issue because I read the docs and they lied to me?

But it works for me using service bindings, which are pretty straightforward

Master page:
async fetch(request, env) {
const proxyWorkerResponse = await env.ProxyWorker.fetch(request);

Proxy Page:
export default {async fetch(request, env) {
console.log("request(request) ", request);
pathName = request.url.pathname;

const originalResponse = await fetch(request);
url = new URL(originalResponse.url);

if(pathName ==“/”){
pathName =“”;
}

I am a total of 4 pages, One Master and 3 sub pages all feeding their data into the master.
ATM its a work in progress, but the principle works
The pages all get called individually in the logs
image