ScaleScaleScaleScale

Tips / Nginx


How to run CGI scripts using Thttpd + Nginx

Days ago we wrote a guide about How to serve CGI scripts using Nginx, in that tutorial we explored how Nginx doesn’t fully support CGI parsing natively, however installing a FastCGI wrapper can make Nginx able to parse CGI scripts. Today we will learn an alternative method to Run CGI scripts with thttpd + Nginx.

What’s thttpd?

thttpd it’s an old and a super lightweight web server that has a very special feature: it can run CGI scripts natively. You probably haven’t heard that name before… and that’s why it’s not a so popular web server like Apache, IIS, or Nginx, however, thttpd has been there since a while and is one of the best solutions to run CGI scripts with minimal footprint.

Configuring a Nginx proxy for CGI requests

We will create a proxy with Nginx to serve the scripts via thttpd, assuming you are running CentOS/RHEL systems. In this case we will need to have EPEL Repo working, make sure you follow this tutorial to install it: Installing EPEL Repository on CentOS

Install thttpd

yum install thttpd
chkconfig thttpd on

Edit thttpd configuration file

nano -w /etc/thttpd.conf

Make sure the file contains this configuration:

host=127.0.0.1
port=81
dir=/var/www/yoursite.com
user=thttpd
cgipat=**.cgi|**.pl
logfile=/var/log/thttpd.log
pidfile=/var/run/thttpd.pid

In this case we defined “dir=/var/www/yoursite.com” as the root directory for the CGI scripts, we have also set the default thttpd port to 81 and defined the listen address to be localhost, tweak as you need.

Save the file and start thttpd.

service thttpd restart

Nginx Proxy configuration to pass on the requests for CGI scripts to thttpd

Add this configuration to your Nginx configuration for the site you want to serve the CGI scripts, example:

nano -w /etc/nginx/conf.d/yoursite.com.conf

Then add this block inside:

location ~ .cgi|pl$ {
proxy_pass   http://127.0.0.1:81;

# Other proxy configurations
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;
}

Full virtual host configuration would look similar to this:

### yoursite.com
server {
log_not_found off;
access_log logs/yoursite.com-access_log;
error_log  logs/yoursite.com-error_log crit;

        listen       80;
        server_name  yoursite.com www.yoursite.com;
        root   /var/www/yoursite.com;
       index  index.php index.cgi index.pl index.html index.htm;

# cgi proxy over thttpd
location ~ .cgi|pl$ {
proxy_pass   http://127.0.0.1:81;

# Other proxy configurations
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;
}

# php-fpm configuration
        location ~ .php$ {
        try_files $uri =404;
        fastcgi_pass   unix:/tmp/php5-fpm.sock;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
        fastcgi_buffer_size 128k;
        fastcgi_buffers 256 4k;
        fastcgi_busy_buffers_size 256k;
        fastcgi_temp_file_write_size 256k;
        }
}

Reload Nginx configuration to apply changes:

service nginx reload

Test if thttpd is serving the CGI scripts

We will create a simple CGI script with perl:

nano -w /var/www/yoursite.com/test.cgi

Then paste this inside:


#!/usr/bin/perl

print "Content-type: text/htmlnn";
print "nn";
print "
n"; print "If you see this, then Perl is running on the thttpd backend"; print "

NginxTips.com

"; print "n
n"; print "nn";

Conclusion

As we’ve seen, CGI support is definetly one of the big lacks of current Nginx versions. However, having CGI scripts running on Nginx is not difficult at all, in fact even if you use a FastCGI wrapper or thttpd the result will be the same. At NginxTips We preffer to run CGI scripts with a FastCGI wrapper, however you can also use thttpd if you like as we did today on this tutorial.

Popular search terms:

  • fastcgi thttpd
  • thttpd cgi example
  • thttpd tutorial
  • nginx cgi minimal
profile

Esteban Borges

Linux Geek, Webperf Addict, Nginx Fan. CTO @Infranetworking