我想为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);
我有什么明显的东西在这里或者还有其他问题吗?
答案 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)
node-http-proxy
存在未解决的问题https://github.com/nodejitsu/node-http-proxy/issues/454#issuecomment-25661401