Chinaunix首页 | 论坛 | 博客
  • 博客访问: 119798
  • 博文数量: 34
  • 博客积分: 1485
  • 博客等级: 上尉
  • 技术积分: 351
  • 用 户 组: 普通用户
  • 注册时间: 2007-05-10 11:06
文章分类

全部博文(34)

文章存档

2011年(2)

2010年(17)

2009年(4)

2008年(7)

2007年(4)

我的朋友

分类: LINUX

2010-03-24 15:22:24

ginx 上一直不支持,perl 的运行,现有一种运行perl fastcgi 的方式来,在nginx运行.

系统 suselinux sp2   ubuntu 9.04 测试通过

1,安装perl moduler
cpan FCGI
2, 安装 nagios nginx

3,下载perl_cgi.pl
#!/usr/bin/perl

use FCGI;
#perl -MCPAN -e 'install FCGI'
use Socket;
use POSIX qw(setsid);
#use Fcntl;
 
require 'syscall.ph';
 
&daemonize;
 
#this keeps the program alive or something after exec'ing perl scripts
END() { } BEGIN() { }
*CORE::GLOBAL::exit = sub { die "fakeexit\nrc=".shift()."\n"; }; 
eval q{exit}; 
if ($@) { 
	exit unless $@ =~ /^fakeexit/; 
};
 
&main;
 
sub daemonize() {
    chdir '/'                 or die "Can't chdir to /: $!";
    defined(my $pid = fork)   or die "Can't fork: $!";
    exit if $pid;
    setsid                    or die "Can't start a new session: $!";
    umask 0;
}
 
sub main {
        #$socket = FCGI::OpenSocket( "127.0.0.1:8999", 10 ); #use IP sockets
        $socket = FCGI::OpenSocket( "/usr/local/nginx/logs/cgi.sock", 10 ); #use UNIX sockets - user running this script must have w access to the 'nginx' folder!!
        $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) ){ 
                my $bytes_read = 0;
                while ($bytes_read < $req_len) {
                        my $data = '';
                        my $bytes = read(STDIN, $data, ($req_len - $bytes_read));
                        last if ($bytes == 0 || !defined($bytes));
                        $stdin_passthrough .= $data;
                        $bytes_read += $bytes;
                }
            }
 
            #running the cgi app
            if ( (-x $req_params{SCRIPT_FILENAME}) &&  #can I execute this?
                 (-s $req_params{SCRIPT_FILENAME}) &&  #Is this file empty?
                 (-r $req_params{SCRIPT_FILENAME})     #can I read this file?
            ){
		pipe(CHILD_RD, PARENT_WR);
		my $pid = open(KID_TO_READ, "-|");
		unless(defined($pid)) {
			print("Content-type: text/plain\r\n\r\n");
                        print "Error: CGI app returned no output - Executing $req_params{SCRIPT_FILENAME} failed !\n";
			next;
		}
		if ($pid > 0) {
			close(CHILD_RD);
			print PARENT_WR $stdin_passthrough;
			close(PARENT_WR);
 
			while(my $s = ) { print $s; }
			close KID_TO_READ;
			waitpid($pid, 0);
		} else {
	                foreach $key ( keys %req_params){
        	           $ENV{$key} = $req_params{$key};
                	}
        	        # cd to the script's local directory
	                if ($req_params{SCRIPT_FILENAME} =~ /^(.*)\/[^\/]+$/) {
                        	chdir $1;
                	}
 
			close(PARENT_WR);
			close(STDIN);
			#fcntl(CHILD_RD, F_DUPFD, 0);
			syscall(&SYS_dup2, fileno(CHILD_RD), 0);
			#open(STDIN, "<&CHILD_RD");
			exec($req_params{SCRIPT_FILENAME});
			die("exec failed");
		}
            } 
            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";
            }
 
        }
}

注意以上这行
$socket = FCGI::OpenSocket( "/usr/local/nginx/logs/cgi.sock", 10 );
那个目录是,cgi 建的sock 的文件,要在nginx 里使用.

chmod +x perl_cgi.pl

4.设置nginx

server {
        
        listen       192.168.0.60:80;
        #listen       nginx-nagios:80;
        server_name  nagios.test.com;
 
        #charset koi8-r;
 
        access_log  /var/log/nginx/nagios.test.com.log  main;
	error_log   /var/log/nginx/error-nagios.log  error;
 
 
 
        location / {
 
	    root /usr/local/nagios/share;
	    index index.php index.html;
	    auth_basic      "nagios";
            auth_basic_user_file    /usr/local/nagios/etc/.htpasswd; 
            #auth_basic_user_file    /usr/local/nginx/conf/htpasswd.users;
        }
 
	location /nagios/ {
	    alias /usr/local/nagios/share/;		
	}
 
	location ~\.cgi$ {
	    root /usr/local/nagios/sbin;
            rewrite ^/nagios/cgi-bin/(.*)\.cgi /$1.cgi break;
            fastcgi_pass unix:/usr/local/nginx/logs/cgi.sock;
            #fastcgi_pass 127.0.0.1:8999;
            fastcgi_param  SCRIPT_FILENAME  /usr/local/nagios/sbin/$fastcgi_script_name;
            include        fastcgi_params; 
            fastcgi_index  index.cgi;
            auth_basic      "nagios";
            #auth_basic_user_file    /usr/local/nginx/conf/htpasswd.users; 
            auth_basic_user_file    /usr/local/nagios/etc/.htpasswd; 
        }
 
 
 
	location ~ .+\.php$ {
            root  /usr/local/nagios/share; 
 
            fastcgi_pass   127.0.0.1:1026;
            fastcgi_index  index.php;
 
            fastcgi_param  SCRIPT_FILENAME  /usr/local/nagios/share$fastcgi_script_name;
        }
 
 
 
        #include server-commons.inc;
 
        # for JSP web applications
	include        fastcgi_params;
        # for backward compatibility, some old URLs for SOAP web service are mapped
}

注意以下两处
fastcgi_pass unix:/usr/local/nginx/logs/cgi.sock;
上文说的cgi的sock文件.
auth_basic_user_file /usr/local/nagios/etc/.htpasswd;
nagios manager 的用户和密码.


阅读(1611) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~