4

下面是一个示例脚本:

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

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

有关 Apache 配置的更多信息,此行用于配置处理程序:

Addhandler fcgid-script .fcgi

我被告知 suexec 正在使用中并充当 fcgi 包装器。


编辑:

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

默认情况下没有安装 die 和 warn 处理程序。这意味着如果您没有运行启用了 sfio 的 perl,则默认情况下,任何警告或死亡消息都不会最终出现在服务器的日志中。建议您自己设置模具并警告处理程序。FCGI.pm 包含一个 die 和 warn 处理程序的示例。

因此,我以这种方式尝试过:

#!/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没有。

4

1 回答 1

0

我尝试使用用 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}}
于 2013-04-06T17:50:43.663 回答