Workers as fallback origin for custom hostnames
Hey, I'm trying to use cloudflare workers as a fallback origin for custom hostnames, the backend is in django
and it excpects a sessionid cookie to be passed on every request, but since cloudflare worker is intercepting
the call , i think the cookie is not being sent, kindly help me with same
```js
addEventListener("fetch", (event) => {
event.respondWith(handleRequest(event.request));
});
const urlMap = {
"app.zing.com": "https://bangers.name.io",
"eu.zing.com": "https://bangers-eu.name.io",
};
async function handleRequest(request) {
const url = new URL(request.url);
const targetOrigin = urlMap[url.hostname];
if (!targetOrigin) {
return new Response("Not Found", { status: 404 });
}
const targetUrl = new URL(targetOrigin + url.pathname + url.search);
const modifiedRequest = new Request(targetUrl, request);
modifiedRequest.headers.set("Host", new URL(targetOrigin).hostname);
modifiedRequest.headers.set("X-Forwarded-Host", url.hostname); // I want the origin request host to be same
let response = await fetch(modifiedRequest, { redirect: "manual" });
// Handle redirects to prevent infinite loop
if (response.status >= 300 && response.status < 400) {
let location = response.headers.get("Location");
if (location) {
let newUrl = new URL(location, targetUrl);
if (urlMap[newUrl.hostname]) {
return Response.redirect(newUrl.toString(), response.status);
}
}
}
return response;
}
and it excpects a sessionid cookie to be passed on every request, but since cloudflare worker is intercepting
the call , i think the cookie is not being sent, kindly help me with same
```js
addEventListener("fetch", (event) => {
event.respondWith(handleRequest(event.request));
});
const urlMap = {
"app.zing.com": "https://bangers.name.io",
"eu.zing.com": "https://bangers-eu.name.io",
};
async function handleRequest(request) {
const url = new URL(request.url);
const targetOrigin = urlMap[url.hostname];
if (!targetOrigin) {
return new Response("Not Found", { status: 404 });
}
const targetUrl = new URL(targetOrigin + url.pathname + url.search);
const modifiedRequest = new Request(targetUrl, request);
modifiedRequest.headers.set("Host", new URL(targetOrigin).hostname);
modifiedRequest.headers.set("X-Forwarded-Host", url.hostname); // I want the origin request host to be same
let response = await fetch(modifiedRequest, { redirect: "manual" });
// Handle redirects to prevent infinite loop
if (response.status >= 300 && response.status < 400) {
let location = response.headers.get("Location");
if (location) {
let newUrl = new URL(location, targetUrl);
if (urlMap[newUrl.hostname]) {
return Response.redirect(newUrl.toString(), response.status);
}
}
}
return response;
}