我有一个 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/