Nginx只在php cripts和子域上抛出404错误

时间:2017-11-26 15:14:13

标签: php linux nginx vps

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

/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"

它似乎表现正常:它在尝试访问没有索引的文件夹时返回403禁止,在尝试获取非现有文件时返回404。但是,即使有要求,也会注意到php文件中的404。

最后一行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"

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

我真的不知道该怎么做才能工作。如果有人可以提供帮助,他会非常欢迎。

编辑:这是尝试通过hurl.it获取https://sub.domain.com/时的HTTP响应。 enter image description here

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

1 个答案:

答案 0 :(得分:0)

您缺少root指令。

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

例如:

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

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

有关详情,请参阅this document