C语言 C 语言 FastCGI 与 Nginx
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2149709/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C language FastCGI with Nginx
提问by Arek B.
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.
我正在尝试在 Nginx Web 服务器后面运行用 C 语言编写的 fastcgi 应用程序。Web 浏览器永远不会完成加载并且响应永远不会完成。我不知道如何处理和调试。任何见解将不胜感激。
The hello world application was taken from fastcgi.com and simplified to look like this:
hello world 应用程序取自 fastcgi.com 并简化为如下所示:
#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
cgi-fcgi -connect 127.0.0.1:9000 a.out
or
或者
spawn-fcgi -a120.0.0.1 -p9000 -n ./a.out
spawn-fcgi -a120.0.0.1 -p9000 -n ./a.out
Nginx configuration is:
Nginx 配置为:
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;
}
}
采纳答案by Sinan ünür
You need to call FCGI_Acceptin the whileloop:
您需要FCGI_Accept在while循环中调用:
while(FCGI_Accept() >= 0)
You have FCGI_Accept >= 0in your code. I think that results in the address of the FCGI_Acceptfunction being compared to 0. Since the function exists, the comparison is never false, but the function is not being invoked.
你有FCGI_Accept >= 0你的代码。我认为这会导致将FCGI_Accept函数的地址与0. 由于该函数存在,所以比较永远不会为假,但不会调用该函数。
回答by Homer6
Here's a great example of nginx, ubuntu, c++ and fastcgi.
这是 nginx、ubuntu、c++ 和 fastcgi 的一个很好的例子。
http://chriswu.me/blog/writing-hello-world-in-fcgi-with-c-plus-plus/
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.
如果你想运行他的代码,我已经把它放到了一个带有说明的 git repo 中。您可以检查它并自己运行它。我只在 Ubuntu 上测试过。
回答by bhelm
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.
在您的应用程序正确处理 fastcgi-requests 之后,您需要注意启动应用程序。nginx 永远不会产生 fcgi 进程本身,所以你需要一个守护进程来处理它。
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.
我推荐使用 uwsgi 来管理 fcgi 进程。它能够产生准备好输入的工作进程,并在它们死亡时重新启动它们。它具有高度可配置性且易于安装和使用。
http://uwsgi-docs.readthedocs.org/en/latest/
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.
这很好地集成为 systemd 服务,但也可以在没有的情况下运行。
回答by pau
Try with:
尝试:
$ cgi-fcgi -start -connect localhost:9000 ./hello
It works for me. I'm using archlinux and following the instructions at:
这个对我有用。我正在使用 archlinux 并按照以下说明操作:
回答by Oktaheta
You can try this https://github.com/Taymindis/ngx-c-handler
你可以试试这个 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.
它建立在 fastcgi 之上,可以处理多个请求,并且还有一些核心功能。它可以处理与 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
使用 c/c++ 语言启动 nginx https://github.com/Taymindis/ngx-c-handler/wiki/How-to-build-a-cpp-service-as-c-service-interface

