Nginx - Customizing 404 page

后端 未结 5 2239
无人共我
无人共我 2020-12-07 10:29

Nginx+PHP (on fastCGI) works great for me. When I enter a path to a PHP file which doesn\'t exist, instead of getting the default 404 error page (which comes for any invalid

5条回答
  •  天涯浪人
    2020-12-07 10:55

    The "error_page" parameter makes a redirect, converting the request method to "GET", it is not a custom response page.

    The easiest solution is

         server{
             root /var/www/html;
             location ~ \.php {
                if (!-f $document_root/$fastcgi_script_name){
                    return 404;
                }
                fastcgi_pass   127.0.0.1:9000;
                include fastcgi_params.default;
                fastcgi_param  SCRIPT_FILENAME  $document_root/$fastcgi_script_name;
            }
    
    

    By the way, if you want Nginx to process 404 status returned by PHP scripts, you need to add

    [fastcgi_intercept_errors][1] on;

    E.g.

         location ~ \.php {
                #...
                error_page  404   404.html;
                fastcgi_intercept_errors on;
             }
    

提交回复
热议问题