自定义404页面未显示

时间:2015-09-17 19:36:23

标签: nginx

我刚刚开始使用Nginx,并完成了有关配置PHP FPM的基本配置,并且我已将索引文件更改为index.php而不是index.html。我还设法获得了URL重写的句柄,尽管为了简单起见,我在下面的例子中省略了它。

但是在默认配置文件中,有一个专门用于错误页面的部分,看起来好像已经准备好“即插即用”了。我已取消注释它们,但它们无法正常工作。我在Stackoverflow上经历了一些与404相关的问题,每个人似乎都建议将其作为正确的语法。

可能指令的顺序是错误的,但是, 来自原始示例配置,所以我不知道它为什么不起作用。

我已从配置文件中删除所有注释行,剩下的就是:

server {
    listen 80 default_server;
    listen [::]:80 default_server ipv6only=on;

    root /var/www/html;
    index index.php;

    server_name localhost;

    location / {
        try_files $uri $uri/ index.php;
    }

    error_page 404 /404.html;

    error_page 500 502 503 504 /50x.html;

    location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        include fastcgi_params;
    }
}

现在,当我转到http://localhost/this-page-doesnt-exist时,浏览器会显示文字File not found.,而不是/404.html文件的内容。

此外,我的404.html文件的权限与index.php的权限相同,即{644}。所有者也是相同的。

可能有什么不对?

1 个答案:

答案 0 :(得分:0)

try_files将您对不存在的文件的请求传递给index.php。这又将请求发送到php-fpm。默认情况下,Nginx将php-fpm的响应返回给您正在看到的客户端。

更新你的php-fpm配置看起来像这样,并通知Nginx处理响应中的错误代码。

location ~ \.php$ {
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_pass unix:/var/run/php5-fpm.sock;
    fastcgi_index index.php;
    fastcgi_intercept_errors on;
    include fastcgi_params;
}

文档在这里:fastcgi_intercept_errors

相关问题