`
bollaxu
  • 浏览: 217111 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

使用fastcgi c api时注意的问题

阅读更多

最近用fastcgi的c语言api写服务,发现一个问题。我用nginx来接收请求,并通过fastcgi_pass传递到c程序。在用curl测试请求的时候,发现c程序是有被调用的,但是nginx返回的响应一直是502 "upstream closed prematurely FastCGI stdout while reading response header from upstream"。在网上找了很久,也有同样的问题,但是一般都是说原因是printf的时候没有按照http协议,比如说一定要printf("Content-type: text/html \r\n\r\n")。我按照这个格式写了,但还是出错。最后发现是由于include了c和c++的一些头文件,比如,下面的代码就有可能出现这样的问题(也有可能不出现问题):

 

#include "fcgi_stdio.h"
#include <iostream>
#include <string>
#include <map>
#include <stdlib.h>
#include <stdio.h>

int main()
{
    while(FCGI_Accept() >= 0)     
    {
		//////////////////////////////////////////////////////////////////////////
		//get content type and length
		//////////////////////////////////////////////////////////////////////////
		printf("Content-type: text/*\r\n\r\n"); 
		printf("<title>FastCGI Hello! (C, fcgi_stdio library)</title>\n");
    }
}

在官方的api使用例子里面,只有include “fcgi_stdio.h”和“stdlib.h”这两个头文件。于是我看了一下fcgi_stdio.h这个文件,发现它定义了一个宏,也叫printf,而事实上是调用了FCGI_printf这个函数。 而include其他的头文件可能会导致链接的时候链接到原来的printf实现,所以最保险的做法是:

 

#include "fcgi_stdio.h"
#include <iostream>
#include <string>
#include <map>
#include <stdlib.h>
#include <stdio.h>

int main()
{
    while(FCGI_Accept() >= 0)     
    {
		//////////////////////////////////////////////////////////////////////////
		//get content type and length
		//////////////////////////////////////////////////////////////////////////
		FCGI_printf("Content-type: text/*\r\n\r\n"); 
		FCGI_printf("<title>FastCGI Hello! (C, fcgi_stdio library)</title>\n");
    }
}

这样就万无一失了。

分享到:
评论

相关推荐

    fastcgi c++

    fastcgi fastcgi fastcgi fastcgi fastcgi

    Java的FastCGI网关 jFastCGI

    Java的FastCGI网关 jFastCGI

    FastCGI手册

    FastCGI规范和详细的例子

    Fastcgi学习总结(C/C++语言版)

    C++的版本也是大同小异,大家注意留心fastcgi++包里面的C++示例源代码。 好像很少有人用C/C++来开发fastcgi程序,为此,我把自己的一些经验做一个入门级的引导。我测试过我们的项目性能真的很不错!如果有什么问题...

    lighttpd fastcgi

    5.注意fastcgi程序编译方式 进入cgi-bin目录中编译方式如下 2228 gcc -o caoft.fcgi echo.c -lpthread -lfcgi 2229 gcc -o caoft.fcgi size.c -lpthread -lfcgi 2230 gcc -o caoft.fcgi threaded.c -lpthread -...

    node-fastcgi:使用node.js创建FastCGI应用程序

    使用FastCGI无需更改为http服务器编写的代码即可正常工作。 它可用于构建FastCGI应用程序或将现有的节点应用程序转换为FastCGI。 该实现完全符合。 例 var fcgi = require ( 'node-fastcgi' ) ; fcgi . create...

    fastcgi头文件和库

    fastcgi头文件和库

    FastCGI SDK

    FastCGI的官方开发包,包含C, C++, Perl, Java, 库等文档与开发资源。(FastCGI官网已经打不开了 QwQ)

    IIS FASTCGI安装程序

    用于IIS的FASTCGI模块安装包,版本1.5RC

    php5.3以上在windows下fastcgi安装时的几个参数配置

    fastcgi.impersonate = 1 cgi.rfc2616_headers = 1 cgi.fix_pathinfo = 1 cgi.force_redirect = 0 upload_tmp_dir =C:/php/temp extension_dir = "C:/php/temp" extension=php_bz2.dll extension=...

    nginx+fastcgi编译与移植手册

    nginx fastcgi 编译与移植 手册 nginx fastcgi 编译与移植 手册 nginx fastcgi 编译与移植 手册

    FastCGI 下载FastCGI for IIS6下载 1.5(32位&64位)

    FastCGI 1.5 for IIS6,fastcgi.dll本身不具备解析脚本的能力他的作用是调度php的解析程序处理PHP。内含32位和64位两个版本。

    apache和C++实现fastcgi时Apache中httpd.conf的配置

    apache和C++实现fastcgi时Apache中httpd.conf的配置

    win2003下搭建FastCGI php5.2.17

    所以被迫无奈重装服务器,在网上浏览的时候无意的发现PHP5.3的全新特效及改进,原有的ISAPI方式解析PHP脚本已经不被支持,PHP从5.3.0以后的版本开始使用微软的FastCGI模式,虽说PHP 5.3发布的Windows版已经不支持...

    Apache FastCGI 配置过程小记

    对于配置过程其实比较简单只是纠结于在win32环境下apache 模块 mod_fcgid由于管道问题无法达成,而mod_fastcgi模块则可以达成

    mod_fastcgi-2.4.6.tar.gz

    [原创] Redhat 上 FastCGI 安装与配置 软件包 相关软件包: httpd httpd-devel fcgi-2.4.0.tar.gz mod_fastcgi-2.4.6.tar.gz 请仔细阅读其中的README 配置httpd.conf: LoadModule fastcgi_module modules/mod_...

    fastcgi.zip

    fastcgi2.4.1 安装: ./configure make make install 现在官网已经没有了

    fastcgi-2.4.0安装包

    将它直接在win系统下解压,然后传到linux上就可以直接安装了

    FastCGI Extension for IIS

    FastCGI是可伸缩架构的CGI开放扩展,其主要行为是将CGI解释器进程保持在内存中并因此获得较高的性能。传统的CGI解释器的反复加载是CGI性能低下的主要原因,如果CGI解释器保持在内存中并接受FastCGI进程管理器调度,...

    fastcgi安装文件

    fastcgi安装文件

Global site tag (gtag.js) - Google Analytics