参考:

nginx+c/c++ fastcgi:http://www.yis.me/web/2011/11/01/66.htm

cgi探索之路:http://github.tiankonguse.com/blog/2015/01/19/cgi-nginx-three/

  有些性能要求很高的后端CGI都是用C写的,以lighthttp为server 。虽然php结合php-fpm的fastcgi模式也有不错的性能,但某些情况下C的性能还是php无法比拟的。

  先有必要有这样第一个认识:ngxin作为一个webserver,本身并不包含CGI的解释器,需要通过一个中间件【例如php-fpm】来运行CGI。他们之间的模式大致是:
  nginx <-- socket --> php-fpm-------->php

  那么nginx既然要运行c写的CGI那么也应该有类似php-fpm的东西。这个就是spawn-fcgi。原本是lighttp 内的fastcgi进程管理器。

下面是具体步骤:

1.安装Spawn-fcgi 【提供调度cig application】

wget   http://download.lighttpd.net/spawn-fcgi/releases-1.6.x/spawn-fcgi-1.6.4.tar.gz
tar zxvf spawn-fcgi-1.6..tar.gz
cd spawn-fcgi-1.6.
./configure --prefix=/usr/local/spawn-fcgi
make & make install

2、安装fastcgi库 【提供编写cgi时的类库】

wget  http://www.fastcgi.com/dist/fcgi.tar.gz
tar zxvf fcgi.tar.gz
cd fcgi
./configure --prefix=/usr/local/fastcgikit/make & make install

注意:

cp /usr/local/fastcgikit/lib/libfcgi.so.0 /usr/lib64

一定要这样复制过来,否则会报错找不到文件:

3.nginx安装配置【提供web访问】

location ~ \.cgi$ {
#proxy_pass http://127.0.0.1;
root /cgi;
fastcgi_pass 127.0.0.1:;//让其监听9001端口,与spawn-cgi监听端口一致
fastcgi_index index.cgi;
fastcgi_param SCRIPT_FILENAME /cgi$fastcgi_script_name;
include fastcgi_params;
}

4.测试代码fcgi_hello.c:(c代码)

#include "fcgi_stdio.h" //要写在行首(fcgi_stdio.h里定义的printf与c里的冲突),且用冒号(引用路径而非全局)
#include <stdio.h>
#include <stdlib.h> int main(void)
{
int count = 0;
while(FCGI_Accept() >= 0) {
printf("Content-type: text/html\r\n");
printf("\r\n");
printf("Hello world!<br>\r\n");
printf("Request number %d.", count++);
}
return 0;
}

编译测试代码:(必须要有-lfcgi参数)

g++ fcgi_hello.c -o fcgi_hello -L /usr/local/fastcgikit/lib/ -lfcgi

注意:

(1).该代码要放在/usr/local/fastcgikit/include这个目录下编译,因为要包含头文件fcgi_stdio.h进来;

5.启动spawn-cgi

/usr/local/spawn-fcgi/bin/spawn-fcgi -a 127.0.0.1 -p  -F   -f /usr/local/fastcgikit/include/fcgi_hello -u json

查看效果:ps -ef|grep fcgi_hello

问题:

Nginx + FastCgi + Spawn-fcgi + c 的架构的更多相关文章

  1. Nginx + FastCGI 程序(C/C++) 搭建高性能web service的Demo及部署发布

       FastCGI编程包括四部分:初始化编码.接收请求循环.响应内容.响应结束循环. FCGX_Request request; FCGX_Init(); ); FCGX_InitRequest(& ...

  2. 【入门篇】Nginx + FastCGI 程序(C/C++) 搭建高性能web service的Demo及部署发布

    http://blog.csdn.net/allenlinrui/article/details/19419721 1.介绍     Nginx - 高性能web server,这个不用多说了,大家都 ...

  3. [转] nginx+FastCGI+c++

    from: http://www.cnblogs.com/xiaouisme/archive/2012/08/01/2618398.html 一 安装 目的:不需支持php等.就html就行了.步骤: ...

  4. nginx fastcgi配置

    1.1 nginx概述nginx简介Nginx是俄罗斯人编写的十分轻量级的HTTP服务器,Nginx,它的发音为“engine X”, 是一个高性能的HTTP和反向代理服务器,同时也是一个IMAP/P ...

  5. Nginx + FastCGI 程序(C/C++)搭建高性能web service的demo

    http://blog.csdn.net/chdhust/article/details/42645313 Nginx + FastCGI 程序(C/C++)搭建高性能web service的Demo ...

  6. nginx+fastcgi+c/cpp

    参考:http://github.tiankonguse.com/blog/2015/01/19/cgi-nginx-three/ 跟着做了一遍,然后根据记忆写的,不清楚有没错漏步骤,希望多多评论多多 ...

  7. nginx fastcgi php-fpm的关系梳理

    CGI(Common Gateway Interface)CGI全称是“公共网关接口”(Common Gateway Interface),HTTP服务器与你的或其它机器上的程序进行“交谈”的一种工具 ...

  8. nginx 3.nginx+fastcgi

    死磕nginx 3.nginx+fastcgi 互联网服务器有个非常典型的架构lamp(linux+apache+mysql+php),由于其开源和强大的兼容性而风靡一时,不过随着nginx的横空出世 ...

  9. Nginx+FastCGI运行原理

    Nginx不支持对外部程序的直接调用或者解析,所有的外部程序(包括PHP)必须通过FastCGI接口来调用.FastCGI接口在Linux下是socket(这个socket可以是文件socket,也可 ...

  10. 记录一次自己对nginx+fastcgi(fpm)+mysql压力测试结果

    nginx + fastcgi(fpm) 压力测试: CentOS release 5.9 16核12G内存 静态页面: 并发1000,压测200秒,测试结果: 系统最大负载5.47 成功响应: 25 ...

随机推荐

  1. 安装数据库出现错误vc_red.msi找不到

    用虚拟光驱安装数据的时候可能会出现,找不到vc_red.msi的问题,通过加载的虚拟光驱目录设置,可能 仍然后问题,比如程序停止运行. 解决方法是:解压iso文件,用解压后的文件安装.然后在解压文件夹 ...

  2. django 创建项目

    django-admin startproject project-name 启动服务器 python manage.py runserver 0.0.0.0:8000 配置ALLOW_HOST

  3. VB6.0和VB.Net的函数等对照表

    VB6.0和VB.Net的对照表 VB6.0 VB.NET AddItem Object名.AddItem Object名.Items.Add ListBox1.Items.Add ComboBox1 ...

  4. (Unity)Unity自定义Debug日志文件,利用VS生成Dll文件并使用Dotfuscated进展混淆,避免被反编译

    Unity自定义Debug日志文件,利用VS生成Dll文件并使用Dotfuscated进行混淆,避免被反编译. 1.打开VS,博主所用版本是Visual Studio 2013. 2.新建一个VC项目 ...

  5. (light OJ 1005) Rooks dp

    http://www.lightoj.com/volume_showproblem.php?problem=1005        PDF (English) Statistics Forum Tim ...

  6. C2第七次作业解题报告

    看过题解后如果觉得还算有用,请帮忙加点我所在团队博客访问量 http://www.cnblogs.com/newbe/ http://www.cnblogs.com/newbe/p/4069834.h ...

  7. Django学习(二)

    一  高亮显示 <script type="text/javascript"> $(document).ready(function () { $("#nav ...

  8. node-restify简介

    restify 是Node.js的模块.虽然restify的API或多或少的参考了express,但restify不是一个MVC框架,它是一套为了能够正确构建REST风格API而诞生的框架. ###安 ...

  9. ASP.NET MVC学习之过滤器篇(1)

    一.前言 继前面四篇ASP.NET MVC的随笔,我们继续向下学习.上一节我们学习了关于控制器的使用,本节我们将要学习如何使用过滤器控制用户访问页面. 二.正文 以下的示例建立在ASP.NET MVC ...

  10. C#像运行一个exe 程序一样运行一个dll文件

    [DllImport("kernel32.dll")] public static extern int WinExec(string exeName, int operType) ...