nginx配置cgi

来源:互联网 发布:白虹软件倒闭 编辑:程序博客网 时间:2024/05/01 16:47

CGI

通用网关接口(Common Gateway Interface/CGI)描述了客户端和服务器程序之间传输数据的一种标准。wiki Common_Gateway_Interface
这个 Web 服务器使用了 UNIX shell 环境变量 来保存从 Web 服务器传递出去的参数,然后生成一个运行 CGI 的 独立进程 。CGI的第一个实现是 Perl 写的 1 。

  • 效率低下:每一个连接 fork 一个进程处理。
  • 功能十分有限:CGI只能收到一个请求,输出一个响应。

FastCGI

快速通用网关接口(Fast Common Gateway Interface/FastCGI)是通用网关接口(CGI)的改进,FastCGI使用进程/线程池来处理一连串的请求。这些进程/线程由FastCGI服务器管理,而不是Web服务器。 当进来一个请求时,Web服务器把环境变量和这个页面请求通过一个Socket长连接传递给FastCGI进程。wiki FastCGI

  • 性能:通过进程/线程池规避了CGI开辟新的进程的开销。
  • 语言无关:FastCGI是一套标准,理论上讲只要能进行标准输出(stdout)的语言都可以作为FastCGI标准的Web后端。

安装FCGI

获取FCGI安装包,http://www.fastcgi.com/drupal/node/5

sudo ./configuresudo make && make install 
fcgio.cpp: In destructor 'virtual fcgi_streambuf::~fcgi_streambuf()':fcgio.cpp:50:14: error: 'EOF' was not declared in this scope     overflow(EOF);              ^fcgio.cpp: In member function 'virtual int fcgi_streambuf::overflow(int)':fcgio.cpp:70:72: error: 'EOF' was not declared in this scope             if (FCGX_PutStr(pbase(), plen, this->fcgx) != plen) return EOF;                                                                        ^fcgio.cpp:75:14: error: 'EOF' was not declared in this scope     if (c != EOF)               ^fcgio.cpp: In member function 'virtual int fcgi_streambuf::sync()':fcgio.cpp:86:18: error: 'EOF' was not declared in this scope     if (overflow(EOF)) return EOF;                  ^fcgio.cpp:87:41: error: 'EOF' was not declared in this scope     if (FCGX_FFlush(this->fcgx)) return EOF;                                         ^fcgio.cpp: In member function 'virtual int fcgi_streambuf::underflow()':fcgio.cpp:113:35: error: 'EOF' was not declared in this scope             if (glen <= 0) return EOF;                                   ^Makefile:311: recipe for target 'fcgio.lo' failed

解决办法:在/include/fcgio.h文件中加上 #include ,然后再编译安装就通过了。

安装 spawn-fcgi

spawn-fcgi是一个通用的FastCGI进程管理器,简单小巧,原先是属于lighttpd的一部分,后来由于使用比较广泛,所以就迁移出来作为独立项目了。spawn-fcgi使用pre-fork 模型,功能主要是打开监听端口,绑定地址,然后fork-and-exec创建我们编写的fastcgi应用程序进程,退出完成工作。fastcgi应用程序初始化,然后进入死循环侦听socket的连接请求。

sudo apt-get install spawn-fcgi #可以man spawn-fcgi查看帮助
#include <stdlib.h>#include "fcgi_stdio.h"int main(void){    int count = 0;    while (FCGI_Accept() >= 0)        printf("Content-type: text/html\r\n"        "\r\n"        "<title>FastCGI Hello!</title>"        "<h1>FastCGI Hello!</h1>"        "<div>Request number %d running on host : %s </div>\n"        "<div>QUERY_STRING : %s\n</div>"        "<div>REMOTE_ADDR : %s\n</div>"        "<div>REMOTE_PORT : %s\n</div>"        "<div>REQUEST_METHOD : %s\n</div>"        "<div>CONTENT_TYPE : %s\n</div>"        "<div>CONTENT_LENGTH : %s\n</div>"        "<div>SERVER_PROTOCOL : %s\n</div>"        "<div>REQUEST_URI : %s\n</div>"        "<div>SERVER_SOFTWARE : %s\n</div>",        ++count, getenv("SERVER_NAME"),getenv("QUERY_STRING"),        getenv("REMOTE_ADDR"), getenv("REMOTE_PORT"), getenv("REQUEST_METHOD"),        getenv("CONTENT_TYPE"),getenv("CONTENT_LENGTH"),getenv("REQUEST_URI"),        getenv("SERVER_PROTOCOL"), getenv("SERVER_SOFTWARE"));    return 0;}
#编译,并将demo部署到/var/www/cgi-bin/目录sudo g++ main.cpp -o demo -l fcgi#添加动态库路径/etc/ld.so.conf中添加fcgi的安装路径,如/usr/local/lib,并执行ldconfig更新一下sudo spawn-fcgi -f /var/www/cgi-bin/demo -a 127.0.0.1 -p 9000#出现的错误,spawn-fcgi: child exited with: 127#只在控制台运行了下export LD_LIBRARY_PATH=/usr/local/lib#spawn进程时找不到动态库,将export写入/etc/profile文件,或/etc/ld.so.conf添加动态库路径

nginx配置

location ~ ^/cgi-bin/.*\.cgi$ {    root /var/www;    fastcgi_pass  127.0.0.1:9000;    fastcgi_index index.cgi;    include fastcgi.conf;}

请求http://127.0.0.1/cgi-bin/index.cgi?key1=value1
这里写图片描述

nginx + cgi

nginx 不能直接执行外部可执行程序,并且cgi是接收到请求时才会启动cgi进程,不像fastcgi会在一开就启动好,这样nginx天生是不支持 cgi 的。nginx 虽然不支持cgi,但它支持 fastCGI。所以,我们可以考虑使用fastcgi包装来支持 cgi。原理大致如下图所示:pre-fork几个通用的代理fastcgi程序——fastcgi-wrapper,fastcgi-wrapper启动执行cgi然后将cgi的执行结果返回给nginx(fork-and-exec)。
这里写图片描述

安装fastcgi-wrapper

git clone https://github.com/gnosek/fcgiwrap.gitcd fcgiwrapautoreconf -i./configuremakemake install#启动fastcgi-wrappersudo spawn-fcgi -f /usr/local/sbin/fcgiwrap -p 9002
#include <stdio.h>#include <stdlib.h>int main(void){    int count = 0;    printf("Content-type: text/html\r\n"        "\r\n"        "<title>CGI Hello!</title>"        "<h1>CGI Hello!</h1>"        "Request number %d running on host <i>%s</i>\n",        ++count, getenv("SERVER_NAME"));    return 0;}
sudo g++ main2.cpp -o demo2.cgi   -l fcgi

这里写图片描述
如果出现403 Forbidden检查文件名是否对应

参照:

Nginx + CGI/FastCGI + C/Cpp
fastcgi
https://github.com/gnosek/fcgiwrap.git

0 0
原创粉丝点击