PHP5-FPM如何向nginx发送错误?

PHP5-FPM如何向nginx发送错误?

由于在网上找不到很好的解释,因此我一直在尝试 php-fpm 和 nginx 中的错误日志记录。大多数指南都说,如果我想将错误从 php5-fpm 发送回 nginx,我应该将其更改为catch_workers_outputyes然而,在我的实验中,我发现即使catch_workers_output设置为no,nginx 仍会正确记录错误。

这是我的虚拟主机配置:

server {                                                                                                                                                                                                                                 
        server_name     domain.com;
        return  301 http://www.domain.com$request_uri;
        access_log off;
}

server {
        listen 80;
        listen [::]:80;

        root /home/websites/domain.com;
        index index.php index.html index.htm;


        error_log /home/websites/logs/domain.com/error.log warn;
        access_log /home/websites/logs/domain.com/access.log;

        #switch on gzip
        gzip on;
        gzip_min_length  1100;
        gzip_buffers  10 32k;
        gzip_types    text/plain application/x-javascript text/xml text/css;
        gzip_vary on;


        location / {
                try_files $uri $uri/ /index.php?q=$uri&$args;
        }

        location ~* .(gif|jpg|jpeg|png|css|js|ico)$ {
                expires 30d;
                access_log off;
        }

        error_page 404 /404.html;

        location ~ \.php$ {
                fastcgi_split_path_info ^(.+\.php)(/.+)$;
                # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini

                # With php5-cgi alone:
                # With php5-fpm:
                fastcgi_pass unix:/var/run/php5-fpm.sock;
                fastcgi_index index.php;
                include fastcgi_params;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        }

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        location ~ /\.ht {
                deny all;
        }
}

以下是我的发现:

Exp 1
        poolconf:
                ; catch_workers_output = no (commented out)
                php_admin_value[error_log] = /var/log/fpm-php.www.log
                php_admin_flag[log_errors] = on
                ; php_flag[display_errors] = 0

        result:
                errors not shown in browser
                error written in /var/log/fpm-php.www.log
                error not written in virtualhost error-log in nginx
                errors not displayed in stderr when running php5-fpm non-daemonized

Exp 2
        poolconf:
                catch_workers_output = yes
                php_admin_value[error_log] = /var/log/fpm-php.www.log
                php_admin_flag[log_errors] = on
                php_flag[display_errors] = 0

        results:
                no error in browser
                error written in /var/log/fpm-php.www.log
                error not written to virtualhost error-log by nginx
                errors not displayed in stderr when running php5-fpm non-daemonized

Exp 3
        poolconf:
                catch_workers_output = yes
                ; php_admin_value[error_log] = /var/log/fpm-php.www.log
                ; php_admin_flag[log_errors] = on
                php_flag[display_errors] = 0

        results:
                no errors in browser
                error  NOT written in /var/log/fpm-php.www.log
                error WRITTEN to virtualhost error-log by nginx
                errors DISPLAYED in stderr when running php5-fpm non-daemonized

Exp 4
        poolconf:
                ; catch_workers_output = no (commented out)
                ; php_admin_value[error_log] = /var/log/fpm-php.www.log
                ; php_admin_flag[log_errors] = on
                php_flag[display_errors] = 0

        results:
                no errors in browser
                error NOT written in /var/log/fpm-php.www.log
                error WRITTEN to virtualhost error-log by nginx
                errors NOT displayed in stderr when running php5-fpm non-daemonized

我的问题是,即使 php-fpm 没有 stderr 输出 (当catch_workers_output=no),PHP5-FPM 如何将错误日志发送到 nginx?我在任何地方都找不到相关文档。

答案1

当你的 nginx 配置使用php_admin_valuephp_admin_flagphp_flag它会覆盖 php.ini 中的值。注释掉 nginx 设置将保留 php.ini 文件作为控制。

查看 php.ini 文件中的设置显示错误显示启动错误错误报告html_错误日志错误。那里很可能存在冲突。

相关内容