Aug 202010
 

Here is a simple example of a hello world program for FastCGI written in C. Before you can compile this, you will need to install the FastCGI devkit. At the time of writing the latest version is available from www.fastcgi.com

extract, compile and install fcgi-current.tar.gz

$ tar xvzf fcgi-current.tar.gz
$ cd  fcgi-2.x.x/
$ ./configure
$ make
$ sudo make install

the c code

#include <fcgi_stdio.h>
int main( int argc, char *argv[] )
{
   while( FCGI_Accept() >= 0 ) {
      printf( "Content-Type: text/plain\n\n" );
      printf( "Hello world in C\n" );
   }
   return 0;
}


save as hello.c
compile and linked against fcgi library

$ gcc -o hello hello.c -lfcgi

run the code using spawn-cgi

# spawn-fcgi -a127.0.0.1 -p9000 -n ./hello

Nginx configuration

server {
        listen   80;
        server_name _;

       location / {
                        # host and port to fastcgi server
                        root   /home/user/www;
                        index  index.html;

                        fastcgi_pass 127.0.0.1:9000;
       }
}

that’s for now 🙂

  13 Responses to “Nginx, fastcgi, ‘Hello World’ in C”

Comments (13)
  1. By http specs content must be after headers through two ‘\n’ and nginx gave 502 code with this message in error log:

    2014/01/09 21:32:52 [error] 3826#0: *13 upstream prematurely closed FastCGI stdout while reading response header from upstream, client: 127.0.0.1, server: hello, request: “GET / HTTP/1.1”, upstream: “fastcgi://127.0.0.1:9002”, host: “hello”

    Please, solve it

  2. The error in the above code is as follows :

    text/plainnn

    should read

    text/plain\n\n

    Duh !

  3. LOL, thanks. i’ll fix it 😀

 Leave a Reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

(required)

(required)

*