kaogu zhang
kaogu zhang

Reputation: 1

FCGX_GetParam REQUEST_METHOD can used in windows but return NULL in linux

my nginx/conf.d/default.conf is

server {

listen       80;
server_name  127.0.0.1;

#charset koi8-r;
#access_log  /var/log/nginx/log/host.access.log  main;

location / {
    root   /usr/share/nginx/html;
    index  index.html index.htm;
}

rewrite ^(.*)(?i)charge(.*)$ $1charge$2 break;
rewrite ^(.*)(?i)login(.*)$ $1login$2 break;
rewrite ^(.*)(?i)plat_api(.*)$ $1plat_api$2 break;


location ~charge$  {
    root          /data/servers/NewGame_Server/fcgi_bin;
    fastcgi_pass  127.0.0.1:9991;
    fastcgi_index index.cgi;
    set $real_script_name $fastcgi_script_name;
    if ($fastcgi_script_name ~ "^(.+?\.cgi)(/.+)$") {
        set $real_script_name $1;
        set $path_info $2;
    }

    fastcgi_param SCRIPT_FILENAME $document_root$real_script_name;
    fastcgi_param SCRIPT_NAME $real_script_name;
    fastcgi_param PATH_INFO $fastcgi_script_name;
}

}

my code is like this, in windows it can be used successfully, but in linux i can't get "char * method = FCGX_GetParam("REQUEST_METHOD", request.envp)", "method" return NULL, i have installed php-fpm, what can i do for how to solve this problem?Is some mistakes in my nginx's configs? who can help me? Tanks for every dear classmates!!!

int main()
{

FCGX_Request& request = g_pTheWorld->GetFCGX_Request();

    streambuf * cin_streambuf = cin.rdbuf();
    streambuf * cout_streambuf = cout.rdbuf();
    streambuf * cerr_streambuf = cerr.rdbuf();

    FCGX_Init();
    FCGX_InitRequest(&request, 0, 0);
    while (FCGX_Accept_r(&request) == 0)
    {
        CIfBase* ifobj = CIfFactory::getIfObj();
        if (ifobj == NULL)
        {
            LogError("error_env_platform", "");
            continue;
        }

        // Note that the default bufsize (0) will cause the use of iostream
        // methods that require positioning (such as peek(), seek(),
        // unget() and putback()) to fail (in favour of more efficient IO).
        fcgi_streambuf cin_fcgi_streambuf(request.in);
        fcgi_streambuf cout_fcgi_streambuf(request.out);
        fcgi_streambuf cerr_fcgi_streambuf(request.err);

#if HAVE_IOSTREAM_WITHASSIGN_STREAMBUF
        cin = &cin_fcgi_streambuf;
        cout = &cout_fcgi_streambuf;
        cerr = &cerr_fcgi_streambuf;
#else
        cin.rdbuf(&cin_fcgi_streambuf);
        cout.rdbuf(&cout_fcgi_streambuf);
        cerr.rdbuf(&cerr_fcgi_streambuf);
#endif

        //setvbuf(stdin, NULL, _IONBF, 0);     /*关闭stdin的缓冲*/

        char * method = FCGX_GetParam("REQUEST_METHOD", request.envp);
        if (method == NULL)
        {
            LogError("REQUEST_METHOD","error : size = 0");
            continue;
        }

        string intput;
        if (strcmp(method, "GET") == 0)
        {
            intput = FCGX_GetParam("QUERY_STRING", request.envp);
        }
        else if (strcmp(method, "POST") == 0)
        {
            intput = get_request_content(request);
        }

        FCGX_Finish_r(&request);

    }

#if HAVE_IOSTREAM_WITHASSIGN_STREAMBUF
    cin = cin_streambuf;
    cout = cout_streambuf;
    cerr = cerr_streambuf;
#else
    cin.rdbuf(cin_streambuf);
    cout.rdbuf(cout_streambuf);
    cerr.rdbuf(cerr_streambuf);
#endif

    delete g_pTheWorld;

    return 0;
}

Upvotes: 0

Views: 1386

Answers (1)

Richard Smith
Richard Smith

Reputation: 49672

Most (if not all) nginx distributions come with a file called /etc/nginx/fastcgi_params which includes a number of fastcgi_param statements, including:

fastcgi_param   REQUEST_METHOD   $request_method;

Either, include the file before your customised statements:

location ~charge$  {
    ...
    include fastcgi_params;
    fastcgi_param SCRIPT_FILENAME $document_root$real_script_name;
    fastcgi_param SCRIPT_NAME $real_script_name;
    fastcgi_param PATH_INFO $fastcgi_script_name;
}

Or just add the REQUEST_METHOD definition to your existing list.

See this document for more.

Upvotes: 2

Related Questions