FastCGI with nginx and fcgiwrap on FreeBSD

I wanted nginx to serve CGI scripts from /usr/local/www/cgi-bin. Unlike Apache, nginx supports only FastCGI. Thus, you have to use fcgiwrap.


  1. Install nginx:
    # make -C /usr/ports/www/nginx install clean
  2. Install fcgiwrap:
    # make -C /usr/ports/www/fcgiwrap install clean
  3. Create /usr/local/www/cgi-bin/:
    # mkdir /usr/local/www/cgi-bin
  4. Create a trivial cgi script /usr/local/www/cgi-bin/ which prints the current date and time:
    echo "Content-Type: text/plain"
  5. Make that script executable:
    # chmod 0755 /usr/local/www/cgi-bin/
  6. Configure nginx to forward any requests to /cgi-bin/ to fcgiwrap, and serve those requests from /usr/local/www/cgi-bin. Edit /usr/local/etc/nginx/nginx.conf and insert this after the default location / block:
    http {
        server {
            location / {
            location /cgi-bin {
                root          /usr/local/www;
                include       fastcgi_params;
                fastcgi_pass  unix:/var/run/fcgiwrap/fcgiwrap.sock;
  7. Enable both nginx and fcgiwrap, and also make the unix socket on which fcgiwrap listens available to nginx:
    # sysrc nginx_enable=YES
    # sysrc fcgiwrap_enable=YES
    # sysrc fcgiwrap_socket_group=www
  8. Optionally, redirect any errors from the CGI scripts to the web server's error logfile:
    # sysrc fcgiwrap_flags=-f
  9. Start nginx and fcgiwrap:
    # service fcgiwrap start
    # service nginx start
  10. Point your browser to http://IP_ADDRESS_OF_YOUR_WEBSERVER/cgi-bin/