trynacode
trynacode

Reputation: 371

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. 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!

Upvotes: 0

Views: 4074

Answers (2)

Rob Starling
Rob Starling

Reputation: 3908

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

Upvotes: 1

user149341
user149341

Reputation:

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.

Upvotes: 2

Related Questions