记一次失败的Perl + Nginx + FastCGI 配置过程
作者:互联网
这两天心血来潮,不知道为什么和 Perl + Nginx + FastCGI 配置 耗上了。但是失败了,记录如下:
1)安装Nginx 1.4.3 ,我的是WINDOWS 7 系统,修改配置文件如下:
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 ,代码如下:
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系统写的。
修改后如下代码:
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 ; 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,打开页面显示:
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"
8) 实在不知道怎么弄了,难道和 execute 有关系吗?Windows下不知道怎么修改可执行呢????
标签:cgi,FCGI,req,Perl,Nginx,params,file,print,FastCGI 来源: https://blog.51cto.com/huanghongqiao/2748309