简体   繁体   English

node.js托管SSL?

[英]node.js hosting with SSL?

So say I have a node.js application that hosts both a HTTP and HTTPS server as described in the question: How to force SSL / https in Express.js 所以说我有一个node.js应用程序,它同时托管HTTP和HTTPS服务器,如问题中所述: 如何在Express.js中强制使用SSL / https

In my code I have the following: 在我的代码中,我有以下内容:

// General configuration settings for production usage
app.configure(function () {
  app.set('port', process.env.PORT || 3000);
  app.set('sslport', process.env.SSLPORT || 4000);
  ...
}

http.createServer(app).listen(app.get('port'), function () {
  winston.info('Express server listening on port ' + app.get('port'));
});

var options = {
  key: fs.readFileSync('key.pem'),
  cert: fs.readFileSync('cert.pem')
};

https.createServer(options, app).listen(app.get('sslport'), function () {
  winston.info('Express server listening on port ' + app.get('sslport'));
});

Which works perfectly fine for a local running node server. 这适用于本地运行节点服务器。

However, I want to publish my site to a cloud hosted provider like Azure Web Sites, Heroku, Nodejitsu, etc. 但是,我想将我的网站发布到Azure网站,Heroku,Nodejitsu等云托管提供商。

All of the cloud hosts seem to set a process.env.PORT value, but only the one. 所有云主机似乎都设置了process.env.PORT值,但只设置了一个。 When my HTTPS server is created this usually results in the app crashing, as the PORT is already in use / access denied / etc. 当我的HTTPS服务器被创建时,这通常会导致应用程序崩溃,因为PORT已经在使用/访问被拒绝/等等。

So how do I create / host a site with a secure login page with only one port to work with!? 那么如何创建/托管一个只有一个端口的安全登录页面的网站呢?

If you use Heroku you get SSL without needing to specify a port in nodejs. 如果您使用Heroku,则无需在nodejs中指定端口即可获得SSL。 All you need to do is listen on the heroku PORT environment variable for http requests. 您需要做的就是监听http请求的heroku PORT环境变量。 Once uploaded to heroku you can address your heroku app using either https (on 443) or http (on port 80). 上传到heroku后,您可以使用https(在443上)或http(在端口80上)来解决您的heroku应用。 Heroku routes either to your server. Heroku路由到您的服务器。

Similarly if using elastic load balancing with EC2 you can make use of SSL termination at the load balancer, and again route to your node server listening on port 80 using using http. 同样,如果使用EC2弹性负载平衡,您可以在负载均衡器上使用SSL终结,并再次使用http路由到侦听端口80的节点服务器。 http://aws.amazon.com/elasticloadbalancing http://aws.amazon.com/elasticloadbalancing

In both cases you can use either self-signed or proper SSL certificates depending upon your need. 在这两种情况下,您都可以根据需要使用自签名或正确的SSL证书。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM