Nginx仅在php脚本和子域上抛出404错误

Nginx仅在php脚本和子域上抛出404错误,php,linux,nginx,vps,Php,Linux,Nginx,Vps,我在nginx上遇到了一个问题:在我尝试添加子域之前,一切都很正常。现在,每次我试图从浏览器中获取php文件时,都会出现404错误。但最奇怪的是,在我的主要领域,它仍然有效。nginx上没有错误日志,在访问日志上我可以看到404http响应。有人有答案吗 /etc/nginx/sites available/domain.com server { listen 80; server_name domain.com *.domain.com; access_log /va

我在nginx上遇到了一个问题:在我尝试添加子域之前,一切都很正常。现在,每次我试图从浏览器中获取php文件时,都会出现404错误。但最奇怪的是,在我的主要领域,它仍然有效。nginx上没有错误日志,在访问日志上我可以看到404http响应。有人有答案吗

/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;
}
错误日志的最后两行

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"
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"
它的行为似乎正常:当尝试访问没有索引的文件夹时,返回403禁止,当尝试获取不存在的文件时,返回404禁止。但是,这里要注意的是关于php文件的404,即使是被请求的

访问日志的最后几行

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"
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"
在主域上,php文件有200个响应,并显示在浏览器中。但是,在子域中,它们返回404响应,就像它们不在这里一样。但是,index.php与main_style.css位于同一文件夹中,main_style.css返回200响应并显示

我真的不知道该怎么做才能让它起作用。如果有人能帮忙,他将非常受欢迎

编辑:下面是试图通过hull.it获取HTTP响应的示例


PS:sub.domain.com不是我的域名。我只是更改了它,以避免这篇文章出现在我在谷歌的网站上方。

您缺少
root
指令

如果
location/
location~\.php$
共享同一根,通常在
server
块中指定根,并允许两个位置继承相同的值

例如:

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

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

有关详细信息,请参阅。

缺少
根指令

如果
location/
location~\.php$
共享同一根,通常在
server
块中指定根,并允许两个位置继承相同的值

例如:

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

    location / {
        ...
    }
    location ~ \.php$ {
        ...
    }
}
更多信息,请参阅