基本上,在 http-proxy npm 下面是 Node 使用的一些网络库(特别是http://nodejs.org/api/https.html和 TLS)。即使我的 Apache 能够通过在我的浏览器中访问它来在没有代理的自签名证书上很好地连接我:
https://localhost:8002
您需要建立一个证书颁发机构以克服 Node 中的“无法验证叶签名”错误(我使用了SSLCACertificateFile选项)。然后,你会被“self_signed_cert_in_chain”击中。这导致一些谷歌结果表明 npm 放弃了自签名证书,但我很确定这与 Node.js 无关。
你最终得到的是一些人表明你在你的 https 代理中使用process.env.NODE_TLS_REJECT_UNAUTHORIZED或rejectUnauthorized 。如果您深入研究 http-proxy 源,您会发现它接受代理选项。用这个:
/**
* Module dependencies
*/
// basic includes
express = require('express');
fs = require('fs');
http = require('http');
https = require('https');
httpProxy = require('http-proxy');
require('child_process').spawn(__dirname+'/../../../dependencies/apache/bin/httpd.exe',['-f',__dirname+'/../../../dependencies/apache/conf/httpd-payments.conf']);
var app = module.exports = express();
app.set('port', process.env.PORT || 8001); // we sometimes change the port
// creates an output object for this particular request
//app.use(express.cookieParser(''));
//app.use(express.bodyParser());
//app.use(express.methodOverride());
proxy = httpProxy.createProxyServer();
proxy.on('error', function (err, req, res) {
console.log(err);
res.send(500,err);
res.end();
});
app.all('*',function(req,res,next) {
var options = {
hostname: '127.0.0.1',
port: 8002,
rejectUnauthorized: false,
key: fs.readFileSync(__dirname+"/../../../deployment/server.key.pem"),
cert: fs.readFileSync(__dirname+"/../../../deployment/server.crt.pem")
};
agent = new https.Agent(options);
try {
proxy.web(req,res, {
target: "https://localhost:8002",
proxyTimeout: 30,
agent: agent
});
} catch(e) {
// 500 error
res.send(500,e);
}
})
/**
* Start Server
*/
var options = {
key: fs.readFileSync(__dirname+"/../../../deployment/server.key.pem"),
cert: fs.readFileSync(__dirname+"/../../../deployment/server.crt.pem")
};
server = https.createServer(options,app).listen(app.get('port'), function () {
console.log('Running payments server on port ' + app.get('port'));
});