4

I would like to create a proxy for HTTPS connections with Node.js. I am using the http-proxy library, which works well. I can get a HTTP proxy to work perfectly, but when I try HTTPS, the request to the proxy just times out. Here is my code (a slightly modified version of the node-http-proxy proxy-https-to-https example):

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);

Is there something obvious I'm missing here or is there some other issue?

4

2 回答 2

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”做同样的事情,一切都很好。

于 2013-05-10T18:01:48.780 回答
0

node-http-proxy 存在一个未解决的问题

https://github.com/nodejitsu/node-http-proxy/issues/454#issuecomment-25661401

于 2013-10-04T02:17:04.703 回答