【问题标题】:Root path for 404 PHP page under nginx (php-fpm) doesn't worknginx(php-fpm)下404 PHP页面的根路径不起作用
【发布时间】:2016-02-07 22:34:03
【问题描述】:

如果我尝试访问站点根目录中不存在的任何内容,即http://example.com/unexisting.php,我会使用以下配置从我的自定义 404 页面获得正确好的答案:

server_name example.com;
root        /path/to/example;
index       index.php;
error_page 404  /not_found.php;

location ~ index\.php
{
    try_files $uri/index.php =404;
    return 301 $scheme://$server_name/;
}

location ~ not_found\.php
{
    root        /path/to/example;
    allow all;
    internal;
    fastcgi_intercept_errors off;
    fastcgi_param SCRIPT_FILENAME $document_root/not_found.php;
    include fastcgi_params;
}

location = /
{
    try_files $uri/index.php @static;
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_param SCRIPT_FILENAME $document_root/index.php;
    include fastcgi_params;
}

location ~ \.php$
{
    try_files $uri $uri/ =404;
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_intercept_errors on;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include fastcgi_params;
}

location @static
{
    try_files $uri $uri/ =404;
    expires max;
}

location /
{
    try_files /something_non_existing @static;
}

如果我尝试访问根文件夹外的文件,我。 e. “http://example.com/unexisting_dir/unexisting.php”,404页面在屏幕上,但没有任何图形。因为日志中有类似“GET /unexisting_dir/images/some_image_for_404_page.png HTTP/1.1”的东西,而 nginx 什么也不返回,因为它的用户请求的基础错误。同时,我检查了 $document_root 有正确的基础,所以 php-fpm 工作完美。

问题是如何让 nginx 在为不存在的嵌套(嵌入式)目录服务 404 页面时忽略用户的 URI(不重定向和损害任何潜在的东西)?

非常感谢!

这是“fastcgi_params”(以防万一):

    fastcgi_param  QUERY_STRING       $query_string;
    fastcgi_param  REQUEST_METHOD     $request_method;
    fastcgi_param  CONTENT_TYPE       $content_type;
    fastcgi_param  CONTENT_LENGTH     $content_length;
    fastcgi_param  SCRIPT_NAME        $fastcgi_script_name;
    fastcgi_param  REQUEST_URI        $request_uri;
    fastcgi_param  DOCUMENT_URI       $document_uri;
    fastcgi_param  DOCUMENT_ROOT      $document_root;
    fastcgi_param  SERVER_PROTOCOL    $server_protocol;
    fastcgi_param  HTTPS              $https if_not_empty;
    fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;
    fastcgi_param  SERVER_SOFTWARE    nginx/$nginx_version;
    fastcgi_param  REMOTE_ADDR        $remote_addr;
    fastcgi_param  REMOTE_PORT        $remote_port;
    fastcgi_param  SERVER_ADDR        $server_addr;
    fastcgi_param  SERVER_PORT        $server_port;
    fastcgi_param  SERVER_NAME        $server_name;
    fastcgi_param  REDIRECT_STATUS    200;
    fastcgi_pass unix:/var/run/php5-fpm.sock;

【问题讨论】:

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


    【解决方案1】:

    这里的问题不是 nginx。它正确返回 not_found.php 的输出

    您需要确保输出包含图像的绝对路径,而不是 HTML 中的相对路径。例如

    <img src="/notfound.jpg" />
    

    代替

    <img src="notfound.jpg" />
    

    额外的正斜杠告诉浏览器从文档根目录而不是浏览器请求的当前“目录”请求图像。

    【讨论】:

    • 正确答案是:error_page 404 = $scheme://$server_name/not_found.php; 而不是error_page 404 /not_found.php;
    • @EugeneZakharenko,绝对不是;这将导致 http 重定向,并且对于正确的 UX 来说是一种非常糟糕的做法。
    猜你喜欢
    • 2020-08-08
    • 2020-03-10
    • 1970-01-01
    • 1970-01-01
    • 2017-02-03
    • 2021-04-27
    • 2014-04-24
    • 1970-01-01
    • 2020-11-23
    相关资源
    最近更新 更多