1

我使用本教程http://chriswu.me/blog/writing-hello-world-in-fcgi-with-c-plus-plus/用 nginx + fcgi 编写了一个 c++ 服务器应用程序。c++ 服务器应用程序是由 spawn-fcgi 生成的,因为 nginx 不会自己生成它。这是C++代码:

#include <iostream>
#include "fcgio.h"

using namespace std;

int main(void) {
    // Backup the stdio streambufs
    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"
             << "<html>\n"
             << "  <head>\n"
             << "    <title>Hello, World!</title>\n"
             << "  </head>\n"
             << "  <body>\n"
             << "    <h1>Hello, World!</h1>\n"
             << "  </body>\n"
             << "</html>\n";

        // Note: the fcgi_streambuf destructor will auto flush
    }

    // restore stdio streambufs
    cin.rdbuf(cin_streambuf);
    cout.rdbuf(cout_streambuf);
    cerr.rdbuf(cerr_streambuf);

    return 0;
}

在 linux 上一切正常。在 Windows 上,当 FCGX_Accept_r(&request) 被调用时,完全相同的应用程序将失败。错误消息是 "unknown listenType (0)" 。listenType 是 libfcgi 库https://github.com/jocelyn-old/libfcgi/blob/master/libfcgi/os_win32.c的内部变量 ,应该是 FD_SOCKET_ASYNC 或 FD_PIPE_ASYNC (有更多类型,但其余的是不支持)。只需查看代码,在我看来,要么:

  1. spawn-fcgi 没有正确地将标准输出/输入/错误和环境变量转发到 c++ libfcgi 应用程序,这就是为什么在 windows 上没有正确完成 listenType 变量的初始化
  2. libfcgi 的 Windows 实现太旧了,并且在最近的 Windows 10 上已损坏。这种情况对我来说似乎不太可能,因为我能够使用 apache + fcgi 成功运行相同的 c++ 应用程序并且没有 spawn-fcgi(apache 可以生成 fcgi 应用程序) .

根据证据,可能是 spawn-fcgi 在 Windows 上损坏了,或者我使用 cygwin 构建的 spawn-fcgi 无法正常工作。

有人在 Windows 上成功运行 nginx + c++ fastcgi app + spawn-fcgi 吗?

4

0 回答 0