Configuration

Nginx 監聽一個埠,只有設置為 80 埠才會響應

  • March 10, 2021

作業系統: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;
   }
}

原來是大問題?Nginx 已將 worker_processes 設置為 0。我auto在 nginx.conf 的頂部添加了一行將其設置為,一切都很好!

感謝大家的時間和耐心。

嘗試以下伺服器塊:

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

引用自:https://unix.stackexchange.com/questions/125227