Skip to content
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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use node-http-proxy 1.x to proxy requests to another http server #603

Closed
ketan-vijayvargiya opened this issue Mar 21, 2014 · 6 comments
Closed

Comments

@ketan-vijayvargiya
Copy link

Hello,
My use-case is this: I have a proxy server running in Nodejs (created using httpProxy.createProxyServer. call it server#1). There is another proxy-server (squid-based. call it server#2) to which server#1 should proxy requests to.
So basically, clients will use my server#1 as proxy for their website calls. server#1 will in turn proxy request to server#2. server#2 will actually call the requested domain and send response back to client via server#1.

I could do this in node-http-proxy v0.x by:
var httpProxy = require('http-proxy')
var proxy = new httpProxy.RoutingProxy();
httpProxy.createServer( function (request, response, proxy) {
// blah blah
proxy.proxyRequest( request, response, { host : server#2_host, port: ... } );
});

How to achieve this in node-http-proxy 1.x is the question?
I expected this to work, but it doesn't:
var proxy = httpProxy.createProxyServer({target: {
host: server#2_host,
port: ...
}});
proxy.web(req, res);

Any pointers on how I can do this?

@ketan-vijayvargiya ketan-vijayvargiya changed the title Unable to use node-http-proxy 1.x to proxy requests to another http server Use node-http-proxy 1.x to proxy requests to another http server Mar 21, 2014
@ketan-vijayvargiya
Copy link
Author

i figured out the problem. looks like a bug in v1.x.
https://github.com/nodejitsu/node-http-proxy/blob/master/lib/http-proxy/common.js#L47 line should be:
outgoing.path = req.url
instead of:
outgoing.path = url.parse(req.url).path

this is similar to what was there in v0.10.x

@jcrugzz
Copy link
Contributor

jcrugzz commented Mar 25, 2014

@KetanSpeaketh could you give me a gist that I can run that fails? in the specific case you listed with the new api there is no server running that would be able to receive requests in order to proxy appropriately.

var http = require('http');
var httpProxy = require('http-proxy');

httpProxy.createProxyServer({
  target: {
    host: server#2_host,
    port: ...
  }
}).listen(3000);

// or

var proxy = httpProxy.createProxyServer();
http.createServer(function (req, res) {
  // We can do more variable proxying in this case
  proxy.web(req, res, {
    target: {
      host: server#2_host,
      port: ...
    }
  });
}).listen(3001);

Both of these should work as expected, otherwise please elaborate on the issue you have.

@ketan-vijayvargiya
Copy link
Author

@jcrugzz - hey, thanks for responding.
please refer to https://gist.github.com/KetanSpeaketh/9790240 while i explain the situation. also, to run these snippets, start the node scripts, connect your browser to the internet via proxy (localhost:3010) and navigate to, say, www.amazon.com

gist#1: simple proxy. works as expected.
gist#2: i first connect to proxy on port 3010, which then routes my request to amazon via another proxy on port 9010. this should work, but doesn't. things work if i make the change suggested in my previous comment.

@jcrugzz
Copy link
Contributor

jcrugzz commented Mar 26, 2014

So this is actually interesting as it made me go back and read why this is in place. @KetanSpeaketh I recommend giving #529 a read. it seems that this is ONLY the case when you proxy to ANOTHER proxy. For this specific use case, I can add an option (something like toProxy) that when truthy, keeps the absolute URI instead of making it relative. How does that sound as a solution to this?

@avilaton
Copy link

Hi @jcrugzz,
I can't seem to get this right. I want to proxy all request made to localhost:3000 to an api server someserver.com:portand this works fine from home. At my office, we have a corporate proxy which makes everything harder. I figure the toProxy attribute is what I want, but I cannot get it to work. This had happened for the ´mikeal/request`library where the fix was to use

var request = require('request'),
  r = request.defaults({'proxy':'https://ourshityproxy.corp.company.com:3128'});

How would I configure your lib to do the same? The following gists show how this works from outside the corporate proxy and also how routing to local addresses works fine behind the proxy.
https://gist.github.com/avilaton/3b72861a75eb31fa6dd3
Any clues?

@samcday
Copy link

samcday commented Sep 23, 2014

@avilaton it looks like the toProxy option is broken in latest http-proxy. I've raised #703

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants