【问题标题】:Webpage not publicly accessible via port 80网页无法通过 80 端口公开访问
【发布时间】:2016-11-04 23:42:25
【问题描述】:

我有以下用 Node/Koa 编写的代码,服务于port 80

const 
koa = require('koa'),
route = require('koa-route'),
network = koa(),
common = require('koa-common'),
PORT = 80;

// enable logger middleware
network.use(common.logger('dev'));

// enable static middleware
network.use(common.static(__dirname + '/public'));

network.use(route.get('/', index));
network.use(route.get('/about', about));
function *index() {
 this.body = "<h1>Is this message on my computer, or on yours...?</h1>";
}
function *about() {
 this.body = "<h2>How about now...</h2>";
}
var server = network.listen(PORT, function () {
    console.log('Listening on port %d', server.address().port);
});

我为主机保留了一个地址 (168.192.1.91),在port 80 上设置了到此地址的端口转发,当通过任何协议连接时,在 Windows 10 防火墙中为port 80 设置了一个例外,并且用You Get Signal测试:

确认端口当前处于打开状态。当我浏览到localhost:80 时,我可以看到默认页面。但是,当我在浏览器中输入计算机的公共 IP 地址时(我输入的是我部分模糊的地址,我认为应该是正确的):

此页面无法加载并出现以下错误:

This site can’t be reached

109.[...] took too long to respond.
Try:
Reloading the page
Checking the connection
Checking the proxy and the firewall
ERR_CONNECTION_TIMED_OUT

并且 Koa 日志中没有任何活动(当我通过 localhost:80 浏览到那里时,它记录正​​常)。任何想法可能会阻止连接?

我也试过像这样添加主机地址作为第二个参数:

const HOST = "127.0.0.1";

    var server = network.listen(PORT, HOST, function () {
    console.log('Listening on port %d', server.address().port);
});

这适用于环回地址,但是当我指定我的公共 IP 时出现此错误:

C:\Sites\order-server>node --harmony cheese.js
events.js:154
      throw er; // Unhandled 'error' event
      ^

Error: listen EADDRNOTAVAIL [My public IP here]:80
    at Object.exports._errnoException (util.js:890:11)
    at exports._exceptionWithHostPort (util.js:913:20)
    at Server._listen2 (net.js:1221:19)
    at listen (net.js:1270:10)
    at net.js:1379:9
    at _combinedTickCallback (node.js:386:13)
    at process._tickCallback (node.js:407:11)
    at Function.Module.runMain (module.js:449:11)
    at startup (node.js:142:18)
    at node.js:939:3

【问题讨论】:

  • 你的路由器的端口也打开了吗?
  • @BubbleHacker 我设置了端口转发。这不是一回事吗?

标签: node.js server webserver portforwarding koa


【解决方案1】:

也许 koa 默认只绑定到 127.0.0.1,所以你可以尝试使用 .listen(80, '0.0.0.0') 显式绑定到任何主机

否则,如果防火墙和端口转发进行了相应配置,问题可能是您的 Internet 提供商在某处阻止了传入连接。你可以尝试使用超过 1024 的端口,如果它只阻止下面的端口。

【讨论】:

  • 啊。我想你可能是对的朱利安。我想知道是否需要更改它所服务的 IP 地址,但我在任何文档中都找不到。我想我可以只使用 express,但我认为我更喜欢 Koa,因为我喜欢它是模块化的。我现在需要睡觉,但明天我可能会尝试像明天使用 express 一样将 IP 地址作为第二个参数添加,看看是否可行。如果是,我会接受你的回答:)。
  • 好的,我最后在睡觉前尝试了它,但它确实有效:(。添加了我遇到的问题的错误消息。
  • 没关系。我只需要指定计算机 IP 而不是公共 IP,然后端口转发处理其余部分。现在一切正常。
  • 没错,因为在您的本地网络中,您使用私有 IP 进行通信,而路由器将此 IP 更改为公共 IP (NAT)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-02
  • 2013-10-04
相关资源
最近更新 更多