使用Nginx的自己的错误页面将无法正常工作

时间:2018-02-08 16:43:21

标签: nginx

我想创建自己的错误文件。但我总是看到一个白色的网站“找不到文件”。我已经在PHP位置检查了它,结果相同。

server {
    listen 443 ssl http2;
    listen [::]:443 ssl http2;

    server_name mydomain.com;

    ssl_.........

    root /var/www/html/mydomain.com;
    .....

    error_page 404 /404.php;
    location = /404.php {
        root /var/www/html/mydomain.com/errorpage;
        internal;
    }

    location ^~ /.well-known/acme-challenge/ {
        default_type text/plain;
        root /var/www/letsencrypt;
    }

    location / {
        index index.php;

        try_files $uri $uri/ @rewrite;
    }

    location @rewrite {
        rewrite ^/(site/|contact/)?([^.]+)$ /$1index.php?$2 last;
    }

    location ~ \.php$ {
        include fastcgi_params;
        fastcgi_split_path_info ^(.+\.php)(/.*)$;
        fastcgi_pass unix:/var/run/php/mydomain.com-php7.2-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        proxy_connect_timeout 900;
        proxy_send_timeout 900;
        proxy_read_timeout 900;
        fastcgi_send_timeout 900;
        fastcgi_read_timeout 900;
    }

    .....
}

我找到了很多提示:

fastcgi_intercept_errors on;

等等。但没机会。

1 个答案:

答案 0 :(得分:0)

我找到了一个步骤解决方案。

error_page 404 /404.php;
location ~ \.php$ {
        include fastcgi_params;
        fastcgi_split_path_info ^(.+\.php)(/.*)$;
        fastcgi_pass unix:/var/run/php/mydomain.com-php7.2-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        proxy_connect_timeout 900;
        proxy_send_timeout 900;
        proxy_read_timeout 900;
        fastcgi_send_timeout 900;
        fastcgi_read_timeout 900;
        fastcgi_intercept_errors on;
    }

当404.php在主目录中时,这将起作用。 当我添加这个

location = /404.php {
        root /var/www/html/mydomain.com/errorpage;
        internal;
    }

404.php会找到,但它不会进入php解析器并让我下载404.php

您知道我该如何解决它?