Due to development needs, I want to display PHP errors loud and clear to the browser. So I set my php.ini
:
display_errors = on
This works as expected.
But this messes up the Nginx status code if there is an error. As commented here https://www.php.net/manual/en/errorfunc.configuration.php#126734 :
It's important to note that when display_errors is "on", PHP will send a HTTP 200 OK status code even when there is an error.
I already tried to set fastcgi_intercept_errors on
(or off
) as reccomended somewhere else, but it doesn't seem to make any difference.
So, the question is: how can I have both display_errors = on
and HTTP 500 on errors?
My relevant Nginx config is:
location ~ \.php$ {
try_files $uri =404;
include fastcgi_params;
fastcgi_intercept_errors on;
fastcgi_pass unix:/run/php/php-fpm.sock;
fastcgi_param SCRIPT_FILENAME $request_filename;
}
Tests
I created a PHP file with a missing semicolon for testing. The PHP error is
Parse error: syntax error, unexpected end of file in test.php on line 2
PHP error with display_errors = on
Nginx returns 200 eve if there is an error:
192.168.0.1 - - [27/Sep/2022:08:50:49 0200] "GET /test.php HTTP/2.0" 200 304 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/106.0.0.0 Safari/537.36"
PHP error with display_errors = off
Nginx returns 500:
192.168.0.1 - - [27/Sep/2022:08:53:52 0200] "GET /test.php HTTP/2.0" 500 0 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/106.0.0.0 Safari/537.36"
CodePudding user response:
Have you tried to put:
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
on top of you`re php page?
CodePudding user response:
You should set the default status to 500
.
At the beginning of the code put http_response_code(500);
At the end of the code put http_response_code(200);
May be like this
http_response_code( 500);
ini_set('display_errors', 'on');
http_response_code( 200);