Welcome! Log In Create A New Profile

Advanced

NginX with FastCGI and C

Posted by kalamcs 
NginX with FastCGI and C
August 10, 2009 10:04AM
Hi

I am a newbie with FastCGI, but going through a variety of forums online, I found that a very fast web-server application can be created using a combination of NginX and FastCGI. My obvious choice for CGI is C/C++ (I am looking at being able to serve about a thousand requests per sec). Unfortunately, after compiling and installing NginX all that I have been able to do so far is serve HTML pages.

I think that nginx is expecting some program listening at port 9000 (or whatever is set in the conf file) and execute the cgi file through that program. Is it expected that we create such a program ourselves or is something available? Or am I just too naive and have absolutely no idea about what's going on? :)

Any help would be very much appreciated.

Thanks
Kalam
Re: NginX with FastCGI and C
August 10, 2009 12:45PM
Hi i am fastcgi newbie too and i am also have troubles. And i am try to find some help from skilled devs.

But i can help you little:
You absolutely right, Nginx expect a fastcgi app listening on tcp port 9000.
I am not a linux pro, but linux is the more native env for this software (nginx).
I alredy compiled a sample application on cpp for nginx and that the way how how can i achieved this:

Firstly you need go to http://www.fastcgi.com/ and download "The Development Kit".
Then compile a library (i not completely understand what file is a library .so or somthen else but this works):
---
[size=small]wget http://www.fastcgi.com/dist/fcgi.tar.gz
gzip fcgi-2.4.0.tar.gz -d
tar xvvf fcgi-2.4.0.tar
cd fcgi-2.4.0
./configure
make[/size]
---

then copy libs from .libs directory to /usr/lib/

then you need to code simple application like that
-----------------------------------------
[size=small]#include <string>
#include "fcgi_stdio.h"
#include <stdlib.h>
#include <iostream>

void handle(FCGX_Request request){
FCGX_FPrintF(request.out, "Content-type: text/html\r\n\r\n<TITLE>fastcgi</TITLE>\n<H1>Fastcgi:.</H1>\n");
}

int main(int argc, char* const argv[] )
{

std::string port=":9001";
int listenQueueBacklog = 400;
FCGX_Stream *in, *out, *err;
FCGX_ParamArray envp;


if(FCGX_Init()) exit(1);

int listen_socket = FCGX_OpenSocket(port.c_str(), listenQueueBacklog);
if(listen_socket < 0) exit(1);

FCGX_Request request;
if(FCGX_InitRequest(&request, listen_socket, 0)) exit(1);

int reqCounter = 0;
while(FCGX_Accept_r(&request) == 0)
{
//handle(request);
//FCGX_FPrintF(request.out, "Content-type: text/html\r\n\r\n<TITLE>fastcgi</TITLE>\n<H1>Fastcgi: Hello world.</H1>\n");
reqCounter++;
FCGX_FPrintF(request.out, "\n\r\n\r counter: %d", reqCounter);

//FCGX_Finish_r(&request);
}

return 0;
}[/size]
-----------------------------------------

then you need to compile it

g++ test.cpp -B fcgi/ -Lfcgi/libfcgi/.libs/ -lfcgi

and run it (it says nothing, just works)

after, configure nginx to pass fcgi requests to this program

nginx + this app process about 1500 http requests per second, on my virtual machine (when nginx + php fastcgihandles only about 500)

That all i can do, now i am tring to access post fileds parameters but i don't know how!

-------------------
HEY! Who knows how to develop fastcgi cpp apps, [size=large]please give some help me[/size]! Yes i know - google, but there is no information about this on the internet!

I have these questions now:

[size=large]1. how to get request post data? How to parse it?[/size]
[size=large]2. how to work with cookies, sessions?[/size]

Any help will be very very appreciated!!!



Edited 2 time(s). Last edit at 08/10/2009 12:55PM by eduard.
Re: NginX with FastCGI and C
August 11, 2009 09:29AM
Hey, thanks a lot. I'll try to work with it. Meanwhile, did you try getting the request parameters as they are obtained in normal CGI apps?
Something like...

char *data;
data = getenv("QUERY_STRING");

I don't know whether it will work or not, but its definitely worth a shot.

-Kalam
Re: NginX with FastCGI and C
January 03, 2011 01:38AM
Sorry to be disturbing the peacefullness of a long sleeping thread!!

I didn't understand a part of the post above :

eduard Wrote:
-------------------------------------------------------
>
> and run it (it says nothing, just works)
>
> after, configure nginx to pass fcgi requests to
> this program
>

I didn't understand how you do this part without the "spawn-fcgi" command.
How do we configure nginx server so that it automatically sends requests to the fcgi program without the spawn-fcgi command ?
Sorry, only registered users may post in this forum.

Click here to login

Online Users

Guests: 289
Record Number of Users: 8 on April 13, 2023
Record Number of Guests: 421 on December 02, 2018
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready