0

PHP-FPM Always returns 200 response code regardless PHP errors.

php container:

display_errors = Off
log_errors = On

httpd container:

<IfModule proxy_module>
<FilesMatch \.php$>
    SetHandler  "proxy:fcgi://php:9000"
</FilesMatch>
</IfModule>

index.php

// infinite loop
while (true) {}
// or parser error
wh ile (true) {}

Produces page

Parse error: syntax error, unexpected 'ile' (T_STRING) in /var/www/html/index.php on line 10

With response status 200

Logs

php | 172.22.0.1 - 21/Jan/2019:12:59:03 +0000 "GET /index.php" "http://example.local/" status:200 /var/www/html/web/index.php took:286.962ms mem:18432Kb cpu:83.63%
php | [21-Jan-2019 12:59:03] WARNING: [pool www] child 122 said into stderr: "NOTICE: PHP message: PHP Parse error:  syntax error, unexpected 'ile' (T_STRING) in /var/www/html/index.php on line 10"
httpd | [Mon Jan 21 12:59:04.001447 2019] [proxy_fcgi:error] [pid 8:tid 140486365661928] [client 172.22.0.1:59478] AH01071: Got error 'PHP message: PHP Parse error:  syntax error, unexpected 'ile' (T_STRING) in /var/www/html/index.php on line 10\n'
httpd | example.local 172.22.0.1 - - [21/Jan/2019:12:59:03 +0000] "GET / HTTP/1.1" 200 115 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36"

UPD 1

I've reproduced this behaviour with any php:*-fpm-* image from Docker hub without any modification in php config.

UPD 2

Test repo to reproduce the behaviour https://github.com/mahnunchik/php-fpm-response-code

mahnunchik
  • 995
  • 5
  • 26
  • please, add some more details on what the "php container" is... there are relevant settings in php.ini (that you have posted) but also in fpm config file that may be causing this. – Oerd Jan 21 '19 at 12:47
  • A PHP error doesn't necessarily mean a non 200 response, how is the HTTP server supposed to know that anything has gone wrong?. The best it can do is serve a 500 response if the PHP process is not responding. You need to call `http_response_code` if you wish to set a non 200 return code from the PHP process. If you wish to return a non 200 error for all errors you need to use `set_error_handler` and `set_exception_handler` where you can call `http_response_code` to indicate a failure to the HTTP process. – Geoffrey Jan 22 '19 at 02:30

1 Answers1

0

You can easily fix this by adding the following line at the start of your script:

http_response_code(500);
olvlvl
  • 2,396
  • 1
  • 22
  • 22