Express Js API Http到Https无法正常工作

时间:2018-01-05 08:52:56

标签: node.js express nginx amazon-ec2 https

我尝试从HTTP到我的域的HTTPS,但是我收到了错误。

我已经使用在端口8081上运行的express.js 4编写了API。我的前端在端口80 Nginx上运行。 SSL证书提供商是亚马逊,我使用负载均衡器成功运行https。

我必须使用HTTPS运行我的API,下面我收到错误

_tls_common.js:85
      c.context.setKey(options.key, options.passphrase);
                ^
Error: error:0B080074:x509 certificate routines:X509_check_private_key:key values mismatch
    at Object.createSecureContext (_tls_common.js:85:17)
    at Server (_tls_wrap.js:776:25)
    at new Server (https.js:27:14)
    at Object.createServer (https.js:48:10)
    at Object.<anonymous> (/ubuntu/server.js:95:25)
    at Module._compile (module.js:571:32)
    at Object.Module._extensions..js (module.js:580:10)
    at Module.load (module.js:488:32)
    at tryModuleLoad (module.js:447:12)
    at Function.Module._load (module.js:439:3)
    at Module.runMain (module.js:605:10)
    at run (bootstrap_node.js:427:7)
    at startup (bootstrap_node.js:151:9)
    at bootstrap_node.js:542:3

我的节点js代码:

var http = require('http');
var https = require('https');

var options = {
   key: fs.readFileSync('/etc/ssl/certs/cert.pem',{passphrase: 'XXXXX'}),
   cert: fs.readFileSync('/etc/ssl/certs/ca-certificates.crt'),

 };

var httpServer = http.createServer(app);
var httpsServer = https.createServer(options, app);

httpServer.listen(8081);

httpsServer.listen(8081);

2 个答案:

答案 0 :(得分:1)

我只是创建密钥和证书文件,并包含如下内容。

var app = require('../app');
var server = https.createServer({
    key: fs.readFileSync('/var/www/html/driver_app/key.key', 'utf8'),
    cert: fs.readFileSync('/var/www/html/driver_app/cert.crt', 'utf8'),
     requestCert: false,
    rejectUnauthorized: false
},app); 

其中app是我的快递应用。这对我来说很好用

答案 1 :(得分:1)

错误“证书例程:X509_check_private_key:键值不匹配”表示私钥与SSL证书不匹配(x509证书)