fastcgi c/c++ API 说明
fastcgi c/c++ API 下载地址:https://github.com/FastCGI-Archives
先上example
#include <iostream> #include "fcgio.h" using namespace std; int main(void) { // Backup the stdio streambufs streambuf * cin_streambuf = cin.rdbuf(); streambuf * cout_streambuf = cout.rdbuf(); streambuf * cerr_streambuf = cerr.rdbuf(); FCGX_Request request; FCGX_Init(); FCGX_InitRequest(&request, 0, 0); while (FCGX_Accept_r(&request) == 0) { fcgi_streambuf cin_fcgi_streambuf(request.in); fcgi_streambuf cout_fcgi_streambuf(request.out); fcgi_streambuf cerr_fcgi_streambuf(request.err); cin.rdbuf(&cin_fcgi_streambuf); cout.rdbuf(&cout_fcgi_streambuf); cerr.rdbuf(&cerr_fcgi_streambuf); cout << "Content-type: text/html\r\n" << "\r\n" << "<html>\n" << " <head>\n" << " <title>Hello, World!</title>\n" << " </head>\n" << " <body>\n" << " <h1>Hello, World!</h1>\n" << " </body>\n" << "</html>\n"; // Note: the fcgi_streambuf destructor will auto flush } // restore stdio streambufs cin.rdbuf(cin_streambuf); cout.rdbuf(cout_streambuf); cerr.rdbuf(cerr_streambuf); return 0; }
1.首先 initialize the FCGX library with FCGX_Init()
和initialize FCGX_Request with int FCGX_InitRequest(FCGX_Request *request, int sock, int flags);
int FCGX_InitRequest(FCGX_Request *request, int sock, int flags);
参数:
request是FCGX_Request指针
sock is a file descriptor returned by FCGX_OpenSocket() or 0 (default)
flags是 当指定sock时设为FCGI_FAIL_ON_INTR,默认0
返回值:成功返回0
2.FCGX_Accept_r阻塞接收循环接收fcgi request,_r版本是FCGX_Accept多线程安全版本
int FCGX_Accept_r(FCGX_Request *request);
参数: request FCGX_Request 指针
返回值:成功返回0
3.FCGX_Request结构体
typedef struct FCGX_Request { int requestId; /* valid if isBeginProcessed */ int role; FCGX_Stream *in; FCGX_Stream *out; FCGX_Stream *err; char **envp; /* Don't use anything below here */ struct Params *paramsPtr; int ipcFd; /* < 0 means no connection */ int isBeginProcessed; /* FCGI_BEGIN_REQUEST seen */ int keepConnection; /* don't close ipcFd at end of request */ int appStatus; int nWriters; /* number of open writers (0..2) */ int flags; int listen_sock; int detached; } FCGX_Request;
在FCGX_Accept之前,为FCGX_Request分别给in,out和err分配streambuf
二、Nginx 配置
events { worker_connections 1024; } http { server { listen 80; server_name localhost; location / { fastcgi_pass 127.0.0.1:8000; fastcgi_param GATEWAY_INTERFACE CGI/1.1; fastcgi_param SERVER_SOFTWARE nginx; fastcgi_param QUERY_STRING $query_string; fastcgi_param REQUEST_METHOD $request_method; fastcgi_param CONTENT_TYPE $content_type; fastcgi_param CONTENT_LENGTH $content_length; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_param SCRIPT_NAME $fastcgi_script_name; fastcgi_param REQUEST_URI $request_uri; fastcgi_param DOCUMENT_URI $document_uri; fastcgi_param DOCUMENT_ROOT $document_root; fastcgi_param SERVER_PROTOCOL $server_protocol; fastcgi_param REMOTE_ADDR $remote_addr; fastcgi_param REMOTE_PORT $remote_port; fastcgi_param SERVER_ADDR $server_addr; fastcgi_param SERVER_PORT $server_port; fastcgi_param SERVER_NAME $server_name; } } }
Nginx 在80端口监听http服务,把请求发送给 在端口8000监听的fastcgi进程
三、运行代码
# run nginx using the provided configuration sudo nginx -c <path to nginx.conf> # compile hello_world g++ main_v1.cpp -lfcgi++ -lfcgi -o hello_world # spawn the fcgi app on port 8000 with no fork spawn-fcgi -p 8000 -n hello_world
或
spawn-fcgi -a 127.0.0.1 -p 8088 -f hello_world
1.先启动nginx 服务
2.编译fastcgi程序,注意加链接库 fcgi++和fcgi
3.运行spwan-fcgi 指定可执行程序