2017-11-26 5 views
1

Ich habe ein Problem auf Nginx: alles hat gut funktioniert, bis ich versuchte, eine Subdomain hinzuzufügen. Jetzt, jedes Mal, wenn ich versuche, eine PHP-Datei von meinem Browser zu bekommen, bekomme ich einen Fehler 404. Aber das Seltsamste ist, dass es auf meiner Hauptdomain immer noch funktioniert. Es gibt keine Fehlerprotokolle für nginx, und im Zugriffsprotokoll kann ich die 404-HTTP-Antwort sehen. Hat jemand eine Antwort?Nginx wirft 404 Fehler auf PHP-Skripten und nur auf Subdomain

/etc/nginx/sites-available/domain.com

server { 
    listen 80; 
    server_name domain.com *.domain.com; 

    access_log /var/www/domain.com/logs/nginx_access.log vhosts; 
    error_log /var/www/domain.com/logs/nginx_error.log; 

    location/{ 
     root /var/www/domain/www; 
     index index.php index.html index.htm; 
    } 
    error_page 500 501 502 503 504 /50x.html; 

    location = /50x.html { 
     root /var/www/nginx-default; 
    } 

    location ~ \.php$ { 
     try_files $uri =404; 
     fastcgi_pass unix:/run/php/php7.0-fpm.sock; 
     fastcgi_index index.php; 
     fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; 
     include fastcgi_params; 
     include snippets/fastcgi.conf; 
    } 
} 

server { 
    listen 443 ssl http2; 
    server_name domain.com *.domain.com; 

    access_log /var/www/domain.com/logs/nginx_access.log vhosts; 
    error_log /var/www/domain.com/logs/nginx_error.log; 

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

####### SSL Directives ####### 
    ssl on; 
    ssl_certificate /var/www/domain.com/config/ssl/fullchain.domain.com.pem; 
    ssl_certificate_key /var/www/domain.com/config/ssl/privkey.domain.com.pem; 
    ssl_session_timeout 5m; 
    ssl_protocols TLSv1.2 TLSv1.1 TLSv1; 
    ssl_ciphers ECDHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-SHA384:ECDHE-RSA-AES256-SHA:DHE-RSA-AES256-SHA; 
    ssl_prefer_server_ciphers on; 

    location/{ 
     root /var/www/domain.com/www; 
     index index.php index.html index.htm; 
    } 

    location ~ \.php$ { 
     try_files $uri =404; 
     fastcgi_pass unix:/run/php/php7.0-fpm.sock; 
     fastcgi_index index.php; 
     fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; 
     include fastcgi_params; 
     include snippets/fastcgi.conf; 
     } 


    ### 
    # Rewrite rules 
    ### 

    ### 
    # Rewrite rule 1 : redirect www.domain.com to domain.com 
    ### 

    if ($host ~* ^www\.(.*)domain\.com$) { 
     rewrite ^(.*) //domain.com/$1 permanent; 
    } 
} 

server { 
    listen 443 ssl; 
    server_name sub.domain.com; 

    location/{ 
     root /var/www/domain.com/sub/www; 
     index index.php index.html index.htm; 
    } 

    ### 
    # SSL 
    ### 

    ssl on; 
     ssl_certificate /var/www/domain.com/config/ssl/fullchain.sub.domain.com.pem; 
     ssl_certificate_key /var/www/domain.com/config/ssl/privkey.sub.domain.com.pem; 
     ssl_session_timeout 5m; 
     ssl_protocols TLSv1.2 TLSv1.1 TLSv1; 
     ssl_ciphers ECDHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-SHA384:ECDHE-RSA-AES25$; 
     ssl_prefer_server_ciphers on; 

    location ~ \.php$ { 
       try_files $uri =404; 
     fastcgi_pass unix:/run/php/php7.0-fpm.sock; 
     fastcgi_index index.php; 
     fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; 
     include fastcgi_params; 
       include snippets/fastcgi.conf; 
     } 


    ### 
    # Logging 
    ### 

    access_log /var/www/domain.com/logs/nginx_access.log vhosts; 
    error_log /var/www/domain.com/logs/nginx_error.log; 

} 

server { 
    listen 80; 
    server_name sub.domain.com; 

    location/{ 
     root /var/www/domain.com/sub/www; 
     index index.php index.html index.htm; 
    } 

    location ~ \.php$ { 
     try_files $uri =404; 
     fastcgi_pass unix:/run/php/php7.0-fpm.sock; 
     fastcgi_index index.php; 
     fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; 
     include fastcgi_params; 
       include snippets/fastcgi.conf; 
     } 


    ### 
    # Logging 
    ### 

    access_log /var/www/domain.com/logs/nginx_access.log vhosts; 
    error_log /var/www/domain.com/logs/nginx_error.log; 
} 

Die zwei letzten Zeilen des Fehlerprotokoll

2017/11/26 15:44:37 [error] 25444#25444: *10 directory index of "/var/www/domain.com/sub/www/files/" is forbidden, client: 00.00.000.000, server: sub.domain.com, request: "GET /files/ HTTP/2.0", host: "sub.domain.com" 
2017/11/26 15:46:10 [error] 25444#25444: *14 open() "/var/www/domain.com/sub/www/this_file_does_not_exist.p" failed (2: No such file or directory), client: 00.00.000.000, server: sub.domain.com, request: "GET /this_file_does_not_exist.p HTTP/2.0", host: "sub.domain.com" 

Es normal zu verhalten scheint: es gibt 403 verboten beim Zugriff auf einen Ordner ohne Index und 404 beim Versuch, eine nicht vorhandene Datei zu erhalten. Aber, hier ist über 404 auf PHP-Dateien, selbst wenn angefordert.

letzte Zeilen access.log

domain.com 00.00.000.000 - - [26/Nov/2017:15:45:32 +0100] "GET /index.php HTTP/2.0" 200 176 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36" 
sub.domain.com 00.00.000.000 - - [26/Nov/2017:15:45:42 +0100] "GET /index.php HTTP/2.0" 404 261 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36" 
sub.domain.com 00.00.000.000 - - [26/Nov/2017:15:46:10 +0100] "GET /this_file_does_not_exist.p HTTP/2.0" 404 260 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36" 
sub.domain.com 00.00.000.000 - - [26/Nov/2017:15:46:24 +0100] "GET /main_style.css HTTP/2.0" 200 2931 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36" 

Auf Haupt-Domain, PHP-Dateien haben eine 200-Antwort und werden im Browser angezeigt. Aber in der Subdomäne geben sie eine 404-Antwort zurück, als ob sie nicht hier wären. Aber index.php befindet sich im selben Ordner wie main_style.css, und main_style.css gibt eine Antwort von 200 zurück und wird angezeigt.

Ich weiß wirklich nicht, was ich tun soll, damit das funktioniert. Wenn jemand helfen könnte, wäre er sehr willkommen.

EDIT: Hier ist ein die HTTP-Antwort beim Versuch, https://sub.domain.com/ abzurufen, über hurl.it. enter image description here

PS: sub.domain.com ist nicht mein Domain Name. Ich habe es gerade geändert, um zu vermeiden, dass dieser Beitrag über meiner Website in Google steht.

Antwort

0

Ihnen fehlt eine root Direktive.

Wenn sich location / und location ~ \.php$ den gleichen Stamm teilen, ist es üblich, den Stamm im server Block anzugeben und zuzulassen, dass beide Standorte denselben Wert erben.

Zum Beispiel:

server { 
    ... 
    root /var/www/domain.com/www; 

    location/{ 
     ... 
    } 
    location ~ \.php$ { 
     ... 
    } 
} 

Siehe this document für mehr.