WEB 서버 의 Nginx 지원 CGI

6138 단어 WebNginx CGI 지원
NGINX 설치 절 차 는 생략 합 니 다.
아래 작업 은 모두 su 에서 완료 합 니 다.
======================================================================
perl 약 을 설치 하 다.(yum -y install perl*)
perl - FCGI 지원 오픈
perl -MCPAN -e 'install FCGI'
perl -MCPAN -e 'install FCGI::ProcManager
perl - fcgi 데 몬 perl - fcgi. pl 을 nginx / sbin 디 렉 터 리 에 저장 하도록 설정 합 니 다.chmod 755 perl-fcgi.pl
#!/usr/bin/perl
use FCGI;
use Socket;
use POSIX qw(setsid);
require 'syscall.ph';
&daemonize;
END() { } BEGIN() { }
*CORE::GLOBAL::exit = sub { die "fakeexit
rc=".shift()."
"; }; 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( "/usr/local/services/nginx/logs/perl-fcgi.sock", 51200 ); #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
\r
"); print "Error: CGI app returned no output - Executing $req_params{SCRIPT_FILENAME} failed !
"; next; } if ($pid > 0) { close(CHILD_RD); print PARENT_WR $stdin_passthrough; close(PARENT_WR); while(my $s = <KID_TO_READ>) { 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
\r
"); print "Error: No such CGI app - $req_params{SCRIPT_FILENAME} may not exist or is not executable by this process.
"; } } }

테스트 스 크 립 트 / bin / perl - cgi &
성공 하면 다음 과 같은 정보 가 나 옵 니 다.
FastCGI: server (pid 21315): initialized
FastCGI: manager (pid 17915): server (pid 21315) started
NGINX 설정
======================================================================
vi / usr / local / services / nginx / conf / fcgi. conf (NGINX / conf 에 저장)
fastcgi_index   index.cgi;
fastcgi_pass  unix:/usr/local/services/nginx/logs/perl-fcgi.sock;
fastcgi_param SCRIPT_FILENAME   $document_root$fastcgi_script_name;
fastcgi_param QUERY_STRING      $query_string;
fastcgi_param REQUEST_METHOD    $request_method;
fastcgi_param CONTENT_TYPE      $content_type;
fastcgi_param CONTENT_LENGTH    $content_length;
fastcgi_param GATEWAY_INTERFACE CGI/1.1;
fastcgi_param SERVER_SOFTWARE   nginx;
fastcgi_param SCRIPT_NAME       $fastcgi_script_name;
fastcgi_param REQUEST_URI       $request_uri;
fastcgi_param DOCUMENT_URI      $document_uri;
fastcgi_param DOCUMENT_ROOT     $document_root;
fastcgi_param SERVER_PROTOCOL   $server_protocol;
fastcgi_param REMOTE_ADDR       $remote_addr;
fastcgi_param REMOTE_PORT       $remote_port;
fastcgi_param SERVER_ADDR       $server_addr;
fastcgi_param SERVER_PORT       $server_port;
fastcgi_param SERVER_NAME       $server_name;
fastcgi_read_timeout            60;

다음은 cgi 가상 컴퓨터 설정 예제 입 니 다.
server { 
             listen             80; 
             server_name        urdomain.com; 
             index              index.cgi 
             root               /var/www; 
             location ~  .*\.cgi? { 
                  include         fcgi.conf; 
             } 
     }

/ usr / local / nginx / sbin / nginx - t 테스트 OK 후 nginx 프로필 / usr / local / nginx / sbin / nginx - s reload 다시 불 러 오기

좋은 웹페이지 즐겨찾기