简体   繁体   English

在NGINX上运行CGI脚本

[英]Running CGI scripts on NGINX

I know this question has already been asked, but there were no clear answers on that question ( How to run CGI scripts on Nginx ) that would help me. 我知道已经问过这个问题,但是没有一个明确的答案( 如何在Nginx上运行CGI脚本 )对我有帮助。 In my case, I have installed NGINX by using source code, and have fixed my .config file so that I can read .php files using FASTCGI successfully. 就我而言,我已经使用源代码安装了NGINX,并修复了.config文件,以便可以使用FASTCGI成功读取.php文件。 However, I am having quite some issues when it comes to running CGI scripts. 但是,在运行CGI脚本时,我遇到了很多问题。 I know I have FAST CGI installed and set up, so am I supposed to be naming these .cgi files .fcgi instead? 我知道我已经安装并设置了FAST CGI,所以我应该将这些.cgi文件命名为.fcgi吗? Or am I supposed to include someway for the .cgi file to know that it is working with FAST CGI?? 还是我应该为.cgi文件添加某种方式,以使其知道它可以与FAST CGI一起使用? I tried toying around with the nginf.conf file to include .fcgi, and it looks something like this right now: 我试图绕开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;
    }
}
}

However, whenever I run a .fcgi script such as 但是,每当我运行.fcgi脚本(例如

#!/usr/bin/perl

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

I am greeted with a screen that looks like this: 我看到一个看起来像这样的屏幕:

在此处输入图片说明

I'm pretty sure this is not normal; 我敢肯定这是不正常的。 I should just be seeing Hello, world. 我应该只是看到Hello, world. on my screen, not all the code as well. 在我的屏幕上,不是所有的代码也是如此。 Please let me know if my thinking that is actually wrong and this is supposed to be the correct output. 请让我知道我的想法是否确实错误,这应该是正确的输出。

Additionally, on a side note, if I had this as my files.fcgi file: 此外,如果我将其作为files.fcgi文件,请注意:

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

Running something like this returns a list of all files in the directory that the .fcgi file is located in. Is there anyway I could display this on the web browser? 运行类似的操作会返回.fcgi文件所在目录中所有文件的列表。无论如何,我可以在Web浏览器上显示此文件吗? Looking at examples online, it seems like people have been able to just run file.fcgi on their browser and see the output of the shell command (which led me to believe I'm doing something wrong, because when I run it on the command line it lists all the files but on the browser, it just prints out my code). 在线查看示例,似乎人们已经能够在他们的浏览器上运行file.fcgi并查看shell命令的输出(这使我相信我做错了,因为当我在命令上运行它时,行中列出了所有文件,但是在浏览器上,它只是打印出我的代码)。 Does anyone know what I could possibly doing wrong, assuming I am doing something wrong. 假设我做错了什么,谁知道我可能做错了什么。 If you need any more information, please let me know! 如果您需要更多信息,请告诉我!

Thank you, and have a good day! 谢谢你,有一个美好的一天!

nginx does not support CGI scripts, and cannot launch FastCGI scripts on its own — it can only connect to FastCGI processes that are already running. nginx不支持CGI脚本,也不能单独启动FastCGI脚本-它只能连接到已经在运行的FastCGI进程。

If you want to run CGI scripts, use a web server that supports them, such as Apache. 如果要运行CGI脚本,请使用支持它们的Web服务器,例如Apache。 While there are some workarounds, they will just confuse you at this stage. 尽管有一些解决方法,但在此阶段它们只会使您感到困惑。

Search for "fastcgi wrapper" to find various programs designed to bridge the gap between "modern" webservers that don't like spawning processes to handle requests and traditional CGI programs. 搜索“ fastcgi包装器”以找到各种程序,这些程序旨在弥合不喜欢产生请求处理过程的“现代” Web服务器与传统CGI程序之间的鸿沟。

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

While the standard CGI API is "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", the fcgi API expects your program to be constantly running and handle requests handed to it on a socket -- in that way, it's really more like a server. 标准CGI API是“针对每个请求,服务器使用env vars调用您的程序来描述该请求,并使用stdin上的主体(如果有)调用您的程序,并且您的程序应在stdout并退出时发出响应”,fcgi API希望您的程序能够持续运行并处理套接字上传递给它的请求-这样,它实际上更像是服务器。 See http://en.wikipedia.org/wiki/FastCGI 参见http://en.wikipedia.org/wiki/FastCGI

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM