79820383

Date: 2025-11-14 19:34:54
Score: 2
Natty:
Report link

something such as this is valid?


async function handleRequest(request, env) {
    const url = new URL(request.url);

    // --- 1. Dynamic Content: Route /php traffic via Cloudflare Tunnel ---
    if (url.pathname.startsWith('/php')) {
        
        // --- Massaging Steps for Tunnel Routing ---

        // 1. Path Rewriting: Strip the leading '/php' so Tomcat sees the root path (e.g., /login instead of /phpy/login).
        const originalPathname = url.pathname;
        url.pathname = originalPathname.replace('/php', '');
        
        // 2. Origin Host Assignment: Set the internal hostname for the fetch request. 
        // This hostname is tied to the Cloudflare Tunnel in your Zero Trust configuration.
        url.hostname = '999.999.999.999';
        url.port = '9999';
        url.protocol = 'http:';

        // 3. Request Reconstruction: Clone the original request to apply the new URL and host headers.
        const newRequest = new Request(url, request);

        // 4. Host Header Override: Crucial for Tunnel. Explicitly set the Host header 
        // to the internal hostname so Tomcat knows which virtual host to serve.
        newRequest.headers.set('Host', '999.999.999.999'); 

        // Final fetch request uses the rebuilt request object, sending it securely 
        // through the Cloudflare edge to your connected Tunnel.
        return fetch(newRequest); 
    } 
    
    // --- 2. Static Content: Serve all other traffic directly from R2 ---
    else {
        //usual stuff here...
        //...
        //...
        //...
        //...
        //...
    }
}

// Bind the handler function to the 'fetch' event listener
export default {
    fetch: handleRequest,
};
Reasons:
  • Long answer (-1):
  • Has code block (-0.5):
  • Ends in question mark (2):
  • Self-answer (0.5):
  • Low reputation (1):
Posted by: capn