response headers of backend url
HTTP/2 200
access-control-allow-origin: *
cache-control: max-age=3600
content-type: text/html; charset=utf-8
etag: "4c94e079d45218460e7c0db2d010abfba3a4cff229547fcdeb2d6059333310"
last-modified: Wed, 11 Oct 2023 05:24:20 GMT
strict-transport-security: max-age=31556926
accept-ranges: bytes
date: Tue, 06 Aug 2024 16:07:20 GMT
x-served-by: cache-bom4743-BOM
x-cache: MISS
x-cache-hits: 0
x-timer: S1722960440.785547,VS0,VE269
vary: x-fh-requested-host, accept-encoding
alt-svc: h3=":443";ma=86400,h3-29=":443";ma=86400,h3-27=":443";ma=86400
content-length: 25601
I want to proxy my requests without exposing backend URL
www.frontend.com/login?queryParams -> www.backend.com/login?queryParams
these are response headers after deploying cloudflare worker on this route
HTTP/2 301
date: Tue, 06 Aug 2024 16:09:59 GMT
content-length: 0
location: https://www.backend.com/login
cf-ray: 8af04b640ee63217-BOM
cf-cache-status: DYNAMIC
retry-after: 0
accept-ranges: bytes
alt-svc: h3=":443"; ma=86400
x-cache: HIT
x-cache-hits: 0
x-served-by: cache-bom4751-BOM
x-timer: S1722960600.703772,VS0,VE0
report-to: {"endpoints":[{"url":"https://a.nel.cloudflare.coms=CeseREkv7hOLD9aXW6iL8G9oYh6nSUkSyp5DnEjnw4mTXM3CTN%2F7ppgZ%2BdJYdBjwH5edvop1ztV7SWEKm6bMtBRHq5o0eKetmrvG%2Bc3x7Dtb%2FfIjA%2BJPCg%3D%3D"}],"group":"cf-nel","max_age":604800}
nel: {"success_fraction":0,"report_to":"cf-nel","max_age":604800}
see above response headers show its a permanent redirect
my wrangler.toml
```
name = "fronted-to-backend-proxy"
main = "src/index.ts"
compatibility_date = "2024-07-29"
compatibility_flags = ["nodejs_compat"]
routes = [
{ pattern = "www.frontend.com/login", zone_name = "company_domain.com"},
{ pattern = "www.frontend/refer", zone_name = "company_domain.com"},
]
```
index.ts
export default {
async fetch(request, env, ctx): Promise<Response> {
// Parse the incoming request URL
const url = new URL(request.url);
// Define the URL patterns and their corresponding proxy endpoints
const proxyMappings: { [key: string]: string } = {
"/login": "https://www.backend.com/login",
"/refer": "https://www.backend.com/refer"
};
// Check if the URL pathname matches any of the specified patterns
if (proxyMappings[url.pathname]) {
// Modify the URL to point to the new endpoint
const proxyUrl = new URL(proxyMappings[url.pathname]);
// Append query parameters to the proxy URL
url.searchParams.forEach((value, key) => {
proxyUrl.searchParams.append(key, value);
});
// Create a new request with the modified URL, including headers and session
const modifiedRequest = new Request(proxyUrl.toString(), {
method: request.method,
headers: request.headers,
body: request.body,
redirect: request.redirect
});
// Fetch the modified URL and return the response
return fetch(modifiedRequest);
}
// If no match is found, return a 404 response
return new Response("Not Found", { status: 404 });
}
} satisfies ExportedHandler<Env>;
frontend.com is subdomain configured with cname <>
backend.com is also a subdomain configured with A record
both have flexible ssl configured in cloudflare
Any suggestion is much appreciated.