Nginx, fastcgi, ‘Hello World’ in C

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 🙂

11 Comments

    • G-WAN is a joke! Don’t waste your time. I spent hours trying to get that server online to serve a simple fcgi application. The end result was that it didn’t support 64bit and they had no intentions of supporting 64bit at the time I tried it. I tried everything that was recommended to have G-WAN run on a 64bit system, but in the end the performance improvements were not worth the time. In addition, it is a closed source project. Might as well try Cherokee if you want to try something new.

  1. RealFloat

    Yo Sabka Traffic. I’m sorry, but you’re fatally wrong and spread false believes by your jokingly amateurish testing. I’m so harsh with my critique, because you’re so aggresive on foreign blogs.
    1. Do you even know why and when you need 64Bit?
    2. It supports 64bit, but isn’t 32bit itself. It’s very easy to get it working on true 64bit system, I’ve installed it in gentoo and slackware so where is the problem. Post your experience and ask for helps instead of complaining..
    3. I bet you’ve not even tried Cherokee, but recommend it here, even if you tried it, then I’m sure you’ve not tested it thouroughly to be able to recommend it.
    4. “If you want something new” sounds like you’ve not even bothered to look what that app server is..
    5. G-WAN is a C AppServer not a PHP,C#,Java.. AppServer, but even then your comment doesn’t make sense..
    There is a guy who wrote a fast-cgi handler for gwan to support other languages than C. If you’re talking abuot this, it has nothing to do with the original product.

    cry baby

    • NoImagination

      For me , i definitely believe it always is an excellent product,and i wonder how it grows if it is not opened or maintained by a company?

  2. aleccmail

    Indeed G-Wan is closed source and somehow exotic web server. For some real web development it still has to grow a LOT. right now it is not suitable for common web development.

    I think we have to wait at least a couple more years to see if it opens and has a bigger community. Right now it has only been used on toy applications.

    cheers

  3. AnotherHumanBeing

    @Sabka Traffic
    Use the new 64bit release and the native/handler link for any of the new supported languages instead of the slow fcgi interface or wait for/help with a thread-safe PHP library/compiler if it’s PHP the one you need.

    @aleccmail
    It’s not “somehow exotic”, it’s exceptionally unique in the best meanings of those words. The author is using it to host his web since the first beta and the very reason to create G-WAN was because he couldn’t find the high performing platform needed to build his even more amazing “toy” services on (check the sadly read-only now official forum if curious enough).

    The one-man-team in just a couple of years created the most secure and feature-rich application server to date, (of all publicly availabe at least) even in it’s current release candidate state… and it keeps getting better at tremendous pace (check the recent 3.1.3 release) – compare that with the huge amounts of developers over decades wasted (it’s hard to find another word if you look at the end results) on polishing bad desings instead of thinking out of the box.

  4. I am trying to do the same thing but when I run ‘cgi-fcgi -connect 127.0.0.1:9000 ./echo’ it just outputs the the string and nginx reports : ‘The page you are looking for is temporarily unavailable. Please try again later.’ Any idea, link or resource?

    • well, i don’t know what cgi application you’ve used for spawning the echo
      in my tutorial i used spawn-cgi

      spawn-fcgi -a120.0.0.1 -p9000 -n ./hello
      
    • pau

      I’m just trying the same and works for me.
      The only difference is using the -start argument


      cgi-fcgi -start -connect localhost:9000 ./hello

      I’m using archlinux and following the instructions in the following page:
      https://wiki.archlinux.org/index.php/Nginx

  5. Sergey

    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

Leave a Reply

Your email address will not be published. Required fields are marked *