Askhat
Askhat

Reputation: 367

How to run CGI scripts on Nginx

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

Upvotes: 35

Views: 93130

Answers (8)

pjincz
pjincz

Reputation: 2122

I have been waiting for CGI support for 10 years without any luck. So finally I made a cgi plugin for nginx by myself.

https://github.com/pjincz/nginx-cgi

After installing of the plugin, just turn on cgi support by:

location /cgi-bin {
    cgi on;
}

Upvotes: 1

phil294
phil294

Reputation: 10822

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

Upvotes: 0

andrew-e
andrew-e

Reputation: 732

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.

Upvotes: 1

Brad Allen
Brad Allen

Reputation: 61

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.

Upvotes: 6

Burak Tamt&#252;rk
Burak Tamt&#252;rk

Reputation: 1239

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.

Upvotes: 19

Alton Yu
Alton Yu

Reputation: 61

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

Upvotes: 6

VBart
VBart

Reputation: 15110

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.

Upvotes: 13

David Farrell
David Farrell

Reputation: 457

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.

Upvotes: 27

Related Questions