Nginx – Clarification of netcat result

netcatnginxUbuntu

I've been trying to debug an issue on our servers and Im confused by this response from netcat. Can anyone explain why Im getting these 2 contradictory messages when checking to see if a port is working? Is it failing to connect via TCP and then succeeding with some other method (*)?

ubuntu@1-2-3-4:/var/log$ nc -vz localhost 7777
nc: connect to localhost port 7777 (tcp) failed: Connection refused
Connection to localhost 7777 port [tcp/*] succeeded!

IP4/6 results

nc -vz4 localhost 7777
Connection to localhost 7777 port [tcp/*] succeeded!
nc -vz6 localhost 7777
nc: connect to localhost port 7777 (tcp) failed: Connection refused

Best Answer

This is happening because your daemon is only listening on IPv4.

IPv6 is the default protocol, so if a given hostname has both IPv4 and IPv6 addresses, the IPv6 address is always tried first.

In your case, localhost has the IPv4 address 127.0.0.1 and the IPv6 address ::1. But your daemon is only listening on 127.0.0.1.

So, when nc tries to connect to localhost it first connects to ::1, finds nothing is listening, and returns Connection refused. It then tries to connect to 127.0.0.1 and finds your daemon.

Related Topic