Nginx 监听某个端口,仅在设置为端口 80 时才响应

Avi*_*003 12 configuration nginx funtoo

操作系统:Funtoo。我已将 NGINX 绑定到端口 81(我想将它与我的 Apache 服务器一起运行一小段时间以便于转换),并且它在端口上侦听(如果我指向另一个端口,使用 wget 我得到“连接被拒绝”,但是使用端口 81 我得到“连接”)但它从不提供任何类型的 HTML 响应!

在端口上运行 wget 时,从本地主机,我得到:

# 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...
Run Code Online (Sandbox Code Playgroud)

在另一台电脑上...

$ 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...
Run Code Online (Sandbox Code Playgroud)

在那之后什么也没有发生。文件存在,就是正常的Funtoo nginx.conf。

更新:我可以让它监听端口 80,但它仍然让我感到不安,我无法让它在任何端口上工作......

netstat -aWn | grep 81 | grep LISTEN
tcp 60 0 0.0.0.0:81 0.0.0.0:* LISTEN
Run Code Online (Sandbox Code Playgroud)

编辑:配置文件:

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/*;
}
Run Code Online (Sandbox Code Playgroud)

服务器块:

server {
    listen  *:81;
    root    /usr/share/nginx/html;
    location / {
        index   index.html;
    }
}
Run Code Online (Sandbox Code Playgroud)

spu*_*der 5

尝试以下服务器块:

server {
   listen       81 default_server;
    server_name _;    
    root    /usr/share/nginx/html;
    location / {
        index   index.html;
    }
}
Run Code Online (Sandbox Code Playgroud)

下划线_是通配符,也*:81可能不会按照您的预期进行,只需使用端口号。

然后使用以下命令测试您的设置nginx -t

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
Run Code Online (Sandbox Code Playgroud)

重启nginx:

service nginx restart
Run Code Online (Sandbox Code Playgroud)

用 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.
Run Code Online (Sandbox Code Playgroud)

更新

我在测试系统上安装了 nginx。将库存nginx.conf文件和 1 行更改为/etc/nginx/sites-enabled/default,我能够从端口 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;
    }

}
Run Code Online (Sandbox Code Playgroud)

网络统计输出:

netstat -napl | grep 81
tcp        0      0 0.0.0.0:81              0.0.0.0:*               LISTEN      3432/nginx
Run Code Online (Sandbox Code Playgroud)

下载文件:

$ wget localhost:81
Run Code Online (Sandbox Code Playgroud)

文件内容:

$ 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>
Run Code Online (Sandbox Code Playgroud)

更新2

测试端口:

 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
Run Code Online (Sandbox Code Playgroud)


Avi*_*003 4

原来是个大问题?Nginx 已将worker_processes 设置为0。我在nginx.conf 的顶部添加了一行将其设置为auto,一切都很好!

感谢大家的时间和耐心。