【问题标题】:FCGX_Accept_r runs twice in simple FastCGI application on CFCGX_Accept_r 在 C 上的简单 FastCGI 应用程序中运行两次
【发布时间】:2015-11-24 01:51:02
【问题描述】:

我正在尝试创建用 C 编写的简单 FastCGI 应用:

#include <fcgiapp.h>
#include <stdio.h>
int main()
{
    int sockfd = FCGX_OpenSocket("127.0.0.1:9000", 1024);
    FCGX_Request request;

    FCGX_Init();
    FCGX_InitRequest(&request, sockfd, 0);

    while (FCGX_Accept_r(&request) == 0)
    {
        printf("Accepted\n");
        FCGX_FPrintF(request.out, "Content-type: text/html\r\n\r\n<h1>Hello World!</h1>");
        FCGX_Finish_r(&request);
    }
}

它工作正常 - 当我从浏览器调用它时,它会显示带有“Hello World!”的页面消息。

问题是“while”中的代码工作两次,即我在终端中看到以下输出:

[root@localhost example]$ ./hello 
Accepted 
Accepted

为什么每次请求都会打印两次“Accepted”? 如果我在这里放真正的代码,例如查询一个数据库,它也会被执行两次。

【问题讨论】:

    标签: c nginx fastcgi


    【解决方案1】:

    您使用什么程序来发出网络请求?一些网络客户端也可能会请求一个例如favicon.ico文件,可能是对fcgi进程的第二次请求:

    127.0.0.1 - - [29/Aug/2015:16:02:11 +0000] "GET / HTTP/1.1" 200 32 "-" "..."
    127.0.0.1 - - [29/Aug/2015:16:02:11 +0000] "GET /favicon.ico HTTP/1.1" 200 32 "http://127.0.0.1/" "..."
    

    这可以通过检查网络服务器日志来确定,或者在 C 程序中添加调试以显示请求参数。仅使用 telnet 请求 GET / HTTP/1.0,我看不到使用 nginx 网络服务器的 forward-everything-to-fcgi 网络服务器配置的双重打击。

    【讨论】:

    • 嗬!五年的网络编程中断产生了影响。第二个实际上是浏览器对网站图标的请求。非常感谢!
    • 是的,这也是我绊倒的第一件事。
    猜你喜欢
    • 2019-04-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-29
    • 1970-01-01
    • 2017-09-20
    • 2012-10-12
    相关资源
    最近更新 更多