CGI::Application
FastCGI を使用して Nginx 間で通信する Perl アプリケーションをデプロイしようとしています。
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
と、ポート 5001 にバインドされていることがわかりnetstat
ます。ブラウザで URL にアクセスすると、この非常にシンプルなアプリでも 502 が返されます。Carp
書き込み中のエラー ログには何もありません。
アプリケーションの起動時間が長いため、プレーン CGI (ラッパー スクリプト経由)を使用したくありません。また、アプリケーション全体を Plack/PSGI に変換することもできません。
CGI::Fast
ドキュメントの単純な例であっても、Nginx が Perl と通信しない理由をどのように理解できますか?