Nginx zwei virtuelle Hosts mit Subdomains leiten immer zum Standardserver weiter

Nginx zwei virtuelle Hosts mit Subdomains leiten immer zum Standardserver weiter

Ich habe folgendes Setup:

  • Nginx mit zwei virtuellen Hosts
  • zwei Subdomains: sub1.domain.com und sub2.domain.com

sub1.domain.comfunktioniert einwandfrei. Wenn ich eingebe, gibt der Browser eine 403 zurück. Ich habe die für beide Domänen konfigurierten Verzeichnisse so eingestellt, dass sie ( ) sub2.domain.comgehören .www-datachown -R www-data:www-data /var/www/

Ein Blick auf die Fehlerprotokolle zeigt, dass /var/log/nginx/sub2.domain.com/error.logleer ist, während /var/log/nginx/sub1.domain.com/error.logenthält

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"

Weiß jemand warum der Server sich einloggt auch wenn ich es im Browser sub1.domain.comeingegeben habe ?!sub2.domain.com

Ich denke, das Problem könnte sein, dass sub2.domain.comimmer die Konfiguration von ausgeführt wird sub1.domain.com(was die ist default_server). Laut den Nginx-Dokumenten:

Wenn der Wert mit keinem Servernamen übereinstimmt oder die Anforderung dieses Header-Feld überhaupt nicht enthält, leitet nginx die Anforderung an den Standardserver für diesen Port weiter.

Nginx-Konfiguration für sub1.domain.com

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;
        }

}

Nginx-Konfiguration für sub2.domain.com

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;
        }
}

Ich habe das Tutorial hier befolgthttps://www.digitalocean.com/community/tutorials/how-to-set-up-nginx-server-blocks-virtual-hosts-on-ubuntu-14-04-ltsund richten Sie die Sym-Links ein. /etc/nginx/sites-availableEnthält also die Konfiguration und /etc/nginx/sites-enableddie Sym-Links dieser Konfigurationen.

Beachten Sie auch, dass sub1.domain.comein PHP-Backend bereitgestellt wird, während sub2.domain.comstatische Inhalte (Einzelseitenanwendung) bereitgestellt und die API-Anfragen an ein Node.js-Backend weitergeleitet werden sollten.

Bearbeiten:

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

Meine nginx.confDatei enthält include /etc/nginx/sites-enabled/*;. Die Konfigurationen, die ich oben gepostet habe, sollten also enthalten sein. Zumindest nginx -twird ein Fehler ausgegeben, wenn eine der Konfigurationen einen Fehler enthält.

Edit 2: tl;dr (Zusammenfassung der Kommentare): Der DNS war falsch konfiguriert. Das Einstellen des richtigen AEintrags für beide Subdomains löste das Problem. Die Nginx-Konfiguration funktioniert wie erwartet.

verwandte Informationen