我必须运行一段非常简单的 perl 代码来响应 HTTP 请求。nginx 配置如下:
http {
perl_modules perl/lib;
perl_require errors.pm;
server {
listen 80;
server_name _;
root /usr/share/nginx/html;
location / {
perl errors::response;
}
}
}
我尝试运行的代码是:
package errors;
use strict;
use warnings;
use nginx;
my $errors = {
ThisIsNotOK => {
'code' => '409',
'amzcode' => 'NOK',
'message' => 'Something went really wrong'
},
ItsOK => {
'code' => '200',
'amzcode' => 'OK',
'message' => 'Okay'
},
};
my $fmt =<<ENDFMT;
<?xml version="1.0" encoding="UTF-8"?>
<Error>
<Code>%s</Code>
<Message>%s</Message>
<Resource>/mybucket/whatever.1</Resource>
<RequestId>DEADBEEFBEDAEDA</RequestId>
</Error>
ENDFMT
sub response() {
my $r = shift();
my $curr_err = (keys %$errors)[rand keys %$errors];
$r->send_http_header("application/xml");
$r->status($errors->{$curr_err}{'code'});
$r->print(sprintf($fmt,$errors->{$curr_err}{'amzcode'}, $errors->{$curr_err}{'message'}));
return $errors->{$curr_err}{'code'};
}
1;
__END__
问题是当此代码返回 200/OK 以外的内容时,我收到空响应错误:
%curl -iX获取本地主机
curl:(52)服务器回复为空
nginx 版本:nginx/1.14.1
由 gcc 8.2.1 20180905 (Red Hat 8.2.1-3) (GCC) 构建
使用 OpenSSL 1.1.1 FIPS 于 2018 年 9 月 11 日构建(使用 OpenSSL 1.1.1c FIPS 于 2019 年 5 月 28 日运行)
已启用 TLS SNI 支持
我做错了什么?