下面是一个示例脚本:

#!/usr/bin/perl
use FCGI;
my $request = FCGI::Request();
while($request->Accept() >= 0) {
    die "test";
}

我希望这会将“测试”打印到 Apache 错误日志 as per FCGI spec ,但什么也没有发生。如果我将骰子移到 while 循环的外部和前面,则消息将打印到错误日志中。

有关 Apache 配置的更多信息,此行用于配置处理程序:
Addhandler fcgid-script .fcgi

有人告诉我 suexec 正在使用中并充当 fcgi 包装器。

编辑:

FCGI.pm itself 提出了部分解决方案:



因此,我以这种方式尝试过:
#!/usr/bin/perl

use FCGI;
use IO::Handle;

my ( $stdin, $stdout, $stderr ) = ( IO::Handle->new, IO::Handle->new, IO::Handle->new );
my $request = FCGI::Request( $stdin, $stdout, $stderr );
my $err_handler = sub { print {$stderr} @_ };

while($request->Accept() >= 0) {
    $SIG{__WARN__} =  $SIG{__DIE__} = $err_handler;

    warn "test1";
    die "test2";
}
test2 出现在我的错误日志中没有任何问题,但 test1 没有。

最佳答案

我试图用 Perl 编写的 FastCGI 客户端重现您的问题,FCGI.pm 产生预期的输出。也许 mod_fcgid 有问题。

#!/usr/bin/perl

use strict;
use warnings;

use IO::Socket             qw[];
use Net::FastCGI::Constant qw[:type :role];
use Net::FastCGI::IO       qw[read_record write_record write_stream];
use Net::FastCGI::Protocol qw[build_params dump_record build_begin_request_body];

use warnings FATAL => 'Net::FastCGI::IO';
use constant TRUE  => !!1;

my $socket = IO::Socket::INET->new(Proto => 'tcp', Listen => 5)
  or die qq/Could not create a listener socket: '$!'/;

my $host = $socket->sockhost;
my $port = $socket->sockport;

defined(my $pid = fork())
  or die qq/Could not fork(): '$!'/;

if (!$pid) {
    close STDIN;
    open(STDIN, '+>&', $socket)
      or die qq/Could not dup socket to STDIN: '$!'/;

    require FCGI;

    my $r = FCGI::Request()
      or die qq/Could not create a FCGI request: '$!'./;

    while ($r->Accept >= 0) {
        print "Perl: $] OS: $^O FCGI: $FCGI::VERSION\n";
        warn "test1";
        die "test2";
    }
    exit(0);
}

close $socket;
$socket = IO::Socket::INET->new(Proto => 'tcp', PeerHost => $host, PeerPort => $port)
  or die qq/Could not connect to '$host:$port': '$@'/;

write_record($socket, FCGI_BEGIN_REQUEST, 1, build_begin_request_body(FCGI_RESPONDER, 0));
write_stream($socket, FCGI_PARAMS, 1, build_params({}), TRUE);
write_stream($socket, FCGI_STDIN, 1, '', TRUE);
while () {
    my ($type, $request_id, $content) = read_record($socket)
      or exit;
    warn dump_record($type, $request_id, $content), "\n";
    last if $type == FCGI_END_REQUEST;
}

输出 :
{FCGI_STDERR, 1, "test1 at fcgi-die.pl line 35.\ntest2 at fcgi-die.pl line 36.\n"}
{FCGI_STDERR, 1, ""}
{FCGI_STDOUT, 1, "Perl: 5.014001 OS: darwin FCGI: 0.74\n"}
{FCGI_STDOUT, 1, ""}
{FCGI_END_REQUEST, 1, {0, FCGI_REQUEST_COMPLETE}}

关于perl - 我如何从运行 FCGI.pm 的 Perl 脚本中获取错误以显示在 Apache 错误日志中?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/14563686/

10-11 10:51