如何使用自签名证书配置“ https-proxy” node.js模块(从HTTPS到HTTPS)?

时间:2019-06-24 16:55:49

标签: node.js ssl-certificate node-modules http-proxy

当两个服务器都使用自签名证书时,

“ https-proxy” node.js模块无法将请求从HTTPS代理服务器转发到HTTPS工作服务器。代理服务器报告错误:

...node_modules/http-proxy/lib/http-proxy/index.js:120
    throw err;
    ^

Error: self signed certificate in certificate chain
    at TLSSocket.<anonymous> (_tls_wrap.js:1105:38)
    at emitNone (events.js:106:13)
    at TLSSocket.emit (events.js:208:7)
    at TLSSocket._finishInit (_tls_wrap.js:639:8)
    at TLSWrap.ssl.onhandshakedone (_tls_wrap.js:469:38)

在搜索堆栈溢出时,我在HTTPS代理服务器中的process.env["NODE_TLS_REJECT_UNAUTHORIZED"] = 0;之前和HTTPS工作服务器中的httpProxy.createProxyServer之前尝试了https.createServer,但这没什么区别。

我的HTTPS代理服务器是这样实现的:

const httpProxy = require('http-proxy');
const fs = require('fs')
const log = require('log');

const Configuration = require('../configuration/cfg')

require("log-aws-lambda")();

const target = 'https://' + Configuration.work.host + ':' + Configuration.work.port
log.debug("target = %s", target)

httpProxy.createProxyServer({
    ssl: {
        cert: fs.readFileSync(Configuration.proxy.crt),
        key: fs.readFileSync(Configuration.proxy.key),
        ca: fs.readFileSync(Configuration.proxy.ca)
    }
    , target: target
    , secure: true
}).listen(Configuration.proxy.port, function(err) {
    if (err) {
        log.error("ERROR: %s", err)
    }
    else {
        log.notice("Proxy started on port %d", Configuration.proxy.port);
    }
});

我的HTTPS工作服务器是这样实现的:

var https = require('https')
const express = require('express');
const app = express();
const Configuration = require('../configuration/cfg');
const bodyParser = require('body-parser');
const fs = require('fs')
const log = require('log');
require("log-aws-lambda")();

app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());

app.get('/', function (req, res) {
    res.send('hello world')
  })

https.createServer({
    cert: fs.readFileSync(Configuration.work.crt),
    key: fs.readFileSync(Configuration.work.key),
    ca: fs.readFileSync(Configuration.work.ca)
}, app)
.listen(Configuration.work.port, function (err) {
    if (err) {
        log.error("ERROR: %s", err)
    }
    else {
        log.notice('Work listening on port %d', Configuration.work.port)
    }
});

根据错误消息,问题是因为Configuration.work.*Configuration.proxy.*是自签名证书,但是我无法正确配置它

0 个答案:

没有答案