C language FastCGI with Nginx

2019-02-02 13:50发布

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;
 }
}

标签: c nginx fastcgi
5条回答
老娘就宠你
2楼-- · 2019-02-02 13:55

Try with:

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

It works for me. I'm using archlinux and following the instructions at:

https://wiki.archlinux.org/index.php/Nginx

查看更多
倾城 Initia
3楼-- · 2019-02-02 14:02

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.

查看更多
你好瞎i
4楼-- · 2019-02-02 14:15

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

查看更多
欢心
5楼-- · 2019-02-02 14:21

After your application handles fastcgi-requests correctly, you need to take care of starting the application. nginx will never spawn fcgi processes itself, so you need a daemon taking care of that.

I recommend using uwsgi for managing fcgi processes. It is capable of spawning worker-processes that are ready for input, and restarting them when they die. It is highly configurable and easy to install and use.

http://uwsgi-docs.readthedocs.org/en/latest/

Here is my config:

[uwsgi]
fastcgi-socket = /var/run/apc.sock
protocol = fastcgi
worker-exec = /home/app/src/apc.bin
spooler = /home/app/spooler/
processes = 15
enable-threads = true
master = true
chdir = /home/app/
chmod-socket = 777

This integrates nicely as systemd service, but can also run without.

查看更多
祖国的老花朵
6楼-- · 2019-02-02 14:21

You can try this https://github.com/Taymindis/ngx-c-handler

It is built on top on fastcgi, It handle multiple request, and there are some core feature as well. It can handler function mapping with nginx.

To startup a nginx with c/c++ language https://github.com/Taymindis/ngx-c-handler/wiki/How-to-build-a-cpp-service-as-c-service-interface

查看更多
登录 后发表回答