nginx + встроенный perl


Установка

В пакете extras perl модуль уже есть


$ sudo apt-get install nginx-extras

$ nginx -V
nginx version: nginx/1.10.3 (Ubuntu)
built with OpenSSL 1.0.2g  1 Mar 2016
TLS SNI support enabled
***
--with-http_perl_module
***

Пример из документации


$ cat /etc/nginx/nginx.conf
http {

  perl_modules /etc/nginx/perl/lib;
  perl_require hello.pm;

  perl_set $msie6 '

      sub {
          my $r = shift;
          my $ua = $r->header_in("User-Agent");

          return "" if $ua =~ /Opera/;
          return "1" if $ua =~ / MSIE [6-9]\.\d+/;
          return "";
      }

  ';

  server {
      location / {
          perl hello::handler;
      }
  }
}

$ cat /etc/nginx/perl/lib/hello.pm 
package hello;

use nginx;

sub handler {
    my $r = shift;

    $r->send_http_header("text/html");
    return OK if $r->header_only;

    $r->print("hello!\n<br/>");

    if (-f $r->filename or -d _) {
        $r->print($r->uri, " exists!\n");
    }

    return OK;
}

1;
__END__

Результат


$ curl localhost
hello world!
<br/>/ exists!