ポート 5403 で node.js サーバーを実行しています。このポートのプライベート IP に Telnet できますが、同じポートのパブリック IP に Telnet できません。
この原因は、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 でリッスンさせるにはどうすればいいですか
ベストアンサー1
を呼び出すときに IPV4 アドレスを指定する必要があります。モジュールlisten()
でも同じ問題が発生しましたhttp
。これを使用すると:
var http = require('http');
var server = http.createServer(function(request, response) {
...
});
server.listen(13882, function() { });
netstat の出力からわかるように、IPV6 のみをリッスンします。
$ 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