0

I have to run a very simplistic piece of perl code in response to HTTP request. The nginx config is:

http { 

    perl_modules perl/lib;
    perl_require errors.pm;

    server {
        listen       80;
        server_name  _;
        root         /usr/share/nginx/html;

        location / {
                perl errors::response;
        }
    }
}

The code I try to run is:

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__

The problem is when this code returns something other than 200/OK, I get the empty response error:

% curl -iX GET localhost

curl: (52) Empty reply from server

nginx version: nginx/1.14.1

built by gcc 8.2.1 20180905 (Red Hat 8.2.1-3) (GCC)

built with OpenSSL 1.1.1 FIPS 11 Sep 2018 (running with OpenSSL 1.1.1c FIPS 28 May 2019)

TLS SNI support enabled

What do I do wrong?

  • Welcome to ServerFault, sotona. Have you looked in the logs? – chicks Jun 1 '20 at 21:33
  • I did. The only useful thing there is " [alert] 6170#0: *6 header already sent, client: 127.0.0.1, server: centos01, request: "GET / HTTP/1.1", host: "localhost"" – sotona Jun 2 '20 at 7:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.