Nginx

nginx vhost錯誤“重複預設伺服器”

  • December 12, 2016

我在已設置的 CentOS 7 機器上設置基於名稱的虛擬主機時遇到問題。當我嘗試啟動時,我收到以下錯誤:

12 月 07 日 20:55:28 li805-37 nginx

$$ 7284 $$: nginx:$$ emerg $$/etc/nginx/conf.d/website-one.conf:2 中 0.0.0.0:80 的重複預設伺服器 這裡是內容/etc/nginx/nginx.conf

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

# Load dynamic modules. See /usr/share/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
   worker_connections 1024;
}

http {
   log_format  main  '$remote_addr - $remote_user [$time_local] "$request" 

'
                     '$status $body_bytes_sent "$http_referer" '
                     '"$http_user_agent" "$http_x_forwarded_for"';

   access_log  /var/log/nginx/access.log  main;

   sendfile            on;
   tcp_nopush          on;
   tcp_nodelay         on;
   keepalive_timeout   65;
   types_hash_max_size 2048;

   include             /etc/nginx/mime.types;
   default_type        application/octet-stream;

   include /etc/nginx/conf.d/*.conf;
}

和 vhost 配置,首先website-one執行節點:

upstream node_server {
  server 127.0.0.1:3000 fail_timeout=0;
}

server {
   listen 80;
   server_name website-one.com;

   index index.html index.htm;
   access_log /var/log/webone/access.log;
   error_log /var/log/webone/error.log debug;

   location / {
       proxy_set_header Host $host;
       proxy_set_header X-Real-IP $remote_addr;
       proxy_redirect off;
       proxy_buffering off;
       proxy_pass http://node_server;
   }

   location /public/ {
       root /opt/website-one;
   }
}

那麼website-two現在是一個靜態的html站點:

server {
   listen 80;
   server_name website-two.com;

   access_log /var/log/webtwo/access.log;
   error_log /var/log/webtwo/error.log debug;
   index index.php index.html index.htm;

   location / {
       root /var/www/;
   }

}

此外,我嘗試在目錄中搜尋“default_server”,/etc/nginx結果只是命中了兩條註釋掉的行,/etc/nginx/nginx.con為簡潔起見,我已將其刪除。

對我來說,這完全是一個大腦放屁。首先,我嘗試像這樣啟動 nginx:

sudo systemctl start nginx.service

當它失敗時,我檢查了一條錯誤消息:

sudo systemctl status -l nginx.service

然後,在我解決了問題之後,我再次執行以查看錯誤是否仍然存在sudo systemctl status -l nginx.service,但沒有嘗試啟動服務,該命令只是給了我最後一個錯誤!因此,儘管已經解決了問題,但我遇到了同樣的錯誤。請記住,在更改 nginx 配置時,您必須在檢查錯誤消息之前實際啟動服務。

引用自:https://serverfault.com/questions/819400