菜单开关

周梦康 发表于 2020-11-19 404 次浏览 标签 : Spawn-fcgi

安装工具

yum install fcgi-devel spawn-fcgi -y

写一段代码

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

int main(int argc, char **argv) {
    int count = 0;
    while(FCGI_Accept() >= 0)
        printf("Content-type: text/html\n\n"
           "<html><head><title>FastCGI Hello!</title></head>\n"
               "<body><h1>FastCGI Hello!</h1>\n"
               "Request number %d running on host <i>%s</i></body></html>\n",
               ++count, getenv("SERVER_NAME"));
    return 0;
}

编译生成 cgi 程序

gcc -o hello.cgi a.c -lfcgi

启动 fastcgi 服务监听

$ spawn-fcgi -f /xxx/hello.cgi -p 10001 -P /var/run/fcgi.pid -F 3
spawn-fcgi: child spawned successfully: PID: 14644
spawn-fcgi: child spawned successfully: PID: 14645
spawn-fcgi: child spawned successfully: PID: 14646

-F表示需要几个子进程

配置 nginx

处于简单我直接在我当前博客域名下加了如下配置

        location ~ \.cgi$ {
            fastcgi_pass   127.0.0.1:10001;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }

访问测试

http://mengkang.net/xxx.cgi

xxx 可以是任意字符串,可以用这个来做一些路由了就

持续迭代

$ gcc -o hello.cgi a.c -lfcgi
$ kill -9 `cat /var/run/fcgi.pid` && spawn-fcgi -f /root/hello.cgi -p 10001 -P /var/run/fcgi.pid -F 3

评论列表