0

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. 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. However, I am having quite some issues when it comes to running CGI scripts. I know I have FAST CGI installed and set up, so am I supposed to be naming these .cgi files .fcgi instead? Or am I supposed to include someway for the .cgi file to know that it is working with FAST CGI?? I tried toying around with the nginf.conf file to include .fcgi, and it looks something like this right now:

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

#!/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:

enter image description here

I'm pretty sure this is not normal; I should just be seeing 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:

#!/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? 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). 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!

Community
  • 1
  • 1
trynacode
  • 361
  • 1
  • 8
  • 18
  • Just making the suffix .fcgi doesn't make that example into a FastCGI program; it's still a traditional CGI program – Rob Starling Jun 29 '16 at 05:57
  • Is there something that I have to include in the FastCGI program? Like `use fast:cgi` or something like that? – trynacode Jun 29 '16 at 15:05
  • unfortunately, it's nowhere near that simple last time i looked into it -- 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. See https://en.wikipedia.org/wiki/FastCGI – Rob Starling Jun 30 '16 at 02:27

2 Answers2

2

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.

If you want to run CGI scripts, use a web server that supports them, such as Apache. While there are some workarounds, they will just confuse you at this stage.

  • Could you go into further details regarding "it can only connect to FastCGI processes that are already running". I know I could just use a different server but I was told to use NGINX so I'd like to at least try to run CGI scripts on it. – trynacode Jun 29 '16 at 15:04
  • Whoever told you that was giving you bad advice. Again: nginx cannot run CGI scripts. –  Jun 29 '16 at 15:27
  • Sorry, FCGI* scripts. It's able to run those from what I've understood, correct? – trynacode Jun 29 '16 at 15:28
  • Or if you have any useful links I could follow that explains how to set up FCGI scripts, that'd be great also. Haven't found anything too helpful after looking into it for a couple days. – trynacode Jun 29 '16 at 15:29
  • @trynacode i added an answer with more info. maybe that'll help. – Rob Starling Jul 01 '16 at 01:11
1

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.

[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. See http://en.wikipedia.org/wiki/FastCGI

Rob Starling
  • 3,868
  • 3
  • 23
  • 40