4

我正在尝试部署一个CGI::Application通过 Nginx 使用的 Perl 应用程序,使用 FastCGI 在它们之间进行通信。

Nginx 不断返回“502 Bad Gateway”,错误日志充满了这个:

2015/02/03 12:40:03 [错误] 11209#0: *2 上游过早关闭连接,同时从上游读取响应标头,客户端:10.1.1.23,服务器:www.example.com,请求:“GET /test .fcgi HTTP/1.1”,上游:“ http://127.0.0.1:5001/test.fcgi ”,主机:“www.example.com”

这是 Nginx 站点配置:

upstream @perl {
#   I had been trying to use a socket, but I switched to TCP to try WireShark.
#   server unix:/var/run/nginx/my-app.sock;
    server 127.0.0.1:5001;
}

server {
    listen       80;
    listen       443 ssl;

    server_name www.example.com;
    root /home/example/sites/www.example.com;

    location ~* \.fcgi(/|$) {

        fastcgi_split_path_info ^(.+?\.cgi)(/.*)$;
        # (I know that the `if` is old-style, and that `try_files` is better, but that shouldn't affect whether it works or not.)
        if (!-f $document_root$fastcgi_script_name) {
                return 404;
        }

        proxy_set_header Host $http_host;
        proxy_set_header X-Forwarded-Host $http_host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Port 443;
        proxy_pass http://@perl;
    }
}

CGI::Fast为了查看问题是否出在 Perl 应用程序本身,我根据文档中的测试代码创建了一个新的 fcgi 应用程序:

#!/usr/bin/perl -wT

use CGI::Fast;

# Added this to see if there are errors.    
# See http://perldoc.perl.org/CGI/Carp.html
BEGIN {
    use CGI::Carp qw(carpout);
    open(LOG, ">>/home/example/sites/www.example.com/err.log") or
      die("Unable to open mycgi-log: $!\n");
    carpout(LOG);
}

local $count = 0;

$ENV{FCGI_SOCKET_PATH} = "127.0.0.1:5001";
$ENV{FCGI_LISTEN_QUEUE} = 100;

while ( my $q = new CGI::Fast ) {
    $count++;
    print $q->header( "text/plain" ),
          "You are request number $count. Have a good day!\n";
}

当我运行时./test.fcgi,我可以看到netstat它已绑定到端口 5001。当我在浏览器中访问 URL 时,使用这个超级简单的应用程序仍然得到 502。错误日志中没有Carp正在写入的内容。

由于应用程序的启动时间长,我不想使用纯 CGI(通过包装脚本),而且我无法将整个应用程序转换为 Plack/PSGI。

我如何才能弄清楚为什么 Nginx 不会与 Perl 对话CGI::Fast,甚至不会与文档中的简单示例对话?

4

1 回答 1

6

你正在运行一个 FastCGI 服务器,但你告诉 Nginx 连接到一个 HTTP 服务器。这些不是相同的协议。

要从 Nginx 连接到 FastCGI 服务器,请使用ngx_http_fastcgi_module, not ngx_http_proxy_module,例如:

fastcgi_pass 127.0.0.1:5001;

请注意,该proxy_set_header模块不适用于此处,因为 FastCGI 使用 CGI 环境变量,而不是标头。如果您需要设置额外的标头,您可以使用该fastcgi_param指令来设置,例如

fastcgi_param ENVIRONMENT_VARIABLE $value;

Nginx 在一个文件中附带了一些标准fastcgi_param指令,您可能只想批量导入:

include fastcgi_params;
于 2015-02-20T19:24:25.637 回答