FG
📡 Networking

can't handle www.host

Freshabout 21 hours ago
Mar 14, 20260 views
Confidence Score55%
55%

Problem

Suppose I wanted to proxy a site like cb2.com -- visiting this site, you'll see that it immediately redirects you to www.cb2.com. This is a problem, as I'm unable to proxy the content of the site as my browser begins accessing www.cb2.com directly instead of continuing to proxy via localhost. I've tried simply setting the host to be www.cb2.com instead of cb2.com, but it fails. No errors, the browser just waits. [code block]

Unverified for your environment

Select your OS to check compatibility.

1 Fix

Canonical Fix
Unverified Fix
New Fix – Awaiting Verification

Implement HTTP Proxy with Host Header Modification

Medium Risk

When attempting to proxy a site like cb2.com, the server detects the host as 'cb2.com' and redirects to 'www.cb2.com'. This redirection causes the proxy to fail because the browser tries to access the redirected site directly instead of through the proxy. By modifying the Host header in the proxy request, we can ensure that the request is sent to the correct domain without triggering a redirect.

Awaiting Verification

Be the first to verify this fix

  1. 1

    Set Up HTTP Proxy Server

    Use a Node.js HTTP proxy library like 'http-proxy' to create a proxy server that can intercept requests and modify headers.

    javascript
    const http = require('http');
    const httpProxy = require('http-proxy');
    
    const proxy = httpProxy.createProxyServer({});
    
    const server = http.createServer((req, res) => {
        req.headers['host'] = 'www.cb2.com'; // Modify the Host header
        proxy.web(req, res, { target: 'http://www.cb2.com' });
    });
    
    server.listen(3000);
  2. 2

    Modify Host Header

    Ensure that the Host header is explicitly set to 'www.cb2.com' in the proxy server configuration. This prevents the browser from following the redirect.

    javascript
    req.headers['host'] = 'www.cb2.com';
  3. 3

    Test Proxy Functionality

    After setting up the proxy server, test it by accessing http://localhost:3000 in your browser. This should route the request through your proxy without redirection.

  4. 4

    Handle HTTPS Requests

    If the target site uses HTTPS, ensure your proxy server can handle secure connections by using the 'https-proxy-agent' package. Update your proxy configuration accordingly.

    javascript
    const HttpsProxyAgent = require('https-proxy-agent');
    
    proxy.on('proxyReq', (proxyReq, req, res, options) => {
        // Set the agent for HTTPS
        proxyReq.setHeader('Host', 'www.cb2.com');
    });

Validation

To confirm the fix worked, access http://localhost:3000 in your browser. You should see the content of www.cb2.com without any redirection issues. Additionally, check the network tab in your browser's developer tools to ensure the requests are being sent to the correct host.

Sign in to verify this fix

Environment

Submitted by

AC

Alex Chen

2450 rep

Tags

proxyhttpnode.jslow-priorityneeds-investigation