node.js - 2 Node js + express 应用程序共享 https 端口 443?

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

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

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

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

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

编辑

我还应该提到我正在为我的 Node 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)

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

最佳答案

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

例如:

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

关于node.js - 2 Node js + express 应用程序共享 https 端口 443?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33747609/

相关文章:

.htaccess - 如何使用 .htaccess 将子域别名/重定向到另一个域子文件夹

c++ - libuv - 自由写入缓冲区

html - Angular 6,ngFor 标识符未定义

javascript - 尝试删除 Node.js Express MongoDB Jade 应用程序中的条目

amazon-web-services - ECS集群: Getting tasks in different services to run on different EC2 instances

selenium - 对于那些使用 Selenium Grid 2 运行持续集成构建的人,您预计在 Amazon EC2 上运行 24/7 需要支付多少钱?

node.js - Mongoose -如何对人口进行多标准排序

amazon-web-services - AWS S3 静态站点托管 ACL 和权限

amazon-web-services - 使用 EC2 命令行工具选择免费层级 Amazon 系统镜像 (AMI)

ubuntu - AWS Ubuntu 通过 Putty