程序帮助信息

$ fcgiwrap -h
Usage: fcgiwrap [OPTION]
Invokes CGI scripts as FCGI.

fcgiwrap version 1.1.0

Options are:
  -f            Send CGI's stderr over FastCGI
  -c <number>       Number of processes to prefork
  -s <socket_url>   Socket to bind to (say -s help for help)
  -h            Show this help message and exit

Report bugs to Grzegorz Nosek <root@localdomain.pl>.
fcgiwrap home page: <http://nginx.localdomain.pl/wiki/FcgiWrap>

启动程序

$ fcgiwrap -f -s unix:/home/cai/opt/fcigwrap/var/fcgiwrap.socket &

查看程序运行状态

$ ps -aux | grep fcgiwrap

如果程序运行了,可以看到运行进程号

编写cgi程序

我们用一个简单的脚本程序hello.sh

#!/bin/bash

echo -ne 'Status: 200\r\n'
echo -ne 'Content-Type: text/plain\r\n'
echo -ne '\r\n'

echo 'PATH_INFO: ' $PATH_INFO
echo 'QUERY_STRING: ' $QUERY_STRING

exit 0

设置nginx配置

location / {
    fastcgi_param       SCRIPT_FILENAME     "/home/cai/opt/bin/hello.sh";
    fastcgi_param       PATH_INFO           $uri;
    fastcgi_param       QUERY_STRING        $args;
    fastcgi_param       HTTP_HOST           $server_name;
    fastcgi_pass        unix:/var/run/fcgiwrap.socket;
    include             fastcgi_params;     # 必须放在最后
}

访问程序

curl http://localhost/test/?name=cai
PATH_INFO:  /test
QUERY_STRING:  name=cai

spawn-fcgi 与 fcgiwra共同提供服务

  • 通过spawn-fcgi启动fcgiwrap
$ /spawn-fcgi -f /usr/local/bin/fcgiwrap -p 8081
  • 配置nginx的fcgiwrap参数
location ~ .*\.cgi$ {
    root cgi-bin;
    fastcgi_pass 127.0.0.1:8081;
    fastcgi_index index.cgi;
    include fastcgi.conf;
}
  • 编写cgi程序
#include <stdio.h>

#include <stdlib.h>



int main(void)

{

    int count = 0;

    printf("Content-type: text/html\r\n"

        "\r\n"

        "<title>CGI Hello!</title>"



        "<h1>CGI Hello!</h1>"

        "Request number %d running on host <i>%s</i>\n",

        ++count, getenv("SERVER_NAME"));

    return 0;

}
  • 编译cgi程序
$ g++ cgi.cpp -o demo.cgi -lfcgi
  • 将demo.cgi放入/opt/nginx/cgi-bin/
cp demo.cgi /opt/nginx/cgi-bin
  • 访问程序

v2-5f378ba4cf78c01d47c2f098e2853da0_720w.jpg

CGI Hello!
Request number 1 running on host localhost