PHP-FPM始终返回200个响应代码,而不管PHP错误如何

时间:2019-01-21 12:44:39

标签: php apache docker alpine fpm

PHP-FPM始终返回200个响应代码,而不管PHP错误。

php容器:

display_errors = Off
log_errors = On

httpd容器:

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

index.php

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

产生页面

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

响应状态为200

日志

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

我已经用Docker Hub中的任何php:*-fpm-*映像重现了此行为,而未在php配置中进行任何修改。

UPD 2

测试仓库以重现行为https://github.com/mahnunchik/php-fpm-response-code

1 个答案:

答案 0 :(得分:0)

您可以通过在脚本的开头添加以下行来轻松解决此问题:

http_response_code(500);