【问题标题】:2 node js + express applications sharing https port 443?2 节点 js + express 应用程序共享 https 端口 443?
【发布时间】:2015-11-18 04:56:24
【问题描述】:

我有一个 ec2 实例,我在其中托管一个节点 js 应用程序 (NodeJS1)。我制作了 node js 应用程序的替代版本 (NodeJS2)。

我为 ec2 实例注册了一个域。

我还设置了 ec2 安全组,以便 https 使用 443 - 似乎没有办法告诉 ec2 我想在自定义端口上使用 https。

由于唯一可能的 https 端口是 443,有什么办法可以让两个 nodejs 应用程序共享这个端口?

编辑

我还应该提到我正在为我的节点 js 应用程序使用 express。我目前正在这样启动我的应用程序:

var app = express();
app.set('port', process.env.PORT || 3030);
fs = require('fs')
var sslOptions = {
                key: fs.readFileSync('./mykey.key'),
                cert: fs.readFileSync('./mycert.crt'),
        };
https = require('https').createServer(sslOptions, app);
var server = https.listen(app.get('port'), function (err) {
if (err) throw err;
console.log('listening on ' + server.address().port)
});

另一个更新

我现在正在尝试这个:

var httpProxy = require('http-proxy');
var fs = require('fs');
var proxyTable = {};

proxyTable['example.com/baz'] = 'http://localhost:3030';
proxyTable['example.com/buz'] = 'http://localhost:3031';

var httpOptions = {
    router: proxyTable
};
var httpsOptions = {
    router: proxyTable,
    ssl: {
        key: fs.readFileSync('./mykey.key', 'utf8'),
        cert: fs.readFileSync('./mycert.crt','utf8')}
};
httpProxy.createServer(httpsOptions).listen(443, function(err){
        if (err) throw err;
        console.log('proxy listening...');
});

当我运行它时,我收到以下错误:

/home/ec2-user/MacaronicWebApp/node_modules/http-proxy/lib/http-proxy/index.js:119
    throw err;
          ^
Error: Must provide a proper URL as target
    at ProxyServer.<anonymous> (/home/ec2-user/MacaronicWebApp/node_modules/http-proxy/lib/http-proxy/index.js:68:35)
    at Server.closure (/home/ec2-user/MacaronicWebApp/node_modules/http-proxy/lib/http-proxy/index.js:125:43)

我搜索了这个错误,看起来这个方法已经过时(不再支持)我怎样才能得到这个行为?

【问题讨论】:

  • 您如何设想确定哪个版本应该获得哪个流量?你也许可以用虚拟主机解决你的问题

标签: node.js amazon-web-services amazon-ec2


【解决方案1】:

从技术上讲,您可以将它们作为两个独立的应用程序运行在其他端口上,然后使用 npm 包http-proxy。使用 http-proxy,您可以根据请求的 URI 在 443 上提供所有内容。

例如:

router: {
   'example.com/nodejs1': 'localhost:4000',
   'example.com/nodejs2': 'localhost:4001',
   ...
}

【讨论】:

  • 是的!如果您将example.com 设置为 SSL 域,您只需获取 URI 并使用代理服务与另一个端口上的一个节点应用程序进行通信。然后应用程序将通过代理返回并通过 443 退出。
  • @Micheal 我还是有点困惑,我忘了说我正在使用快递(见编辑)http-proxy 如何与快递一起工作?谢谢!
  • @Micheal 如何在您的示例中使用“路由器”。我在 http-proxy 文档中没有看到它。
猜你喜欢
  • 2019-05-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-31
  • 2017-05-04
  • 2018-01-12
相关资源
最近更新 更多