Perl web serving with nginx and FastCGI - not able to read parameters

空扰寡人 提交于 2019-12-10 20:41:58

问题


There are any number of tutorials out there on how to use FastCGI to CGI wrappers to serve Perl code using nginx. But I'm comfortable working with Perl modules myself, so I don't need the wrapper. I'm trying to figure out the right way to set this up. Here's the code I have so far:

#!perl

use CGI;
use FCGI;

my $s = FCGI::OpenSocket(':9000',20);
my $r = FCGI::Request( \*STDIN, \*STDOUT, \*STDERR,
    \%ENV, $s);
while ($r->Accept >= 0) {
    my $cgi = CGI->new;
    print "Content-type: text/html\n\n";
    print "<html><body>The foo input is ", $cgi->param('foo'), "</body></html>";
    $r->Finish;
}

And enable it in nginx like so:

location /foo {
    include /etc/nginx/fastcgi_params;
    fastcgi_pass  127.0.0.1:9000;
    fastcgi_index index.pl;
}

The problem is that no matter how many times I call the script, param returns the same value that was passed the very first time it was called since starting the program. Is there a better way of doing this? I'm open to alternatives to CGI.pm as well.


回答1:


CGI::Fast will handle most of the work for you, including setting up the daemon.

use CGI::Fast;

local $ENV{FCGI_SOCKET_PATH} = ":9000";
local $ENV{FCGI_LISTEN_QUEUE} = 20;

while ($q = CGI::Fast->new) {
    print $q->header;
    print "<html><body>The foo input is ", $cgi->param('foo'), "</body></html>";
}

An alternative is Nginx::Simple which gives you more control over the behavior of your cgi-script-as-daemon.



来源:https://stackoverflow.com/questions/7206323/perl-web-serving-with-nginx-and-fastcgi-not-able-to-read-parameters

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!