=encoding utf-8 =head1 NAME ngx_http_perl_module - Module ngx_http_perl_module =head1 The C module is used to implement location and variable handlers in Perl and insert Perl calls into SSI. This module is not built by default, it should be enabled with the C<--with-http_perl_module> configuration parameter. B This module requires L version 5.6.1 or higher. The C compiler should be compatible with the one used to build Perl. =head1 Known Issues The module is experimental, caveat emptor applies. In order for Perl to recompile the modified modules during reconfiguration, it should be built with the C<-Dusemultiplicity=yes> or C<-Dusethreads=yes> parameters. Also, to make Perl leak less memory at run time, it should be built with the C<-Dusemymalloc=no> parameter. To check the values of these parameters in an already built Perl (preferred values are specified in the example), run: $ perl -V:usemultiplicity -V:usemymalloc usemultiplicity='define'; usemymalloc='n'; Note that after rebuilding Perl with the new C<-Dusemultiplicity=yes> or C<-Dusethreads=yes> parameters, all binary Perl modules will have to be rebuilt as well — they will just stop working with the new Perl. There is a possibility that the main process and then worker processes will grow in size after every reconfiguration. If the main process grows to an unacceptable size, the L procedure can be applied without changing the executable file. While the Perl module is performing a long-running operation, such as resolving a domain name, connecting to another server, or querying a database, other requests assigned to the current worker process will not be processed. It is thus recommended to perform only such operations that have predictable and short execution time, such as accessing the local file system. =head1 Example Configuration http { perl_modules 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; } } The FlibEhello.pm> module: 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
"); if (-f $r->filename or -d _) { $r->print($r->uri, " exists!\n"); } return OK; } 1; __END__ =head1 Directives =head2 perl B perl I>::I>E'sub { ... }'> B I B I Sets a Perl handler for the given location. =head2 perl_modules B perl_modules I>> B I Sets an additional path for Perl modules. =head2 perl_require B perl_require I>> B I Defines the name of a module that will be loaded during each reconfiguration. Several C directives can be present. =head2 perl_set B perl_set I< I> I>::I>E'sub { ... }'> B I Installs a Perl handler for the specified variable. =head1 Calling Perl from SSI An SSI command calling Perl has the following format: =head1 The $r Request Object Methods =over =item C<$r-Eargs> returns request arguments. =item C<$r-Efilename> returns a filename corresponding to the request URI. =item C<$r-Ehas_request_body(I>)> returns 0 if there is no body in a request. If there is a body, the specified handler is set for the request and 1 is returned. After reading the request body, nginx will call the specified handler. Note that the handler function should be passed by reference. Example: package hello; use nginx; sub handler { my $r = shift; if ($r->request_method ne "POST") { return DECLINED; } if ($r->has_request_body(\&post)) { return OK; } return HTTP_BAD_REQUEST; } sub post { my $r = shift; $r->send_http_header; $r->print("request_body: \"", $r->request_body, "\"
"); $r->print("request_body_file: \"", $r->request_body_file, "\"
\n"); return OK; } 1; __END__ =item C<$r-Eallow_ranges> enables the use of byte ranges when sending responses. =item C<$r-Ediscard_request_body> instructs nginx to discard the request body. =item C<$r-Eheader_in(I>)> returns the value of the specified client request header field. =item C<$r-Eheader_only> determines whether the whole response or only its header should be sent to the client. =item C<$r-Eheader_out(I>, I>)> sets a value for the specified response header field. =item C<$r-Einternal_redirect(I>)> does an internal redirect to the specified I>. An actual redirect happens after the Perl handler execution is completed. B Redirections to named locations are currently not supported. =item C<$r-Elog_error(I>, I>)> writes the specified I> into the L. If I> is non-zero, an error code and its description will be appended to the message. =item C<$r-Eprint(I>, ...)> passes data to a client. =item C<$r-Erequest_body> returns the client request body if it has not been written to a temporary file. To ensure that the client request body is in memory, its size should be limited by L, and a sufficient buffer size should be set using L. =item C<$r-Erequest_body_file> returns the name of the file with the client request body. After the processing, the file should be removed. To always write a request body to a file, L should be enabled. =item C<$r-Erequest_method> returns the client request HTTP method. =item C<$r-Eremote_addr> returns the client IP address. =item C<$r-Eflush> immediately sends data to the client. =item C<$r-Esendfile(I>[, I>[, I>]])> sends the specified file content to the client. Optional parameters specify the initial offset and length of the data to be transmitted. The actual data transmission happens after the Perl handler has completed. =item C<$r-Esend_http_header([I>])> sends the response header to the client. The optional I> parameter sets the value of the C response header field. If the value is an empty string, the C header field will not be sent. =item C<$r-Estatus(I>)> sets a response code. =item C<$r-Esleep(I>, I>)> sets the specified handler and stops request processing for the specified time. In the meantime, nginx continues to process other requests. After the specified time has elapsed, nginx will call the installed handler. Note that the handler function should be passed by reference. In order to pass data between handlers, C<$r-Evariable()> should be used. Example: package hello; use nginx; sub handler { my $r = shift; $r->discard_request_body; $r->variable("var", "OK"); $r->sleep(1000, \&next); return OK; } sub next { my $r = shift; $r->send_http_header; $r->print($r->variable("var")); return OK; } 1; __END__ =item C<$r-Eunescape(I>)> decodes a text encoded in the “%XX” form. =item C<$r-Euri> returns a request URI. =item C<$r-Evariable(I>[, I>])> returns or sets the value of the specified variable. Variables are local to each request. =back