您需要为每个应用程序定义 SSL 选项并分配给每个应用程序,如下所示:
// (A) read SSL files
var fs = require('fs');
var appOneSSLOps = {
key: fs.readFileSync('./path_to_file/private.key'),
cert: fs.readFileSync('./path_to_file/certificate.crt')
}
var appTwoSSLOps = {
key: fs.readFileSync('./path_to_file/private2.key'),
cert: fs.readFileSync('./path_to_file/certificate2.crt')
}
// (B) assign SSL files to app
var https = require('https');
var appOneServer = https.createServer(appOneSSLOps , appOne).listen(443);
var appTwoServer = https.createServer(appTwoSSLOps , appTwo).listen(80);
// (C) route 80 to 443 - > on your machine route port 80 to 443 either manually or by child_process: I assume you are using linux Ubuntu System
childProcess = require('child_process');
var optionExec = {timeout: 3000}; //option(s) for childProcess.exec
childProcess.exec(
'sudo iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 80 -j REDIRECT --to-port 443',
optionExec,
function(err, stdout, stderr) {
}
);
// (D) then enforce SSL - I assume appOne is the main app.
appOne.use(function(request, response, next) {
if(!request.secure) {
response.redirect('https://' + request.headers.host + request.url);
}
next();
});
注意:我假设appOne
是主应用程序。