Nginx 侦听端口,仅在设置为端口 80 时才响应

Nginx 侦听端口,仅在设置为端口 80 时才响应

操作系统: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...

在另一台电脑上...

$ 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...

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

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

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

编辑:配置文件:

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 {
    listen  *:81;
    root    /usr/share/nginx/html;
    location / {
        index   index.html;
    }
}

答案1

尝试以下服务器块:

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

下划线_是通配符,而且*:81可能不会执行您所期望的操作,只需使用端口号即可。

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

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

重启nginx:

service nginx restart

使用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.

更新

我在测试系统上安装了 nginx。通过 stocknginx.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;
    }

}

网络统计输出:

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

下载文件:

$ wget localhost:81

文件内容:

$ 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>

更新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

答案2

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

感谢大家的时间和耐心。

答案3

我通过在配置文件/etc/nginx/nginx.conf中添加一个websocket解决了这个问题,你可以在中看到反向代理的整个配置文件https://help.teradici.com/s/article/1050

神奇的部分是添加:

upstream websocket {
    server <DESTINATION_IP>:<DEST_PORT>;
}

相关内容