Nginx,PHP + FPM自定义错误页面 [英] Nginx, PHP + FPM Custom Error Pages

查看:192
本文介绍了Nginx,PHP + FPM自定义错误页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一些自定义错误页面,但似乎无法使500个页面正常工作.

I am trying to create some custom error pages but can't seem to get the 500 one working.

我有以下配置:

server {
    listen 80;

    root /var/www/devsite;
    index index.php;
    server_name devsite;

    error_page 403 = /error.php?code=403;   
    error_page 404 = /error.php?code=404;
    error_page 500 = /error.php?code=500;

    location / {
        try_files $uri =404;
    }

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        include fastcgi_params;
    }

}

起初,我认为可能是因为它是一个PHP文件,所以我进行了更改:

At first, I thought it might be because it's a PHP file so I changed:

error_page 500 = /error.php?code=500;

到静态页面:

error_page 500 /500.html

但是当我破坏一些PHP代码来触发它时,它仍然只显示一个空白页面,带有500个响应代码.

But it still just shows a blank page with a 500 response code when I break some PHP code to trigger it.

然后我尝试使其成为location ~ \.php$中的最后一条规则,但同样发生.有什么想法为什么自定义500页无法正常工作?

I then tried to make it the last rule inside location ~ \.php$ but the same happens. Any ideas why the custom 500 page won't work?

我还注意到,如果您尝试访问扩展名为.php的拒绝访问"文件,它将不会显示自定义403页面,而是显示内置页面.有没有办法使规则也覆盖.php文件?

I also notice that if you try to access an "access denied" file that has the .php extension, it will not show the custom 403 page and show the built-in page instead. Is there a way to make the rule cover .php files too?

推荐答案

您缺少的部分是 fastcgi_intercept_errors 指令.没有此指令,Nginx将不会接触来自CGI后端的响应,只要它们是有效的:

The piece you're missing is the fastcgi_intercept_errors directive. Without this directive, Nginx won't touch responses from CGI backends, so long as they are valid:

确定应将FastCGI服务器响应的代码大于或等于300传递给客户端,还是重定向到nginx以使用error_page指令进行处理.

Determines whether FastCGI server responses with codes greater than or equal to 300 should be passed to a client or be redirected to nginx for processing with the error_page directive.

您需要在PHP处理位置中放置以下内容:

You need to put the following in your PHP handling location:

fastcgi_intercept_errors on;

顺便说一句,您可能不需要 error_page 行中的=(取决于您的预期用途).此语法指示Nginx使用从您指向的PHP脚本返回的响应代码,而不是原始的响应代码:

As an aside, you may not need the = in your error_page lines (depending on your intended use). This syntax instructs Nginx to use the response code returned from the PHP script you're pointing to instead of the original response code:

如果错误响应是由代理服务器或FastCGI服务器处理的,则服务器可能返回不同的响应代码(例如200、302、401或404)...以返回的代码进行响应.

If an error response is processed by a proxied server or a FastCGI server, and the server may return different response codes (e.g., 200, 302, 401 or 404) ... respond with the code it returns.

这篇关于Nginx,PHP + FPM自定义错误页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆