FastCGI介绍:
FastCGI像是一个常驻(long-live)型的CGI,它可以一直执行着,只要激活后,不会每次都要花费时间去fork一次(这是CGI最为人诟病的fork-and-execute 模式)。它还支持分布式的运算, 即 FastCGI 程序可以在网站服务器以外的主机上执行并且接受来自其它网站服务器来的请求。 FastCGI是语言无关的、可伸缩架构的CGI开放扩展,其主要行为是将CGI解释器进程保持在内存中并因此获得较高的性能。众所周知,CGI解释器的反复加载是CGI性能低下的主要原因,如果CGI解释器保持在内存中并接受FastCGI进程管理器调度,则可以提供良好的性能、伸缩性、Fail- Over特性等等。
配置环境介绍:
1、服务器:nginx 2、系统版本:腾讯云centos7.0 3、两个插件:spawn_fastcgi和fastcgi 4、开发语言:C/C++
原理介绍
主要内容介绍
1、nginx配置文件: location ~\.fcgi$ { fastcgi_pass 127.0.0.1:8000; include fastcgi.conf; include fastcgi_params; } 2、程序的编译运行: gcc demo.c -o demo.fcgi -lfcgi sudo cp demo.fcgi /usr/local/nginx/fastcgi_temp/ -f sudo spawn-fcgi -f /usr/local/nginx/fastcgi_temp/demo.fcgi -a 127.0.0.1 -p 8000
测试用例
/************************************************************************* > File Name: demo.c > Author: vividwei > Mail: 353470469@qq.com > Created Time: 2016年12月03日 星期六 19时26分44秒 ************************************************************************/ #include <string.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <fcgi_stdio.h> //#include "url_decode.h" int main() { char post_str[1024]; char post_res[1024]; int len = 0; int index = 0; while (FCGI_Accept() >= 0) { ┊ len = atoi(getenv("CONTENT_LENGTH")); ┊ memset(post_str, 0x0, sizeof(post_str)); ┊ memset(post_res, 0x0, sizeof(post_res)); ┊ //read(0, post_str, len); ┊ for (index = 0; index < len; ++index) ┊ { ┊ ┊ post_str[index] = FCGI_fgetc(stdin); ┊ ┊ if (NULL == post_str) ┊ ┊ { ┊ ┊ ┊ printf("get form NULL!"); ┊ ┊ ┊ break; ┊ ┊ } ┊ } ┊ //url_decode(post_str, len, post_res); ┊ //printf("Content-type: text/html\n\n<><><>%s,%s<><>\n\n", post_str, post_res); ┊ printf("Content-type: text/html\n\n%s\n\n", post_str); } return 0; }注意事项:
1、Post请求时,需要使用FCGI_fgetc(stdin)来进行form表单逐个字节的读取,并需要对其进行URL转码。 2、Get请求时,需要对"QUERY_STRING"这个环境变量进行读值,也需要对其进行URL转码。 3、进行URL转码的函数url_decode
