I cannot use SNICallback

送分小仙女□ 提交于 2019-12-11 05:38:52

问题


I couldn't manage using SNICallback on createServerfunction. When I try the below codes I get an error as Missing PFX or certificate + pricate key.

How can I solve this issue?

var fs = require('fs'),
      tls = require('tls'),
      https = require('https');

var certs = {
    "safe.myDomain.com": {
        key: fs.readFileSync('../SSL/safe/private/key.pem'),
        cert: fs.readFileSync('../SSL/safe/certs/cert.pem') 
    },
    "api.myDomain.com": {
        key: fs.readFileSync('../SSL/api/private/key.pem'),
        cert: fs.readFileSync('../SSL/api/certs/cert.pem')   
    }
}

var httpsOptions = {
    SNICallback: function(hostname, cb) {
      var ctx = tls.createSecureContext(certs[hostname])
      cb(null, ctx])
    }
}

https.createServer(httpsOptions).listen(1443, function() {
    console.log('HTTPS server is listening on port 1443')
})

回答1:


The options to https.createServer must include key and cert as they are required. Even though that set won't be used if SNI provides a hostname.

See tls.createServer where it marks key and cert as required. (Linked from https.createServer.)




回答2:


SNICallback works great in NodeJS. I use it in production all the time. Here is a Stackoverlow answer with a detailed example of how to make it work.

Is it Possible to Dynamically Return an SSL Certificate in NodeJS?



来源:https://stackoverflow.com/questions/29604697/i-cannot-use-snicallback

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!