【问题标题】:Can't serve php custom error files from a parent folder in nginx无法从 nginx 的父文件夹中提供 php 自定义错误文件
【发布时间】:2022-01-15 10:08:53
【问题描述】:

我有以下目录方案

/srv/www/htdocs/www/

其中 www 是公共目录根目录,而 htdocs 包含公共访问之外的文件。这是 nginx.conf

server {
    listen       443 ssl http2;

    root   /srv/www/htdocs/www;

    location / {
        index index.php;
    }

    location ~ \.php$ {
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_intercept_errors on;
        include fastcgi_params;
    }

    error_page 401 @errorPage;
    error_page 403 @errorPage;
    error_page 404 @errorPage;
    location @errorPage {
        root /srv/www/htdocs;
        rewrite ^ error.php?error=$status last;
    }

}

我要做的是在 401,403 和 404 触发时提供 error.php 页面,但我仍然得到默认的 Nginx 404 页面,即使我以编程方式触发 403 或 401。

我在指定位置做错了吗?

【问题讨论】:

    标签: php nginx nginx-location


    【解决方案1】:

    root 放在@errorPage 中毫无意义。该位置是通往error.php 的垫脚石,它最终回到location ~ \.php$,根值为/srv/www/htdocs/www

    最简单的解决方案是将error.php 移动到www 目录中。


    如果要将error.php 保留在www 目录之外,则需要在location @errorPage 块中复制大部分location ~ \.php$ 块。

    例如:

    location @errorPage {
        fastcgi_pass  127.0.0.1:9000;
        include       fastcgi_params;
        fastcgi_param SCRIPT_FILENAME /srv/www/htdocs/error.php;
        fastcgi_param QUERY_STRING error=$status;
    }
    

    fastcgi_param 语句置于include 之后,否则后者将覆盖新值。

    【讨论】:

    • 谢谢您,您的解决方案有效!!!顺便说一句,我想将错误页面排除在公共访问之外,这就是我移动文件是父文件夹的原因。
    猜你喜欢
    • 1970-01-01
    • 2020-06-10
    • 2022-01-03
    • 1970-01-01
    • 2020-10-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多