最近我一直在尝试从 PHP 应用程序中的本地文件日志记录转移到通过 stdout 推送 PHP 错误,以便它们与 docker 设置中的其他日志一起输出。如果您将位置设置error_log
为,/dev/stdout
那么我可以通过跟踪 docker 日志看到来自 PHP 的错误,这种方法很有效。但是,这些相同的错误也会出现在 nginx 容器中,如下所示,通过“FastCGI 在 stderr 中发送”:
docker-compose-nginx-phpfpm-php-fpm-1 | NOTICE: PHP message: test
docker-compose-nginx-phpfpm-php-fpm-1 | 172.18.0.3 - 18/Jan/2022:20:00:20 +0000 "GET /index.php" 200
docker-compose-nginx-phpfpm-web-1 | 2022/01/18 20:00:20 [error] 32#32: *18 FastCGI sent in stderr: "PHP message: test" while reading response header from upstream, client: 172.18.0.1, server: phpfpm.local, request: "GET / HTTP/1.1", upstream: "fastcgi://172.18.0.2:9000", host: "localhost:8080"
为了清楚起见:
PHP 容器日志
docker-compose-nginx-phpfpm-php-fpm-1 | NOTICE: PHP message: test
Nginx 容器日志
docker-compose-nginx-phpfpm-web-1 | 2022/01/18 20:00:20 [error] 32#32: *18 FastCGI sent in stderr: "PHP message: test" while reading response header from upstream, client: 172.18.0.1, server: phpfpm.local, request: "GET / HTTP/1.1", upstream: "fastcgi://172.18.0.2:9000", host: "localhost:8080"
这是怎么回事?这是预期的行为吗?
这是一个非常基本的和标准的 php-fpm/nginx 设置的结果,其中 docker-compose.yml 如下所示:
version: "3.9"
services:
web:
image: nginx:latest
ports:
- "8080:80"
volumes:
- ./src:/var/www/html
- ./default.conf:/etc/nginx/conf.d/default.conf
links:
- php-fpm
php-fpm:
image: php:8-fpm
volumes:
- ./src:/var/www/html
default.conf 类似:
server {
index index.php index.html;
server_name phpfpm.local;
error_log /var/log/nginx/error.log;
access_log /var/log/nginx/access.log;
root /var/www/html;
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass php-fpm:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
}
以及类似的 index.php:
<?php
ini_set('display_errors', 'off');
ini_set('error_log', '/dev/stdout');
error_log('test');
echo phpinfo();