這兩天心血來潮,不知道爲何和 Perl + Nginx + FastCGI 配置 耗上了。可是失敗了,記錄以下:
1)安裝Nginx 1.4.3 ,個人是WINDOWS 7 系統,修改配置文件以下:
html
location ~ \.(pl|cgi|perl)?$ { root var/www/huanghongqiao; fastcgi_pass 127.0.0.1:9002; fastcgi_index index.cgi; fastcgi_param SCRIPT_FILENAME cgi$fastcgi_script_name; include fastcgi_params; }2)安裝FCGI 模塊,使用 PPM。
3)在 var/www/huanghongqiao/cgi 目錄下新建 test.PL ,代碼以下: linux
use FCGI; my $socket = FCGI::OpenSocket( "localhost:9002", 5 ); my $request = FCGI::Request( \*STDIN, \*STDOUT, \*STDERR, \%ENV, $socket ); my $count; while( $request->Accept() >= 0 ) { print "Content-type: text/html\r\n\r\n"; print "nums is .. "; print ++$count; } FCGI::CloseSocket( $socket );4) 運行這個test.PL 和 nginx,結果以下圖:
5) 從網上下了 nginx-fcgi.pl回來修改,由於它不能在個人windows下運行,估計就是針對linux系統寫的。
修改後以下代碼:
nginx
use FCGI; sub main { $socket = FCGI::OpenSocket( "localhost:9002", 5 ); $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--; } } $file = $req_params{DOCUMENT_ROOT} . '/' . $req_params{SCRIPT_FILENAME}; $file =~ s@\\@/@g; #我測試過了,得拼接成絕對物理路徑 #我不知道怎麼才能用$req_params{SCRIPT_FILENAME}這個相對路徑 #不然提示沒有文件。 if ((-x $file) && (-s $file) && (-r $file) ){ foreach $key ( keys %req_params){ $ENV{$key} = $req_params{$key}; } open $cgi_app, '-|', $file, $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 <$cgi_app>; close $cgi_app; } } else { print("Content-type: text/plain\r\n\r\n"); print "$file size is empty!\n" if not -s $file; print "$file can't execute!\n" if not -x $file; print "$file can't readable!\n" if not -r $file; print "Error: No such CGI app - $file may not exist or is not executable by this process.\n"; } } } main();6)test.pl 修改爲簡單的 print "" 等內容
7)運行這個 nginx-fcgi.pl,打開頁面顯示:
shell
E:/nginx-1.4.3/var/www/huanghongqiao/cgi/test.pl can't execute! Error: No such CGI app - E:/nginx-1.4.3/var/www/huanghongqiao/cgi/test.pl may not exist or is not executable by this process.若是我去掉 (-x $file) 這個測試,errlo.log 裏就會顯示 :
2013/10/27 20:20:56 [error] 1496#2172: *45 upstream prematurely closed FastCGI stdout while reading response header from upstream, client: 127.0.0.1, server: localhost, request: "GET /test.pl HTTP/1.1", upstream: "fastcgi://127.0.0.1:9002", host: "localhost:82" windows
8) 實在不知道怎麼弄了,難道和 execute 有關係嗎?Windows下不知道怎麼修改可執行呢????
app