New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problem with redirections #830

Open
jesusprubio opened this Issue May 26, 2015 · 1 comment

Comments

Projects
None yet
2 participants
@jesusprubio

jesusprubio commented May 26, 2015

Hi guys! I'm writing a proxy to change the responses of the webs the user is visiting (of course its browser is pointing to our proxy). I'm using Harmon as suggested in the doc and everything seems to work fine 馃槃 but I'm having a problem which I resume here:

  • If I visit example.com it works well.
  • If I go to nodejs.org it freezes. The problem is that the browser is sending me to "http://nodejs.org" instead of the "https" site (it also works forcing the target, like it's shown in the comments). They are returning a 301 and the problem is there. I suppose I'm losing something here, I've tested with the commented options but I didn't have luck.
  • Moreover the parsed response includes a 200 code instead a 301 one, so I'm a bit confused.

Any idea please?

鉃  node-for-ninjas git:(master) 鉁 curl -I http://nodejs.org
HTTP/1.1 301 Moved Permanently
Server: nginx
Date: Tue, 26 May 2015 18:28:35 GMT
Content-Type: text/html
Content-Length: 178
Connection: keep-alive
Location: https://nodejs.org/
'use strict';

var http = require('http'),
    https = require('https'),
    httpProxy = require('http-proxy'),
    url = require('url'),

    PROXY_PORT = 8000,

    proxy, server;

// Create a proxy server with custom application logic
proxy = httpProxy.createProxy({
    // secure: false,
    // hostRewrite: true,
    // autoRewrite: true,
    // protocolRewrite: true
    // xfwd : true
});

// console.log(parsedUrl);
proxy.on('error', function (err) {
    console.log('ERROR');
    console.log(err);
});

server = http.createServer(function (req, res) {
    var finalUrl = req.url,
    // var finalUrl = 'https://nodejs.org',
        finalAgent = null,
        parsedUrl = url.parse(finalUrl);

    if (parsedUrl.protocol === 'https:') {
        finalAgent = https.globalAgent;
    } else {
        finalAgent = http.globalAgent;
    }
    console.log('URL');
    console.log(finalUrl);

    proxy.web(req, res, {
        target: finalUrl,
        agent: finalAgent,
        headers: { host: parsedUrl.hostname }
        // xfwd : true,
        // secure: false,
        // hostRewrite: true,
        // autoRewrite: true,
        // protocolRewrite: true
    });
    // proxy.close();
});

console.log('listening on port ' + PROXY_PORT);
server.listen(PROXY_PORT);
@yejijie

This comment has been minimized.

Show comment
Hide comment
@yejijie

yejijie Nov 24, 2015

I also encounter the issue when I proxy google, you should add the options--autoRewrite:true.

yejijie commented Nov 24, 2015

I also encounter the issue when I proxy google, you should add the options--autoRewrite:true.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment