在傳統CGI腳本中抑制默認mod_perl錯誤頁面我有一個Perl中的CGI腳本,它自己生成HTTP錯誤頁面。我經由ModPerl::Registry mod_perl下運行它,使用以下的Apache2配置:如何使用ModPerl :: Registry
Alias /perl "/var/www/perl"
<Directory "/var/www/perl">
SetHandler perl-script
PerlResponseHandler ModPerl::Registry
PerlOptions +ParseHeaders
Options Indexes FollowSymlinks +ExecCGI
AllowOverride None
Order allow,deny
Allow from all
</Directory>
一切都很好,除了一個小的問題:當在頭印刷HTTP狀態是超過200種不同(例如404),阿帕奇附加一個默認的HTML錯誤文檔到我自己生成的響應。
採取例如以下簡單的CGI腳本:
#!/usr/bin/perl
use strict;
use warnings;
use CGI qw(:standard :escapeHTML -nosticky);
use CGI::Carp qw(fatalsToBrowser);
use Apache2::Const qw(:http :common);
our $cgi = CGI->new();
print $cgi->header(-type=>'text/html', -charset => 'utf-8',
-status=> '404 Not Found');
our $mod_perl_version = $ENV{'MOD_PERL'} ? " $ENV{'MOD_PERL'}" : '';
print <<"EOF";
<html>
<head>
<title>die_error_minimal$mod_perl_version
</head>
<body>
404 error
</body>
</html>
EOF
exit;
在
HTTP/1.1 404 Not Found
Date: Sun, 27 Nov 2011 13:17:59 GMT
Server: Apache/2.0.54 (Fedora)
Connection: close
Transfer-Encoding: chunked
Content-Type: text/html; charset=utf-8
<html>
<head>
<title>die_error_minimal mod_perl/2.0.1
</head>
<body>
404 error
</body>
</html>
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>404 Not Found</title>
</head><body>
<h1>Not Found</h1>
<p>The requested URL /perl/die_error_minimal.cgi was not found on this server.</p>
<hr>
<address>Apache/2.0.54 (Fedora) Server at localhost Port 80</address>
</body></html>
注意與上述結果提到Apache配置運行它的是,在例如CGI腳本上面替換exit;
任return Apache2::Const::OK;
或return Apache2::Const::DONE;
,如「How do I suppress the default apache error document in mod_perl?」中所建議的那樣,對SO沒有幫助 - 結果保持不變。
我應該在我的Apache配置中修復什麼,或者我應該添加到我的CGI腳本以抑制mod_perl/Apache生成響應的附加錯誤頁面?
BTW。你使用什麼** Apache配置?如果它與我的不同,可以將它添加到響應中嗎? TIA。 –
謝謝,它的作品。 –
不幸的是,在某些情況下(閱讀:對於其他人),這會導致發送HTTP狀態爲「200 OK」的頁面而不是正確的錯誤信息。 –