设为首页 收藏本站
查看: 649|回复: 0

[经验分享] nginx.cgi

[复制链接]

尚未签到

发表于 2018-11-12 08:39:08 | 显示全部楼层 |阅读模式
  #!/usr/bin/perl
  #
  #    author        Daniel Dominik Rudnicki
  #    thanks to:    Piotr Romanczuk
  #    email         daniel@sardzent.org
  #    version       0.4.3
  #    webpage       http://www.nginx.eu/
  #
  #    BASED @ http://wiki.codemongers.com/NginxSimpleCGI
  #
  #
  # use strict;
  use FCGI;
  #perl -MCPAN -e 'install FCGI'
  use Getopt::Long;
  use IO::All;
  use Socket;
  sub init {
  GetOptions(
  "h"    => \$help,
  "verbose!"=>\$verbose,
  "pid=s"    => \$filepid,
  "l=s" => \$logfile,
  "S:s"   => \$unixsocket,
  "P:i"   => \$unixport
  ) or usage();
  usage() if $help;
  print "    Starting Nginx-fcgi\n" if $verbose;
  print "    Running with $> UID" if $verbose;
  print "    Perl $]" if $verbose;
  #if ( $> == "0" ) {
  #print "\n\tERROR\tRunning as a root!\n";
  #print "\tSuggested not to do so !!!\n\n";
  #exit 1;
  #}
  if ( ! $logfile ) {
  print "\n\tERROR\t log file must declared\n"
  . "\tuse $0 with option -l filename\n\n";
  exit 1;
  }
  print "    Using log file $logfile\n" if $verbose;
  "\n\n" >> io($logfile);
  addlog($logfile, "Starting Nginx-cfgi");
  addlog($logfile, "Running with $> UID");
  addlog($logfile, "Perl $]");
  addlog($logfile, "Testing socket options");
  if ( ($unixsocket && $unixport) || (!($unixsocket) && !($unixport)) ) {
  print "\n\tERROR\tOnly one option can be used!\n";
  print "\tSuggested (beacuse of speed) is usage UNIX socket -S \n\n";
  exit 1;
  }
  if ($unixsocket) {
  print "    Daemon listening at UNIX socket $unixsocket\n" if $versbose;
  addlog($logfile, "Deamon listening at UNIX socket $unixsocket");
  } else {
  print "    Daemon listening at TCP/IP socket *:$unixport\n" if $verbose;
  #
  addlog($logfile, "Daemon listening at TCP/IP socket *:$unixport");
  }
  if ( -e $filepid ) {
  print "\n\tERROR\t PID file $filepid already exists\n\n";
  addlog($logfile, "Can not use PID file $filepid, already exists.");
  exit 1;
  }
  if ( $unixsocket ) {
  print "    Creating UNIX socket\n" if $verbose;
  $socket = FCGI::OpenSocket( $unixsocket, 10 );
  if ( !$socket) {
  print "    Couldn't create socket\n";
  addlog($logfile, "Couldn't create socket");
  exit 1;
  }
  print "    Using UNIX socket $unixsocket\n" if $verbose;
  } else {
  print "    Creating TCP/IP socket\n" if $verbose;
  $portnumber = ":".$unixport;
  $socket = FCGI::OpenSocket( $unixport, 10 );
  if ( !$socket ) {
  print "    Couldn't create socket\n";
  addlog($logfile, "Couldn't create socket");
  exit 1;
  }
  print " Using port $unixport\n" if $verbose;
  }
  addlog($logfile, "Socket created");
  if ( ! $filepid ) {
  print "\n\tERROR\t PID file must declared\n"
  . "\tuse $0 with option -pid filename\n\n";
  exit 1;
  }
  print "    Using PID file $filepid\n" if $verbose;
  addlog($logfile, "Using PID file $filepid");
  my $pidnumber = $$;
  $pidnumber > io($filepid);
  print " PID number $$\n" if $verbose;
  addlog($logfile, "PID number $pidnumber");
  }
  sub addzero {
  my ($date) = shift;
  if ($date < 10) {
  return "0$date";
  }
  return $date;
  }
  sub logformat {
  my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$iddst) = localtime(time);
  my $datestring;
  $year += 1900;
  $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 1;
  }
  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 1;
  }
  init;
  #
  END() { } BEGIN() { }
  *CORE::GLOBAL::exit = sub { die "fakeexit\nrc=".shift()."\n"; }; eval q{exit};
  if ($@) {
  exit unless $@ =~ /^fakeexit/;
  };
  # fork part
  my $pid = fork();
  if( $pid == 0 ) {
  &main;
  exit 0;
  }
  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 0;
  sub main {
  $request = FCGI::Request( \*STDIN, \*STDOUT, \*STDERR, \%req_params, $socket );
  if ($request) { request_loop()};
  FCGI::CloseSocket( $socket );
  }
  sub request_loop {
  while( $request->Accept() >= 0 ) {
  # processing any STDIN input from WebServer (for CGI-POST actions)
  $stdin_passthrough = '';
  $req_len = 0 + $req_params{'CONTENT_LENGTH'};
  if (($req_params{'REQUEST_METHOD'} eq 'POST') && ($req_len != 0) ){
  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 !");
  if ($cgi_app) {
  print ;
  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.");
  }
  }
  }


运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.iyunv.com/thread-633911-1-1.html 上篇帖子: nginx不得不说的参数之 为什么nginx error_page遇到后端错误时不跳转? 下篇帖子: nginx 日志问题
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表