crypto.createCredentials()
is deprecated, so use tls.createSecureContext()
instead.
tls.createServer() must have key
and cert
in the options, because they are required in the manual. Perhaps tls.createServer()
uses these parameters as defaults in case SNICallback
is not supported.
var secureContext = {
'mydomain.com': tls.createSecureContext({
key: fs.readFileSync('../path_to_key1.pem', 'utf8'),
cert: fs.readFileSync('../path_to_cert1.crt', 'utf8'),
ca: fs.readFileSync('../path_to_certificate_authority_bundle.ca-bundle1', 'utf8'), // this ca property is optional
}),
'myotherdomain.com': tls.createSecureContext({
key: fs.readFileSync('../path_to_key2.pem', 'utf8'),
cert: fs.readFileSync('../path_to_cert2.crt', 'utf8'),
ca: fs.readFileSync('../path_to_certificate_authority_bundle.ca-bundle2', 'utf8'), // this ca property is optional
}),
}
try {
var options = {
SNICallback: function (domain, cb) {
if (secureContext[domain]) {
if (cb) {
cb(null, secureContext[domain]);
} else {
// compatibility for older versions of node
return secureContext[domain];
}
} else {
throw new Error('No keys/certificates for domain requested');
}
},
// must list a default key and cert because required by tls.createServer()
key: fs.readFileSync('../path_to_key.pem'),
cert: fs.readFileSync('../path_to_cert.crt'),
}
https.createServer(options, function (req, res) {
res.end('Your dynamic SSL server worked!')
// Here you can put proxy server routing here to send the request
// to the application of your choosing, running on another port.
// node-http-proxy is a great npm package for this
}).listen(443);
} catch (err){
console.error(err.message);
console.error(err.stack);
}
Inside the server you can use nodejs package http-proxy to route your https request to your various applications.