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

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;


that’s for now 🙂

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

Comments (14)
  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:, server: hello, request: “GET / HTTP/1.1”, upstream: “fastcgi://”, host: “hello”

    Please, solve it

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


    should read


    Duh !

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

  4. doesn’t work for me … nginx/1.14.0

 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>




This site uses Akismet to reduce spam. Learn how your comment data is processed.