配置文件如下:

请注意,网上提供的官方文档在运行时可能会出现问题,此文中保证无问题。

  1 #!/usr/bin/perl
  2 #
  3 #       author          Daniel Dominik Rudnicki
  4 #       thanks to:      Piotr Romanczuk
  5 #       email           daniel@sardzent.org
  6 #       version         0.4.
  7 #       webpage         http://www.nginx.eu/
  8 #
  9 #       BASED @ http://wiki.codemongers.com/NginxSimpleCGI
 10 #
 11 #
 12 # use strict;
 13 use FCGI;
 14 use Getopt::Long;
 15 use IO::All;
 16 use Socket;
 17 
 18 sub init {
 19         GetOptions(     "h"     => \$help,
 20                         "verbose!"=>\$verbose,
 21                         "pid=s" => \$filepid,
 22                         "l=s" => \$logfile,
 23                         "S:s"   => \$unixsocket,
 24                         "P:i"   => \$unixport) or usage();
 25                 usage() if $help;
 26 
 27         print " Starting Nginx-fcgi\n" if $verbose;
 28         print " Running with $> UID" if $verbose;
 29         print " Perl $]" if $verbose;
 30 
 31 #       if ( $> == "" ) {
 32 #               print "\n\tERROR\tRunning as a root!\n";
 33 #               print "\tSuggested not to do so !!!\n\n";
 34 #               exit ;
 35 #       }
 36 
 37         if ( ! $logfile ) {
 38                 print "\n\tERROR\t log file must declared\n"
 39                         . "\tuse $0 with option -l filename\n\n";
 40                 exit ;
 41         }
 42         print " Using log file $logfile\n" if $verbose;
 43         "\n\n" >> io($logfile);
 44         addlog($logfile, "Starting Nginx-cfgi");
 45         addlog($logfile, "Running with $> UID");
 46         addlog($logfile, "Perl $]");
 47         addlog($logfile, "Testing socket options");
 48 
 49         if ( ($unixsocket && $unixport) || (!($unixsocket) && !($unixport)) ) {
 50                 print "\n\tERROR\tOnly one option can be used!\n";
 51                 print "\tSuggested (beacuse of speed) is usage UNIX socket -S \n\n";
 52                 exit ;
 53         }
 54 
 55         if ($unixsocket) {
 56                 print " Daemon listening at UNIX socket $unixsocket\n" if $versbose;
 57                 addlog($logfile, "Deamon listening at UNIX socket $unixsocket");
 58         } else {
 59                 print " Daemon listening at TCP/IP socket *:$unixport\n" if $verbose;
 60                 #
 61                 addlog($logfile, "Daemon listening at TCP/IP socket *:$unixport");
 62         }
 63 
 64         if ( -e $filepid ) {
 65                 print "\n\tERROR\t PID file $filepid already exists\n\n";
 66                 addlog($logfile, "Can not use PID file $filepid, already exists.");
 67                 exit ;
 68         }
 69 
 70         if ( $unixsocket ) {
 71                 print " Creating UNIX socket\n" if $verbose;
 72                 $socket = FCGI::OpenSocket( $unixsocket,  );
 73                 if ( !$socket) {
 74                         print " Couldn't create socket\n";
 75                         addlog($logfile, "Couldn't create socket");
 76                         exit ;
 77                 }
 78                 print " Using UNIX socket $unixsocket\n" if $verbose;
 79         } else {
 80                 print " Creating TCP/IP socket\n" if $verbose;
 81                 $portnumber = ":".$unixport;
 82                 $socket = FCGI::OpenSocket( $unixport,  );
 83                 if ( !$socket ) {
 84                         print " Couldn't create socket\n";
 85                         addlog($logfile, "Couldn't create socket");
 86                         exit ;
 87                 }
 88                 print " Using port $unixport\n" if $verbose;
 89         }
 90         addlog($logfile, "Socket created");
 91 
 92         if ( ! $filepid ) {
 93                 print "\n\tERROR\t PID file must declared\n"
 94                         . "\tuse $0 with option -pid filename\n\n";
 95                 exit ;
 96         }
 97         print " Using PID file $filepid\n" if $verbose;
 98         addlog($logfile, "Using PID file $filepid");
 99 
         my $pidnumber = $$;
         $pidnumber > io($filepid);
         print " PID number $$\n" if $verbose;
         addlog($logfile, "PID number $pidnumber");
 
 }
 
 sub addzero {
         my ($date) = shift;
         if ($date < ) {
                 return "0$date";
         }
        return $date;
 }
 
 sub logformat {
         my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$iddst) = localtime(time);
         my $datestring;
         $year += ;
         $mon++;
         $mon  = addzero($mon);
         $mday = addzero($mday);
         $min  = addzero($min);
         $datestring = "$year-$mon-$mday $hour:$min";
         return($datestring);
 }
 
 sub addlog {
         my ($log_file, $log_message) = @_;
         my $curr_time = logformat();
         my $write_message = "[$curr_time]   $log_message";
         $write_message >> io($log_file);
         "\n" >> io($log_file);
 }
 
 sub printerror {
         my $message = @_;
         print "\n       Nginx FastCGI\tERROR\n"
                 . "\t $message\n\n";
         exit ;
 }
 
 sub usage {
         print "\n       Nginx FastCGI \n"
                 . "\n\tusage: $0 [-h] -S string -P int\n"
                 . "\n\t-h\t\t: this (help) message"
                 . "\n\t-S path\t\t: path for UNIX socket"
                 . "\n\t-P port\t\t: port number"
                 . "\n\t-p file\t\t: path for pid file"
                 . "\n\t-l file\t\t: path for logfile"
                 . "\n\n\texample: $0 -S /var/run/nginx-perl_cgi.sock -l /var/log/nginx/nginx-cfgi.log -pid /var/run/nginx-fcgi.pid\n\n";
         exit ;
 }
 
 
 init;
         exit unless $@ =~ /^fakeexit/;
 } ;
 
 # fork part
 my $pid = fork();
 
 if( $pid ==  ) {
         &main;
         exit ;
 }
 
 print " Forking worker process with PID $pid\n" if $verbose;
 addlog($logfile, "Forking worker process with PID $pid");
 print " Update PID file $filepid\n" if $verbose;
 addlog($logfile, "Update PID file $filepid");
 $pid > io($filepid);
 print " Worker process running.\n" if $verbose;
 addlog ($logfile, "Parent process $$ is exiting");
 exit ;
 
 sub main {
         $request = FCGI::Request( \*STDIN, \*STDOUT, \*STDERR, \%req_params, $socket );
         if ($request) { request_loop()};
                 FCGI::CloseSocket( $socket );
 }
 
 sub request_loop {
         while( $request->Accept() >=  ) {
                 # processing any STDIN input from WebServer (for CGI-POST actions)
                 $stdin_passthrough = '';
                 $req_len =  + $req_params{'CONTENT_LENGTH'};
                 if (($req_params{'REQUEST_METHOD'} eq 'POST') && ($req_len != ) ){
                         while ($req_len) {
                                 $stdin_passthrough .= getc(STDIN);
                                 $req_len--;
                         }
                 }
 
                 # running the cgi app
                 if ( (-x $req_params{SCRIPT_FILENAME}) &&
                         (-s $req_params{SCRIPT_FILENAME}) &&
                         (-r $req_params{SCRIPT_FILENAME})
                 ){
                         foreach $key ( keys %req_params){
                                 $ENV{$key} = $req_params{$key};
                         }
                         if ( $verbose ) {
                                 addlog($logfile, "running $req_params{SCRIPT_FILENAME}");
                         }
                         # http://perldoc.perl.org/perlipc.html#Safe-Pipe-Opens
                         #
                         #open $cgi_app, '-|', $req_params{SCRIPT_FILENAME}, $stdin_passthrough or print("Content-type: text/plain\r\n\r\n"); print "Error: CGI app returned no output - Executing $req_params{SCRIPT_FILENAME} failed !\n"; # addlog($logfile, "Error: CGI app returned no output - Executing $req_params{SCRIPT_FILENAME} failed !");
                         open $cgi_app, '-|', "echo '$stdin_passthrough' | '$req_params{SCRIPT_FILENAME}'" or print("Content-type: text/plain\r\n\r\n"); print "Error: CGI app returned no output - Executing $req_params{SCRIPT_FILENAME} failed !\n"; # addlog($logfile, "Error: CGI app returned no output - Executing $req_params{SCRIPT_FILENAME} failed !");
 
                         if ($cgi_app) {
                                 print <$cgi_app>;
                                 close $cgi_app;
                         }
                 } else {
                         print("Content-type: text/plain\r\n\r\n");
                         print "Error: No such CGI app - $req_params{SCRIPT_FILENAME} may not exist or is not executable by this process.\n";
                         addlog($logfile, "Error: No such CGI app - $req_params{SCRIPT_FILENAME} may not exist or is not executable by this process.");
                 }
         }
 }

221

最新文章

  1. mac 下配置 git
  2. 国内其他的maven库
  3. jenkins邮件通知功能
  4. 什么是FOUC?如何避免FOUC?///////////////////////////zzzz
  5. PowerDesigner的数据类型
  6. Thread详解
  7. USE_DB_RECOVERY_FILE_DEST的使用详解(转载)
  8. docker学习笔记(1)
  9. canvas1
  10. HDU--杭电--3415--Max Sum of Max-K-sub-sequence--队列--双向队列
  11. absort函数和exit函数
  12. centos7上修改主机名
  13. Spring常用注解总结(2)
  14. Windows 10 安装 Mongod
  15. latex 图片标题居中
  16. malloc函数 链表
  17. Java系统和PHP系统相互调用
  18. JSP复习(part 3 )
  19. 修改 /etc/pam.d/login, linux 本地账号密码无法登陆,一直返回 登陆的login界面
  20. 关于java调用Dll文件的异常 Native library (win32-x86-64/CtrlNPCDLL.dll) not found in resource pat

热门文章

  1. C# 导出到Excel
  2. [原创]cocos2d-x研习录-第二阶 概念类之节点类(CCNode)
  3. c语言多线程队列读写
  4. Sea.js学习5——Sea.js的构建工具spm
  5. 正常月报表年初未分配利润修改backup
  6. sql 数据量高并发的数据库优化(转)
  7. ios系统的中arm指令集
  8. Python从题目中学习:random() module
  9. APPLICATION ERROR #1502 .
  10. openldap加密传输 nslcd