FAQ

Page Discussion Edit History

SimplePerl

Contents

[edit] Nginx::Simple

Nginx::Simple is a Perl module which allows for a "Apache mod_perl-like" interface. Version (0.04)

[edit] Synopsis:

[edit] nginx.conf

 perl_modules perl;
 perl_require Test.pm;
 server {
   listen       80;
   server_name  localhost;
   location / {
     perl Test::handler;
   }
 }

[edit] Test.pm

 package Test;
 use Nginx::Simple;
 
 sub main # mandatory
 {
   my $self = shift;
 
   $self->header_set('Content-Type' => 'text/html');
   $self->print('rock on!');
 
   $self->log('I found a rabbit...');
 
   my $something = $self->param("here");
   $self->print("I found $something...");
 }
 
 # (optional) triggered after main runs
 sub cleanup
 {
   my $self = shift;
 
   # do stuff here---like clean database connections?
 }
 
 # (optional) triggered on a server error, otherwise it will return a standard nginx 500 error
 sub error
 {
   my $self = shift;
   my $error = shift;
   $self->status(500);
   $self->print("oh, uh, there is an error! ($error)");
 }

CPAN module: http://search.cpan.org/dist/Nginx-Simple/lib/Nginx/Simple.pm