代码之家  ›  专栏  ›  技术社区  ›  robjmills

PHP错误出现在PHP和Nginx容器中

  •  0
  • robjmills  · 技术社区  · 3 年前

    最近,我一直在尝试从PHP应用程序中本地基于文件的日志记录转移到通过stdout推送PHP错误,以便在docker设置中与其他日志一起输出,并遵循既定原则 here 。如果您设置 error_log 位置到 /dev/stdout 然后我通过跟踪docker日志看到了来自PHP的错误。然而,nginx容器中也会出现同样的错误,如下所示,通过“stderr中发送的FastCGI”:

    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();
    
    0 回复  |  直到 3 年前
        1
  •  1
  •   Martin Zeitler    3 年前

    这可能源于为PHP-FPM设置的默认错误处理程序:
    https://www.php.net/manual/en/function.error-log.php

    正确的处理程序是: error_log = /proc/self/fd/2
    这似乎是当前进程的文件描述符2。