一、安装
1 先安装2个包
spawn-fcgi-1.6.4.tar.gz
fcgi-2.4.1-SNAP-0910052249
安装 fcgi-2.4.1-SNAP-0910052249
报错:
fcgio.cpp: In destructor 'virtual fcgi_streambuf::~fcgi_streambuf()':
fcgio.cpp:50: error: 'EOF' was not declared in this scope
fcgio.cpp: In member function 'virtual int fcgi_streambuf::overflow(int)':
fcgio.cpp:70: error: 'EOF' was not declared in this scope
fcgio.cpp:75: error: 'EOF' was not declared in this scope
fcgio.cpp: In member function 'virtual int fcgi_streambuf::sync()':
fcgio.cpp:86: error: 'EOF' was not declared in this scope
fcgio.cpp:87: error: 'EOF' was not declared in this scope
fcgio.cpp: In member function 'virtual int fcgi_streambuf::underflow()':
fcgio.cpp:113: error: 'EOF' was not declared in this scope
make[2]: *** [fcgio.lo] Error 1
make[2]: Leaving directory `/root/downloads/fcgi-2.4.1-SNAP-0910052249/libfcgi'
make[1]: *** [all-recursive] Error 1
make[1]: Leaving directory `/root/downloads/fcgi-2.4.1-SNAP-0910052249'
make: *** [all] Error 2
解决办法:在/include/fcgio.h文件中加上 #include <cstdio>,然后再编译安装就通过了。
2 编写测试代码
1) mkdir test
vi demo.c
输入下列代码:
#include "fcgi_stdio.h"
#include <stdlib.h>
int main(void)
{
int count = 0;
while (FCGI_Accept() >= 0)
printf("Content-type: text/html\r\n"
"\r\n"
"<title>FastCGI Hello!</title>"
"<h1>FastCGI Hello!</h1>"
"Request number %d running on host <i>%s</i>\n",
++count, getenv("SERVER_NAME"));
return 0;
}
2) 编译
g++ demo.c -o demo.cgi -lfcgi
3) ldd demo.cgi
[wdm@localhost test]$ ldd demo.cgi
linux-vdso.so.1 => (0x00007fffc390b000)
libfcgi.so.0 => not found
libstdc++.so.6 => /usr/lib64/libstdc++.so.6 (0x00000035eae00000)
libm.so.6 => /lib64/libm.so.6 (0x00000035e5e00000)
libgcc_s.so.1 => /lib64/libgcc_s.so.1 (0x00000035ea200000)
libc.so.6 => /lib64/libc.so.6 (0x00000035e5600000)
/lib64/ld-linux-x86-64.so.2 (0x00000035e4e00000)
解决:
查找动态库位置
sudo find / -name 'libfcgi.so'
/usr/local/lib/libfcgi.so
/home/wdm/opensource/fcgi-2.4.1-SNAP-0910052249/libfcgi/.libs/libfcgi.so
配置动态库搜索路径
sudo vi /etc/ld.so.conf
include ld.so.conf.d/*.conf
/usr/local/lib/
/usr/lib64/
/usr/lib/
生效
sudo ldconfig -v
3 启动fcgi程序
[wdm@localhost test]$ spawn-fcgi -a 127.0.0.1 -p 8081 -f ./demo.cgi
spawn-fcgi: child spawned successfully: PID: 11819
4 nginx fastcgi配置
打开配置文件
sudo vi nginx.conf
配置内容
location = /50x.html {
root html;
}
location = /demo.cgi {
fastcgi_pass 127.0.0.1:8081;
fastcgi_index index.cgi;
include fastcgi.conf;
}
重启
sudo ./nginx -s reload
5) 测试
http://192.168.255.137/demo.cgi
FastCGI Hello!
Request number 1 running on host localhost
二、使用
1、fastCGI环境变量
CGI程序通过标准输入(STDIN)和标准输出(STDOUT)来进行输入输出。此外CGI程序还通过环境变量来得到输入,操作系统提供了许 多环境变量,它们定义了程序的执行环境,应用程序可以存取它们。Web服务器和CGI接口又另外设置了一些环境变量,用来向CGI程序传递一些重要的参 数。CGI的GET方法还通过环境变量QUERY-STRING向CGI程序传递Form中的数据。 下面是一些常用的CGI环境变量:
变量名 |
描述 |
CONTENT_TYPE
|
这个环境变量的值指示所传递来的信息的MIME类型。目前,环境变量CONTENT_TYPE一般都是:application/x-www-form-urlencoded,他表示数据来自于HTML表单。
|
CONTENT_LENGTH
|
如果服务器与CGI程序信息的传递方式是POST,这个环境变量即使从标准输入STDIN中可以读到的有效数据的字节数。这个环境变量在读取所输入的数据时必须使用。
|
HTTP_COOKIE
|
客户机内的 COOKIE 内容。
|
HTTP_USER_AGENT
|
提供包含了版本数或其他专有数据的客户浏览器信息。
|
PATH_INFO
|
这个环境变量的值表示紧接在CGI程序名之后的其他路径信息。它常常作为CGI程序的参数出现。
|
QUERY_STRING
|
如果服务器与CGI程序信息的传递方式是GET,这个环境变量的值即使所传递的信息。这个信息经跟在CGI程序名的后面,两者中间用一个问号'?'分隔。
|
REMOTE_ADDR
|
这个环境变量的值是发送请求的客户机的IP地址,例如上面的192.168.1.67。这个值总是存在的。而且它是Web客户机需要提供给Web服务器的唯一标识,可以在CGI程序中用它来区分不同的Web客户机。
|
REMOTE_HOST
|
这个环境变量的值包含发送CGI请求的客户机的主机名。如果不支持你想查询,则无需定义此环境变量。
|
REQUEST_METHOD
|
提供脚本被调用的方法。对于使用 HTTP/1.0 协议的脚本,仅 GET 和 POST 有意义。
|
SCRIPT_FILENAME
|
CGI脚本的完整路径
|
SCRIPT_NAME
|
CGI脚本的的名称
|
SERVER_NAME
|
这是你的 WEB 服务器的主机名、别名或IP地址。
|
SERVER_SOFTWARE
|
这个环境变量的值包含了调用CGI程序的HTTP服务器的名称和版本号。例如,上面的值为Apache/2.2.14(Unix)
|
2、fastCGI例子
FCGX_Stream *in, *out, *err;
FCGX_ParamArray evnp;
while(FCGX_Accept(&in, &out, &err, &evnp) >= 0)
{
char *query = FCGX_GetParam("QUERY_STRING", evnp); //获取请求
try
{
}
catch(exception &e)
{
TRACE(LOG_ERROR, "error: " << e.what());
return -1;
}
}
FCGX_FPrintF(out, "Content-type: text/json;charset=utf-8\r\n"
"Content-Length: %d\r\n"
"\r\n"
"%s", ... , ....);