接上幾節, 本節寫有請求和響應的demo.html
一. myecho.cpp以下:python
#include <stdlib.h> #include <unistd.h> #include "fcgio.h" #include "fcgi_config.h" using namespace std; int main (void) { int count = 0; long pid = getpid(); 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" "<TITLE>myecho</TITLE>\n" "<H1>myecho</H1>\n" "<H4>PID: " << pid << "</H4>\n" "<H4>Request Number: " << ++count << "</H4>\n"; } cin.rdbuf(cin_streambuf); cout.rdbuf(cout_streambuf); cerr.rdbuf(cerr_streambuf); return 0; }
二. 編譯+運行測試.nginx
#g++ -o myecho myecho.cpp -I /usr/local/include -L /usr/local/lib -lfcgi -lstdc++ -lfcgi++c++
#./myecho瀏覽器
若編譯或運行時出錯(通常是庫問題), 參考上幾節解決辦法.測試
三. 將cgi拷貝到ngix(目標路徑沒有則手動建立)spa
cp ***/myecho /usr/local/nginx/cgibincode
四. 啓動Spawn-fcgi.htm
/usr/local/nginx/sbin/spawn-fcgi -a 127.0.0.1 -p 8088 -f /usr/local/nginx/cgibin/myechoblog
五. 訪問.
注: python index.py已經運行的狀況下.
瀏覽器: localhost/myecho.cgi
注: fastcgi的example下有許多例子能夠參考.