【问题标题】:Catch PHP 404 errors with nginx and fastcgi使用 nginx 和 fastcgi 捕获 PHP 404 错误
【发布时间】:2013-12-24 13:19:15
【问题描述】:

我已经为客户端设置了一个基本服务器,并且说客户端需要基于 nginx(我通常将 apache 用于基于 PHP 的服务器)

这是工作配置。

server {
    listen 80;
    server_name localhost;
    auth_basic "My web site";
    auth_basic_user_file "/usr/local/nginx/www_passwd";

    location ~ ^/(?:share|conf) {
        deny all;
    }

    location ~ /\.ht {
        deny all;
    }

    location ~ /\.svn {
        deny all;
    }

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



    location ~ \.php$ {
        root "/var/www";
        fastcgi_pass unix:/etc/phpcgi/php-cgi.socket;
        fastcgi_index index.php;
        fastcgi_intercept_errors on;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}

这对大多数程序都非常有效。 但是我无法捕获 PHP 404 错误,这是模块所必需的。

模块位于名为“extra_app”的目录中

所以我尝试在上面的配置中添加这个:

location ~ ^/extra_app/([a-zA-Z0-9\.])$ {
    error_page 404 = /var/www/extra_app/index.php;
    root /var/www/extra_app/;
    index index.php index.html index.htm;
}

我需要能够从 /extra_app/ 目录中的 php 文件请求中截获 404 错误。 我已将此添加到上述配置的 .php 部分:

fastcgi_intercept_errors on;

这没有任何效果。 (是的,我重启了nginx服务!)

有人知道解决办法吗?

谢谢!

[编辑]

我不确定是否需要 404 错误页面...也许可以为 nginx 编写“mod_rewrite”规则?我是 nginx 的新手,我什至不确定你是否可以重写 url...

【问题讨论】:

标签: php linux nginx http-status-code-404 config


【解决方案1】:

可能会导致您出现问题的一件事是位置块的范围。错误页面指令位于一个位置块中,而发生错误时正在使用的 php 块位于另一个位置块中,因此看不到 error_page 指令。

您可以将 error_page 指令上移一个级别(但随后将捕获所有 404 错误,或者构建 php 块的副本但使用 /extra-app 前缀。

   location ~ ^/extra_app/(.*\.php)$ {
        root "/var/www";
        error_page 404 /extra_app/404.php
        fastcgi_pass unix:/etc/phpcgi/php-cgi.socket;
        fastcgi_index index.php;
        fastcgi_intercept_errors on;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }

因为 nginx 会选择最长的前缀,所以它会优先匹配所有其他应用程序中较短的前缀。

【讨论】:

    猜你喜欢
    • 2016-08-25
    • 2013-08-12
    • 2019-01-17
    • 1970-01-01
    • 1970-01-01
    • 2015-04-08
    • 2011-03-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多