http-party / node-http-proxy

A full-featured http proxy for node.js
https://github.com/http-party/node-http-proxy
Other
13.95k stars 1.98k forks source link

proxy to specific url failed #184

Closed terrywh closed 12 years ago

terrywh commented 12 years ago

I try to proxy a request to http://ptlogin2.qq.com/jump?ptlang=2052&clientuin=304290615&clientkey=0D399336F7C4F553C468779FC21FEC92B77EEE1F3D766C61BC3F3B19AD65E070&u1=http%3A%2F%2Fuser.qzone.qq.com%2F573692919 which actually send a "302 Found" status code, but the proxy failed to send any response which cause Chrome showing an "ERR_EMPTY_RESPONSE" error page.

i found visit that url cause proxyError, and i add some code found it to be "error: { [Error: socket hang up] code: 'ECONNRESET' }"

and i get the similar error when proxy to url http://stats.sports.qq.com/NBAImages/TeamImages/Bulls-1.png

my source code:

var http = require('http'),
    httpProxy = require('http-proxy'),
    url  = require('url');
//
// Create your proxy server
//
httpProxy.createServer(function (req, res, proxy) {
  //
  // Buffer the request so that `data` and `end` events
  // are not lost during async operation(s).
  //
  var buffer = httpProxy.buffer(req);
  var parsed_url = url.parse(req.url);
  var host = "localhost";
  var port = 8001;
  if(typeof(parsed_url.host)=='undefined') {
  } else {
      port = parsed_url.host.split(':');
      host = parsed_url.hostname;
      if(port[1]) {
        port = port[1];
      }else{
        port = 80;
      }
  }
  //setTimeout(function () {
    proxy.proxyRequest(req, res, {
       'host': host,
       'port': port,
       'buffer': buffer,
    });      
  //}, 2000);
}).listen(8000);
AvianFlu commented 12 years ago

The server you're trying to proxy to might be expecting certain headers that aren't being passed through - or it might be rejecting requests that have obviously been proxied.

node-http-proxy sets the x-forwarded-* headers, and your connections may be getting refused because of this.

coderarity commented 12 years ago

@wuhaocn2008 are you still having an issue with this?

terrywh commented 12 years ago

it seems the latter url is ok right now, but the first url is still having some kind of issue when i use the proxy server on CHROME to visit. It keeps loading for a very long time.

coderarity commented 12 years ago

I think the problem is the query part of the URL. I'm going to work on getting rid of the hanging problems in node-http-proxy, I think that's the source of a lot of people's problems. Clear error messages are far more useful than hanging.

coderarity commented 12 years ago

It sounds like it was hanging - I just added a pull request to give reasonable errors instead of just hanging. See nodejitsu/node-http-proxy#216. After that pull request is merged in and a new version is up on npm, can you try it again and tell me what error it gives you (if any)?

terrywh commented 12 years ago

USE proxyError to track the errors, here is what i got when proxy the first url:

{ [Error: socket hang up] code: 'ECONNRESET' }

it seems you are right about the x-forwarded-for thing. when i proxy some url, it will emit that reset error. For the specific url on top, the reason might be that there should not be a header "x-forwarded" or the server will close the connection.

Thank you for all your trouble.