Nginx 兩個帶有子網域的虛擬主機總是指向default_server

Nginx 兩個帶有子網域的虛擬主機總是指向default_server

我有以下設定:

  • 具有兩個虛擬主機的 Nginx
  • 兩個子網域:sub1.domain.com 和 sub2.domain.com

sub1.domain.com工作正常。如果我輸入,瀏覽器會傳回403 sub2.domain.comwww-datachown -R www-data:www-data /var/www/

查看錯誤日誌發現它/var/log/nginx/sub2.domain.com/error.log是空的而/var/log/nginx/sub1.domain.com/error.log包含

2018/03/09 11:29:00 [error] 19024#0: *13009 directory index of "/var/www/sub1.domain.com" is forbidden, client: ip, server: sub1.domain.com, request: "GET / HTTP/1.1", host: "my-host"

sub1.domain.com有誰知道為什麼即使我sub2.domain.com在瀏覽器中輸入,伺服器也會包含?

我認為問題可能是,似乎總是執行(即)sub2.domain.com的配置。根據 nginx 文件:sub1.domain.comdefault_server

如果其值與任何伺服器名稱都不匹配,或者請求根本不包含此標頭字段,則 nginx 會將請求路由到此連接埠的預設伺服器。

sub1.domain.com 的 Nginx 配置

server {
        listen 80 default_server;
        listen [::]:80 default_server ipv6only=on;

        root /var/www/sub1.domain.com;
        index index.php index.html index.htm;

        server_name sub1.domain.com;

        access_log      /var/log/nginx/sub1.domain.com/access.log;
        error_log       /var/log/nginx/sub1.domain.com/error.log;

        location / {
                try_files $uri $uri/ =404;
        }

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        location ~ /\.ht {
                deny all;
        }

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        location ~ \.php$ {
                try_files $uri =404;
                fastcgi_split_path_info ^(.+\.php)(/.+)$;
                fastcgi_pass unix:/var/run/php5-fpm.sock;
                fastcgi_index index.php;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                fastcgi_param AUTH_SECRET "my-secret";
                include fastcgi_params;
        }

}

sub2.domain.com 的 Nginx 配置

upstream backend{
        server 127.0.0.1:3000;
}

server {
        listen 80;
        listen [::]:80;

        access_log      /var/log/nginx/sub2.domain.com/access.log;
        error_log       /var/log/nginx/sub2.domain.com/error.log;

        root    /var/www/sub2.domain.com;

        server_name sub2.domain.com;

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        location ~ /\.ht {
                deny all;
        }

        # route /api requests to backend
        location /api/v1 {
                proxy_pass              http://backend;
                proxy_http_version      1.1;
                proxy_redirect          off;
                proxy_set_header Upgrade        $http_upgrade;
                proxy_set_header Connection     "upgrade";
                proxy_set_header Host           $http_host;
                proxy_set_header X-Scheme       $scheme;
                proxy_set_header X-Real-IP      $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header X-NginX-Proxy  true;
        }

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

我已經按照這裡的教學進行操作https://www.digitalocean.com/community/tutorials/how-to-set-up-nginx-server-blocks-virtual-hosts-on-ubuntu-14-04-lts並設定符號連結。因此/etc/nginx/sites-available包含配置並/etc/nginx/sites-enabled包含這些配置上的符號連結。

另請注意,它sub1.domain.com服務於 php 後端,而sub2.domain.com應該提供一些靜態內容(單頁應用程式)並將 api 請求定向到 Node.js 後端。

編輯:

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

我的nginx.conf文件包含include /etc/nginx/sites-enabled/*;.因此,應該包括我上面發布的配置。nginx -t如果其中一個配置有錯誤,至少會拋出錯誤。

編輯 2:tl;dr(評論摘要):DNS 配置錯誤。為兩個子域設定正確的A記錄解決了問題。 nginx 設定按預期工作。

相關內容