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.