10

OS: Funtoo. I have bound NGINX to port 81 (I want to run it alongside my Apache server for a short time for ease of transition), and it listens at the port (If I point at another port, using wget I get "Connection refused", but using port 81 I get "connected") but it never serves an HTML response of any kind!

When running a wget on the port, from the localhost, I get:

# wget localhost:81
-2014-04-16 23:56:45- http://localhost:81/
Resolving localhost... 127.0.0.1
Connecting to localhost|127.0.0.1|:81... connected.
HTTP request sent, awaiting response...

On another computer...

$ wget 192.168.18.42:81
-2014-04-16 23:57:19- http://192.168.18.42:81/
Connecting to 192.168.18.42:81... connected.
HTTP request sent, awaiting response...

Nothing ever happens after that. The documents exist, it's the normal Funtoo nginx.conf.

UPDATE: I can make it listen to port 80, but it still rattles me that I can't get it to work on any port....

netstat -aWn | grep 81 | grep LISTEN
tcp 60 0 0.0.0.0:81 0.0.0.0:* LISTEN

Edit: Configuration files:

user nginx nginx;
worker_rlimit_nofile 6400;

error_log /var/log/nginx/error_log info;

events {
    worker_connections 1024;
    use epoll;
}

http {
    include /etc/nginx/mime.types;

    # This causes files with an unknown MIME type to trigger a download action in the browser:
    default_type application/octet-stream;

    log_format main
        '$remote_addr - $remote_user [$time_local] '
        '"$request" $status $bytes_sent '
        '"$http_referer" "$http_user_agent" '
        '"$gzip_ratio"';

    client_max_body_size 64m;

    # Don't follow symlink if the symlink's owner is not the target owner.

    disable_symlinks if_not_owner;
    server_tokens off;
    ignore_invalid_headers on;

    gzip off;
    gzip_vary on;
    gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript application/javascript text/x-js image/x-icon image/bmp;

    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;

    index index.html;
    include /etc/nginx/sites-enabled/*;
}

Server block:

server {
    listen  *:81;
    root    /usr/share/nginx/html;
    location / {
        index   index.html;
    }
}
  • Do you have a packet filter enabled (iptables)? If so, did you remember to allow port 81? – Andreas Wiese Apr 17 '14 at 13:13
  • iptables isn't enabled. – Aviator45003 Apr 17 '14 at 13:24
  • 2
    Then relevant parts of your configuration would be helpful, I guess. – Andreas Wiese Apr 17 '14 at 13:28
5

Try the following server block:

server {
   listen       81 default_server;
    server_name _;    
    root    /usr/share/nginx/html;
    location / {
        index   index.html;
    }
}

The underscore _ is a wildcard, Also the *:81 likely doesn't do what you expect, just use the port number.

Then test your settings with nginx -t:

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

Restart nginx:

service nginx restart

Test with netstat:

root@gitlab:~# netstat -napl | grep 80
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      7903/nginx      
tcp        0      0 127.0.0.1:8080          0.0.0.0:*               LISTEN      2662/unicorn.

Update

I installed nginx on a test system. With the stock nginx.conf file and a 1 line change to /etc/nginx/sites-enabled/default, I was able to retrieve files from port 81

cat /etc/nginx/sites-enabled/default
server {

    listen   81;
    server_name localhost;
    root /usr/share/nginx/www;
    index index.html index.htm;


    location / {
        try_files $uri $uri/ /index.html;
    }

    location /doc/ {
        alias /usr/share/doc/;
        autoindex on;
        allow 127.0.0.1;
        deny all;
    }

}

Netstat output:

netstat -napl | grep 81
tcp        0      0 0.0.0.0:81              0.0.0.0:*               LISTEN      3432/nginx

Download file:

$ wget localhost:81

Contents of file:

$ cat index.html
<html>
<head>
<title>Welcome to nginx!</title>
</head>
<body bgcolor="white" text="black">
<center><h1>Welcome to nginx!</h1></center>
</body>
</html>

Update2

Test port:

 root@gitlab:# nc -vz localhost 81
 Connection to localhost 81 port [tcp/*] succeeded!
 root@gitlab:# nc -vz localhost 443
 nc: connect to localhost port 443 (tcp) failed: Connection refused
  • Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 9 0 0.0.0.0:81 0.0.0.0:* LISTEN 1026/nginx: master Still no success. Is Recv-Q being 9 a hint of some sort? It goes up every time I try a wget or something like that. The server block is exactly as you prescribed. – Aviator45003 Apr 23 '14 at 20:53
  • I'm not sure what Recv-Q is. Do you have anything in /etc/nginx/sites-available ? – spuder Apr 23 '14 at 21:09
  • 1
    @T.C. Please see my updated answer. – spuder Apr 23 '14 at 21:49
  • no change to my status, even following the new config file. Is it possible there is something blocking ports not port 80? Is there a way I can test this? – Aviator45003 Apr 23 '14 at 23:10
  • 1
    T.C. yes, use nc, see update – spuder Apr 23 '14 at 23:18
4

Turns out the big problem? Nginx had set worker_processes to 0. I added a line setting it to auto in the top of my nginx.conf, and all was well with the world!

Thank you all for your time and patience.

  • You just saved me from giving up after about 1 hour of frustration—apparently one of my automated variables used for templating Nginx configuration output a 0 for worker_processes, and I was completely flabbergasted after quadruple-checking every other config file, DNS, hosts, etc. – geerlingguy Mar 8 '16 at 2:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.