can't reach nginx port with netcat

1

I have this simple nginx config file:

server {
  listen 4000;
  location / {
    root /usr/share/nginx/html;
    index index.html index.htm;
    try_files $uri $uri/ /index.html =404;
  }
}

nginx service is running, but if I run:

nc -z localhost 4000

I don't get a response, but if I check for port 80 it works:

nc -z localhost 80
Connection to localhost 80 port [tcp/http] succeeded!

I am running nginx on docker with ports 80 and 4000 exposed.

Simon Ernesto Cardenas Zarate

Posted 2018-06-18T18:53:50.027

Reputation: 139

Answers

2

There are two things involved here.

  1. Possibly you're using two different ncs. One mute and one verbose. In this case for the mute one either use the -v switch to have it speak up, or use its exit code $?. (0 for success, 1 for failure.)

  2. If 1. is not the case, then use -v or $? anyhow. Hard to say what might be the reason of this mixed behaviour for different ports.

Illustration:

$ nc -z localhost 8080
$ echo $?
0
$ nc -z localhost 8081
$ echo $?
1
$ nc -zv localhost 8081
nc: connect to localhost port 8081 (tcp) failed: Connection refused
nc: connect to localhost port 8081 (tcp) failed: Connection refused
$ nc -zv localhost 8080
Connection to localhost 8080 port [tcp/http-alt] succeeded! 

user534635

Posted 2018-06-18T18:53:50.027

Reputation:

I am getting connection refused to port 4000, as well as exit code 1 (not success). I think this is related more to an nginx thing that I don't understand – Simon Ernesto Cardenas Zarate – 2018-06-18T19:09:38.403

@SimonErnestoCardenasZarate Check Docker port forwards and firewall. – None – 2018-06-18T19:10:59.137

@SimonErnestoCardenasZarate And nginx status and logs. – None – 2018-06-18T19:11:32.390