Worker reverse proxy script http://www.example.net/dir
→ http://www.domain.com/dir
not routing or preserving the html host headers and dir path from the end user. Example: Reverse proxy must forward host header as host:www.example.net
→ www.domain.com
but it is coming in as host:www.domain.com
routes have been added to the domain as www.example.net/dir/*
My worker script below:
addEventListener(‘fetch’, event => {
event.respondWith(handleRequest(event.request))
})
async function handleRequest(request) {
// Extract the ‘Host’ header from the original request
const hostHeader = request.headers.get(‘Host’);
if (!hostHeader) {
return new Response(‘Host header not found in the request.’, { status: 400 });
}
// Define the URL of your reverse proxy
const proxyURL = 'https://your-reverse-proxy.com';
// Modify the request’s URL to point to the reverse proxy
const url = new URL(request.url);
url.host = new URL(proxyURL).host;
url.protocol = new URL(proxyURL).protocol;
// Forward the modified request to the reverse proxy while preserving the ‘Host’ header
const response = await fetch(url, {
method: request.method,
headers: request.headers,
body: request.body
});
return new Response(response.body, {
status: response.status,
statusText: response.statusText,
headers: response.headers
});
}