How to run CGI scripts on Nginx
Asked Answered
G

7

34

I have problem setting up CGI scripts to be run on Nginx, so far I've found http://wiki.nginx.org/SimpleCGI this stuff but problem is that I can't make perl script run as service so that it will run in background and even in case of restart it will start running automatically

Do you have any idea? I'm running Centos 5

I've found some solutions here but I couldn't integrate code given there with this Perl script I'm completely zero at Perl, please help me Thanks

Gusman answered 26/7, 2012 at 10:36 Comment(1)
Since you're "zero at Perl" consider writing your CGI in C instead. Perl has some serious disadvantages like it needs to be compiled every time it's used and it's usually hard to understand unless you're a perl junkie. If your program is compiled C stdin comes from the browser, stdout goes to the browser, it's also tiny and efficient. I'll put in a plug for one of my own programs, look at jbe.c in it, that's what compiles into the CGI program jbe. sourceforge.net/projects/cgi-jukeboxDunton
D
13

Nginx is a web server. You need to use an application server for your task, such as uWSGI for example. It can talk with nginx using its native very effective binary interface called uwsgi.

Diagram answered 26/7, 2012 at 11:55 Comment(5)
Since the OP is tagged perl, PSGI/Plack (Perl's WSGI + Rack) is probably what's needed.Sharronsharyl
I'll be darned; just learned uWSGI includes PSGI support, so, nice!Sharronsharyl
Are you saying that Apache and Lighttpd as well as many other are not Web servers because they do support CGI? Using "application server" to run CGI-scripts is an overkill.Baber
Apache can be both: an application server and a web-server. It acts as an application server when it runs your web applications. Lighttpd doesn't run CGI itself. It communicates with application using FastCGI protocol, like nginx does.Diagram
Sorry, but no. You don't need an application server to run a CIG. A normal web server can run CGIsReider
N
25

Nginx doesn't have native CGI support (it supports fastCGI instead). The typical solution for this is to run your Perl script as a fastCGI process and edit the nginx config file to re-direct requests to the fastCGI process. This is quite a complex solution if all you want to do is run a CGI script.

Do you have to use nginx for this solution? If all you want to do is execute some Perl CGI scripts, consider using Apache or Lighttpd as they come with CGI modules which will process your CGI scripts natively and don't require the script to be run as a separate process. To do this you need install the web server and edit the web server config file to load the CGI module. For Lighttpd, you will need to add a line in the config file to enable processing of CGI files. Then put the CGI files into the cgi-bin folder.

Natatorium answered 26/7, 2012 at 11:22 Comment(4)
Thank you for your comment, yes I need to use Nginx because whole web-site is running under Nginx and now I have this(forkosh.com/mimetex.html) CGI script to run under web-site, web-site is Math test so I need support of Mimetex notation, but it is runned only as CGI... so far I've configured to FastCGI for PHP scripts, but it didn't work for simple CGI :( please help...Gusman
Although apache and lighttpd come with their own cgi module, the cgi scripts still run in a separate process --- as the web server's child process. Their cgi module only do the work like parsing URL, populating environment variables.Pleuropneumonia
Did you ever figure out how to run CGI scripts with NGINX? I have FASTCGI properly set up, but when I run a CGI script, all I see is the script's code.Treadmill
Thank you! This answer might not help OP but it did help me. By the way, do you have any idea why NGINX didn't even plan to support CGI at all? CGI seems to be a nice fallback for me.Gramarye
F
19

Install another web server(Apache, Lighttpd) that runs on different port. Then proxy your CGI request to the webserver with nginx.

You just need to add this to nginx configuration, after installed a web server on 8080

location /cgi-bin {
    proxy_pass http://127.0.0.1:8080;
}

Take a look at Nginx Location Directive Explained for more details.

Frascati answered 26/7, 2012 at 12:2 Comment(4)
should I create another instance of web-server on different port? I mean do i need install new instance of Nginx?? or it should be Apache one?Gusman
You should install apache, or lighthttpd on different port, then proxy it the cgi-bin folder via nginx.Brushwork
What is the benefit of doing it this way? Why not just run Apache by itself? What benefits does NGINX as the middle layer provide?Ideate
You might serving static files or fastcgi while serving some of cgi scripts. Nginx is serving static files and fastcgi related stuffs faster than apache does because of the eventness design of the nginx. Nginx does not execute cgi scripts by the design, since it has to be open a new thread or process for handling the cgi script.Brushwork
D
13

Nginx is a web server. You need to use an application server for your task, such as uWSGI for example. It can talk with nginx using its native very effective binary interface called uwsgi.

Diagram answered 26/7, 2012 at 11:55 Comment(5)
Since the OP is tagged perl, PSGI/Plack (Perl's WSGI + Rack) is probably what's needed.Sharronsharyl
I'll be darned; just learned uWSGI includes PSGI support, so, nice!Sharronsharyl
Are you saying that Apache and Lighttpd as well as many other are not Web servers because they do support CGI? Using "application server" to run CGI-scripts is an overkill.Baber
Apache can be both: an application server and a web-server. It acts as an application server when it runs your web applications. Lighttpd doesn't run CGI itself. It communicates with application using FastCGI protocol, like nginx does.Diagram
Sorry, but no. You don't need an application server to run a CIG. A normal web server can run CGIsReider
M
6

I found this hack using FastCGI to be a little nicer than running another web server. http://nginxlibrary.com/perl-fastcgi/

Murphree answered 8/11, 2017 at 18:28 Comment(0)
K
6

I found this: https://github.com/ruudud/cgi It says:

===

On Ubuntu: apt-get install nginx fcgiwrap
On Arch: pacman -S nginx fcgiwrap

Example Nginx config (Ubuntu: /etc/nginx/sites-enabled/default):

server {
    listen   80;
    server_name  localhost;
    access_log  /var/log/nginx/access.log;

    location / {
        root /srv/static;
        autoindex on;
        index index.html index.htm;
    }

    location ~ ^/cgi {
        root /srv/my_cgi_app;
        rewrite ^/cgi/(.*) /$1 break;

        include fastcgi_params;
        fastcgi_pass unix:/var/run/fcgiwrap.socket;
        fastcgi_param SCRIPT_FILENAME /srv/my_cgi_app$fastcgi_script_name;
    }
}

Change the root and fastcgi_param lines to a directory containing CGI scripts, e.g. the cgi-bin/ dir in this repository.

If you are a control freak and run fcgiwrap manually, be sure to change fastcgi_pass accordingly. The path listed in the example is the default in Ubuntu when using the out-of-the-box fcgiwrap setup.

===

I'm about to try it.

Kittrell answered 2/1, 2019 at 4:7 Comment(2)
Hi, Welcome, Maybe you should try and confirm the solution before posting the answer? :)Colombes
I think nowadays you can use include fastcgi.conf and remove the line fastcgi_param SCRIPT_FILENAME ...Meilen
L
1

Based on Brad's answer, simplified and modernized, with an example for CGI with Awstats:

location ~ ^/cgi-bin/awstats\.pl {

        root /usr/lib/cgi-bin;
        rewrite ^/cgi-bin/(.*) /$1 break;

        include fastcgi.conf;
        fastcgi_pass unix:/run/fcgiwrap.socket;
}

You'll need to install the fcgiwrap package.

Longdrawnout answered 27/6, 2023 at 2:40 Comment(0)
A
0

If you have a dedicated folder, the configuration becomes even simpler with fastcgi-wrap:

sudo apt-get install fcgiwrap
location /cgi-bin {
    gzip off;
    fastcgi_pass unix:/var/run/fcgiwrap.socket;
    include /etc/nginx/fastcgi_params;
}

and then for example

/var/www/html/cgi-bin/hello.sh
#!/bin/bash
echo -e "Content-type:text/html\n"
echo "<pre>"
env # Show available environment vars, including QUERY_STRING etc
Ahoufe answered 1/8, 2023 at 19:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.