我正在端口5403上运行一个node.js服务器。我可以在这个端口上远程访问私有IP,但不能在同一端口上telnet到公共IP。
我认为原因是因为node.js只是在监听ipv6。这是
的结果 netstat -tpln
(Not all processes could be identified, non-owned process info
will not be shown, you would have to be root to see it all.)
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State
PID/Program name
tcp 0 0 127.0.0.1:6379 0.0.0.0:* LISTEN
-
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN
-
tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN
-
tcp 0 0 127.0.0.1:5432 0.0.0.0:* LISTEN
-
tcp6 0 0 :::5611 :::* LISTEN
25715/node
tcp6 0 0 :::22 :::* LISTEN
-
tcp6 0 0 ::1:631 :::* LISTEN
-
tcp6 0 0 :::5403 :::* LISTEN
25709/node
如何让节点服务器监听ipv4
答案 0 :(得分:12)
您需要在调用listen()
时指定IPV4地址,我对http
模块有同样的问题。如果我用这个:
var http = require('http');
var server = http.createServer(function(request, response) {
...
});
server.listen(13882, function() { });
它只能监听IPV6,你可以从netstat输出中看到:
$ netstat -lntp
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp6 0 0 :::13882 :::* LISTEN
但是,如果我指定这样的IPV4地址:
var http = require('http');
var server = http.createServer(function(request, response) {
...
});
server.listen(13882, "0.0.0.0", function() { });
netstat会将服务器报告为正在侦听IPV4:
$ netstat -lntp
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 0 0.0.0.0:13882 0 0.0.0.0:13882 LISTEN
我正在使用Ubuntu 16.04和npm 5.3.0。
HTH