我正在尝试部署一个使用CGI::Application
到Nginx的Perl应用程序,使用FastCGI在它们之间进行通信.@H_502_1@
Nginx不断返回“502 Bad Gateway”,错误日志填写如下:@H_502_1@
@H_502_1@
2015/02/03 12:40:03 [error] 11209#0: *2 upstream prematurely closed connection while reading response header from upstream, client: 10.1.1.23, server: www.example.com, request: “GET /test.fcgi HTTP/1.1”, upstream: “07001”, host: “www.example.com”@H_502_1@
@H_502_1@
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;
}
}
为了查看问题是否与Perl应用程序本身有关,我根据CGI::Fast
documentation中的测试代码创建了一个新的fcgi应用程序:@H_502_1@
@H_502_1@
#!/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写入的错误日志中没有任何内容.@H_502_1@
我不想使用普通的CGI(via a wrapper script),因为应用程序的启动时间很长,我无法将整个应用程序转换为Plack / Psgi.@H_502_1@
我怎么能弄清楚为什么Nginx不会与Perl CGI :: Fast交谈,甚至是文档中的简单示例?@H_502_1@
您正在运行FastCGI服务器,但您告诉Nginx连接到HTTP服务器.这些不是相同的协议.@H_502_1@
要从Nginx连接到FastCGI服务器,请使用ngx_http_fastcgi_module
,而不是ngx_http_proxy_module,例如:@H_502_1@
@H_502_1@
fastcgi_pass 127.0.0.1:5001;
请注意,proxy_set_header模块在此处不适用,因为FastCGI使用CGI环境变量,而不是标头.如果需要设置额外的标题,可以使用fastcgi_param指令,例如@H_502_1@
@H_502_1@
fastcgi_param ENVIRONMENT_VARIABLE $value;
Nginx在一个文件中附带了一些标准的fastcgi_param指令,您可能只想导入批发:@H_502_1@
@H_502_1@
include fastcgi_params;
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。