运行 NGINX 上的 CGI 脚本

Running CGI scripts on NGINX

我知道这个问题已经有人问过,但是没有关于那个问题 (How to run CGI scripts on Nginx) 的明确答案可以帮助我。就我而言,我已经使用源代码安装了 NGINX,并修复了我的 .config 文件,以便我可以使用 FASTCGI 成功读取 .php 文件。但是,在 运行ning CGI 脚本方面,我遇到了很多问题。我知道我已经安装并设置了 FAST CGI,所以我应该将这些 .cgi 文件命名为 .fcgi 吗?或者我应该以某种方式包含 .cgi 文件以知道它正在使用 FAST CGI?我试着用 nginf.conf 文件来包含 .fcgi,它现在看起来像这样:

worker_processes  2;

pid        logs/nginx.pid;
error_log syslog:server=unix:/dev/log,facility=local7,tag=nginx,severity=error;

events {
worker_connections  1024;
}


http {
include       mime.types;
default_type  application/octet-stream;

access_log syslog:server=unix:/dev/log,facility=local7,tag=nginx,severity=info combined;

sendfile        on;
keepalive_timeout  65;


server {
    listen       80;
    server_name  localhost;
        root   /home/parallels/Downloads/user_name/nginx/html;
    location / {

 index index.html index.htm new.html;
        autoindex on;
    }

location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass 127.0.0.1:9000;
 #fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;
fastcgi_param  HTTPS              off;
include fastcgi_params;
fastcgi_buffer_size 16k;
fastcgi_buffers 4 16k;
}
location ~ \.pl|fcgi$ {
  try_files $uri =404;
  gzip off;
  fastcgi_pass  127.0.0.1:9000;
  fastcgi_index index.pl;
  #fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
  include fastcgi_params;
  } 

    #error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   html;
    }
}
}

但是,每当我 运行 一个 .fcgi 脚本,例如

#!/usr/bin/perl

print "Content-type: text/html\n\n";
print "<html><body>Hello, world.</body></html>";

迎接我的是这样一个屏幕:

我很确定这不正常;我应该只在屏幕上看到 Hello, world.,而不是所有代码。如果我的想法实际上是错误的,请告诉我这应该是正确的输出。

此外,附带说明一下,如果我将此作为我的 files.fcgi 文件:

#!/usr/bin/perl
my $output = `ls`;
print $output

运行 类似这样的东西 returns .fcgi 文件所在目录中所有文件的列表。无论如何我可以在网络浏览器上显示它吗?查看在线示例,似乎人们已经能够在他们的浏览器上 运行 file.fcgi 并查看 shell 命令的输出(这让我相信我正在做某事错了,因为当我 运行 它在命令行上列出所有文件但在浏览器上时,它只打印出我的代码)。假设我做错了什么,有谁知道我可能做错了什么。如果您需要更多信息,请告诉我!

谢谢,祝你有个愉快的一天!

nginx 不支持 CGI 脚本,并且无法自行启动 FastCGI 脚本 — 它只能连接到已经 运行ning 的 FastCGI 进程。

如果您想要 运行 CGI 脚本,请使用支持它们的 Web 服务器,例如 Apache。虽然有一些解决方法,但在这个阶段它们只会让您感到困惑。

搜索 "fastcgi wrapper" 以查找旨在弥补 "modern" 不喜欢生成进程来处理请求的网络服务器与传统 CGI 程序之间差距的各种程序。

[nginx       ]    one socket     [wrapper     ]  ,-- forks a single subprocess
[runs        ] == connection ==> [also runs   ] ---- running your CGI program
[continuously]    per request    [continuously]  `-- for each request

虽然标准 CGI API 是 "for each request, the server calls your program with env vars to describe the request and with the body (if any) on stdin and your program is expected to emit a response on stdout and exit",但 fcgi API 期望您的程序始终 运行 并处理通过套接字传递给它的请求 --这样,它真的更像是一个服务器。参见 http://en.wikipedia.org/wiki/FastCGI