Nginx Library

A repository of Nginx-related articles

Running CGI scripts using thttpd

Nginx cannot run CGI scripts natively. You have to either use the Perl-FastCGI daemon to run your Perl scripts, or for a more “native” CGI environment, proxy the CGI scripts to a web-server that has the ability to execute them natively. Apache, surely, will be overkill for this purpose. Keeping in mind the low-memory requirements of most users who run Nginx, we’ll use thttpd. thttpd is a tiny, lightweight server (only around 800 kB RSS) which has the ability to execute CGI scripts natively.

First, let us install thttpd. thttpd hasn’t been in active development since late 2003 (v2.25b), but there isn’t any security issue with it. The only issue is that it doesn’t respect (or pass on) the X-Forwarded-For header. So, if you are using Debian 5.0.X Lenny, please build from source after applying this patch or install from the Squeeze repositories using apt-pinning (This issue was fixed in v2.25b-10) .

Assuming you are using Debian Squeeze,

apt-get install thttpd

Replace the thttpd configuration file with our own :

mv /etc/thttpd/thttpd.conf /etc/thttpd/thttpd.conf.orig
vi /etc/thttpd/thttpd.conf

Put this in the file :

dir=/var/www  #change this to the $document_root from nginx config file

Now let us start thttpd.

invoke-rc.d thttpd start

We also need to configure nginx to pass on the requests for CGI scripts to thttpd. Add this to your configuration file :

location ~ \.cgi|pl$ {
     include proxy.conf;

We also need to create a file with some configuration parameters for nginx :

vi /etc/nginx/proxy.conf

Put this in the file :

proxy_redirect          off;
proxy_set_header        Host            $host;
proxy_set_header        X-Real-IP       $remote_addr;
proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
client_max_body_size    10m;
client_body_buffer_size 128k;
proxy_connect_timeout   90;
proxy_send_timeout      90;
proxy_read_timeout      90;
proxy_buffers           32 4k;

Now let us reload the Nginx configuration :

nginx -s reload

CGI scripts should be now transparently proxied to and executed by thttpd running on port 8000.

If you find this article helpful, please consider making a donation.