2017-04-25 16 views
2

httpsでExpressでvhostsを実行する方法はありますか?私の現在のコード(非SSL)は、次のようになります。Express vhosts + https

var express = require('express'); 
var vhost = require('vhost'); 
var path = require('path'); 

var appOne = express(); 
var appTwo = express(); 
var appVhosts = module.exports = express(); 

appOne.use(express.static(path.join(__dirname, 'pages'))); 

appTwo.get('/', function(req, res){ 
    res.send('That service isn\'t up right now!') 
}); 

app.use(vhost('siteone.com', appOne)); 
app.use(vhost('sitetwo.com', appTwo)); 

appVhosts.listen(80); 

はしかし、私の知る限り、httpsのモジュールは1つのSSL証明書を受け入れます。

答えて

0

あなたは、各アプリケーションのための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がメインアプリであると仮定します。

0

明らかに、https.Serverはから継承し、addContext()というメソッドを提供します。複数の証明書を設定できます。私はまた、結果を得るためにこのメソッドを使用する非常に小さなパッケージを書いた、https://www.npmjs.com/package/vhttps。そこに私の実装をチェックすることができます。

関連する問題