Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C language FastCGI with Nginx

Tags:

I am attempting to run a fastcgi app written in C language behind the Nginx web server. The web browser never finishes loading and the response never completes. I am not sure how to approach it and debug. Any insight would be appreciated.

The hello world application was taken from fastcgi.com and simplified to look like this:

#include "fcgi_stdio.h"
#include <stdlib.h>

int main(void)
{

 while(FCGI_Accept >= 0)
 {
  printf("Content-type: text/html\r\nStatus: 200 OK\r\n\r\n");

 }

  return 0;
}

Output executable is executed with either one of:

cgi-fcgi -connect 127.0.0.1:9000 a.out

or

spawn-fcgi -a120.0.0.1 -p9000 -n ./a.out

Nginx configuration is:

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;
 }
}
like image 227
Arek B. Avatar asked Jan 27 '10 19:01

Arek B.


People also ask

What is FastCGI in nginx?

FastCGI is a protocol based on the earlier CGI, or common gateway interface, protocol meant to improve performance by not running each request as a separate process. It is used to efficiently interface with a server that processes requests for dynamic content.

Is FastCGI fast?

FastCGI is a fast, open, and secure Web server interface that solves the performance problems inherent in CGI, without introducing the overhead and complexity of proprietary APIs (Application Programming Interfaces).

What is the use of FastCGI?

Basically, FastCGI is a program that manages multiple CGI requests within a single process, saving many program instructions for each request. Without FastCGI, each instance of a user requesting a service causes the Web server to open a new process that gets control, performs the service, and then is closed.


2 Answers

You need to call FCGI_Accept in the while loop:

while(FCGI_Accept() >= 0)

You have FCGI_Accept >= 0 in your code. I think that results in the address of the FCGI_Accept function being compared to 0. Since the function exists, the comparison is never false, but the function is not being invoked.

like image 105
Sinan Ünür Avatar answered Oct 19 '22 08:10

Sinan Ünür


Here's a great example of nginx, ubuntu, c++ and fastcgi.

http://chriswu.me/blog/writing-hello-world-in-fcgi-with-c-plus-plus/

If you want to run his code, I've put it into a git repo with instructions. You can check it out and run it for yourself. I've only tested it on Ubuntu.

https://github.com/homer6/fastcgi

like image 30
Homer6 Avatar answered Oct 19 '22 08:10

Homer6