将nginx错误重定向到php


Redirect nginx errors to php

我的目标是将nginx中的任何错误(如404、405等)重定向到位于index.php的php脚本。`

server {
    root /usr/share/nginx/TradeLog/www/;
    index index.php index.html index.htm default.html default.htm;
    # Make site accessible from http://localhost/
    server_name localhost;
    server_name_in_redirect off;
    fastcgi_intercept_errors on;
    error_page 403 404 /index.php/;

    location / {
        try_files $uri $uri/ /index.php?$args;
    }
    location ~* '.php$
    {
            fastcgi_split_path_info ^(.+'.php)(/.+)$;
            fastcgi_pass unix:/var/run/php5-fpm.sock;
            include fastcgi_params;
            fastcgi_read_timeout 600; # Set fairly high for debugging
            fastcgi_index index.php;
    }
    location ~* '.(jpg|jpeg|gif|css|png|js|ico|html)$ {
        expires max;
    }
}

`

然而,当我尝试这样做时,我会遇到一个nginx404错误。我在这里做错了什么?

编辑

我发现,如果我把fastcgi_intercept_errors on变成off,那么nginx会处理错误。我的下一步是为php脚本设置一个新的请求,该请求将显示错误。这是在位置/errors/code/<code_number>

假设我有以下位置块:

location = /composer.phar { return 403; }

然后我想重定向到/error/code/403 的错误页面

因此,我在想

error_page 403 = /error/code/403;

这对我来说很有用,但我的php脚本仍然在接收原始请求——我总是显示一个404页面,因为我的php脚本正在接收一个它不知道的位置的请求。我想让nginx告诉它在外部显示哪个页面,然后把错误号传给scrip。

我认为您应该替换:

error_page 403 404 /index.php/;

带有:

error_page 403 404 /index.php;

额外的/使重定向到目录,而不是文件。您的其余配置似乎还可以。

这是有效的error_page 404=/index.php;