【问题标题】:running nodejs application on virtual hosting that listens at different port在侦听不同端口的虚拟主机上运行 nodejs 应用程序
【发布时间】:2016-11-23 18:22:10
【问题描述】:

我在 localhost 上使用端口 8443 运行我的 nodejs 服务器。

http://localhost:8443https://localhost:8443

现在,我已将项目部署在一个 IP 为 xxx.xxx.xxx.xxx 的服务器实例上

我已经为同一个端口启动了节点服务器。

从这个服务器实例,我可以在本地访问节点服务器,就像我可以在我的 localhost 机器上一样。

但是如何从 8443 的服务器外部公开访问节点服务器?

xxx.xxx.xxx.xxx:8443

【问题讨论】:

  • 听(8443, '0.0.0.0');或没有 0.0.0.0:listen(8443);
  • 并确保您的防火墙接受端口 8443 上的入站 TCP 连接
  • @magreenberg RIYAJ 不想将第 80 个重定向到 8080 和 443 到 8443。他正在尝试在定义的端口上全局访问应用程序。另外最好将app设置在普通的webserver后面(例如:nginx、apache),因为每个app都有它的静态文件,不建议在nodejs app上处理。

标签: node.js virtualhost


【解决方案1】:

看看这个简单的例子:

var
  fs = require('fs'),
  express = require('express'),
  app = express(),
  http = require('http'),
  https = require('https');

app.get('/', function(req, res) {
  res.send('Hello World!');
});

var httpServer = http.createServer(app);
httpServer.listen(8080, '0.0.0.0', function () {
  console.log('App listening at http://0.0.0.0:8080');
});

var httpsConfig = {
  key: fs.readFileSync('path/to/certificate.key'),
  cert: fs.readFileSync('path/to/certificate.crt')
};
var httpsServer = https.createServer(httpsConfig, app);
httpsServer.listen(8443, '0.0.0.0', function () {
  console.log('App listening at https://0.0.0.0:8443');
});

【讨论】:

  • 那么,它是 nodejs 的虚拟主机吗?
  • @RIYAJKHAN 这是一个在 8443 端口上运行 expressjs 应用程序的示例代码,它监听服务器的所有网络接口(设备)。如果它是便宜的 php 托管服务,那么它当然不会运行(:有关托管的更多信息将很有用
猜你喜欢
  • 1970-01-01
  • 2019-05-19
  • 1970-01-01
  • 2023-03-17
  • 2018-09-20
  • 1970-01-01
  • 2021-05-03
  • 2019-01-13
  • 2012-03-27
相关资源
最近更新 更多