Node.js HTTPS代理服务器无法正常工作

时间:2013-04-24 20:30:58

标签: node.js http https proxy

我想为Node.js创建HTTPS连接的代理。我使用的是http-proxy库,效果很好。我可以让HTTP代理完美地运行,但是当我尝试HTTPS时,对代理的请求只会超时。这是我的代码(node-http-proxy proxy-https-to-https示例的略微修改版本):

var http = require("http"),
    https = require("https"),
    httpProxy = require("http-proxy"),
    fs = require('fs');

var httpsConfig = {
  key: fs.readFileSync('./jackos2500-key.pem'),
  cert: fs.readFileSync('./jackos2500-cert.crt'),
};

https.createServer(httpsConfig, function (req, res) {
  res.writeHead(200, { 'Content-Type': 'text/plain' });
  res.write('hello https\n');
    res.end();
}).listen(8000);

httpProxy.createServer(8000, 'localhost', {
  https: httpsConfig,
  target: {
    https: true,
    rejectUnauthorized: false
  }
}).listen(443);

我有什么明显的东西在这里或者还有其他问题吗?

2 个答案:

答案 0 :(得分:0)

我遇到了同样的问题。我没有看到任何记录。这是我的,这有点不同(第二个匹配我从github示例中看到的示例):

var https = require('https');
var httpProxy = require('http-proxy/lib/node-http-proxy');
var helpers = require('http-proxy/test/helpers');
var request = require('request');

https.createServer(function(req, response) {
  try {
    console.log("forwarding https", req);
    var proxy_request = request(req);
    proxy_request.pipe(response);
  } catch (err) {
    console.log("Forwarding server caught error:\n" + err + "\n")
  }
}).listen(9001);

httpProxy.createServer(function (req, res, proxy) {
  console.log("Got request!");
  proxy.proxyRequest(req, res, {
    port: 9001,
    host: 'localhost',
    buffer: httpProxy.buffer(req)
  });
}, { https: helpers.https }).listen(8001);

......我也尝试过更简单的一个:

httpProxy.createServer(9001,
  'localhost',
  { https: helpers.https }).listen(8001)

当我将Firefox的https代理端口设置为8001并转到任何地方时,我得到“连接已重置”。

我用'http'代替'https'做同样的事情,一切正常。

答案 1 :(得分:0)