CGI under NGINX

Posted on March 5, 2010

First, the perl wrapper did not work for me. This one did:

wget http://www.nginx.eu/nginx-fcgi/nginx-fcgi.txt -O /usr/local/sbin/nginx-fcgi
chmod a+x /usr/local/sbin/nginx-fcgi

You will need an init script to run it. Unless you want to start it manually of course:

wget http://django.hosting4u.cz/static/resources/cgi -O /etc/init.d/cgi
chmod a+x /etc/init.d/cgi

Start it now. You may want it to start at boot too:

update-rc.d cgi defaults
/etc/init.d/cgi start

Next, configure your nginx. Pass the requests to the daemon started with the last command. Modify the following fragment to your needs and put it somewhere in your /etc/nginx/sites-available/default in the server section. I would like to run a single script here:

location /cgi-bin/awstats.pl {
    fastcgi_pass unix:/var/run/cgi/cgi.sock;
    include /etc/nginx/fastcgi_params;
    fastcgi_param PATH_INFO $fastcgi_script_name;
    fastcgi_param SCRIPT_FILENAME /usr/lib/cgi-bin/awstats.pl;
}

In this example above, the url /cgi-bin/awstats.pl will be served by the awstats.pl script. The script would read the request and its output will be fed back to the user.

Last, do not forget to reload nginx:

/etc/init.d/nginx restart