Nginx+FastCgi的测试


Nginx + Fastcgi配置方法

一、nginx安装&配置

1) apt-get install nginx

2) 修改/etc/nginx/sites-available下的default文件

[plain] view plaincopy
#设置首页
root /usr/share/nginx/myweb;
index index.html index.htm;
#修改端口
listen 8880 default_server;
listen [::]:8880 default_server ipv6only=on;
#设置fastcgi程序入口
location /mycgi.cgi {
fastcgi_pass 127.0.0.1:9999; -> fastcgi程序监听的端口
#fastcgi_index mycgi.out;
include fastcgi_params;
}

二、fastcgi管理器spawn-fcgi的安装

apt-get install spawn-fcgi

三、fcgi库的安装

没有找到官网下载,有些奇怪,临时参照附件。

1)修改include/fcgio.h文件,追加#include

2)./configure

3)./make install

4) ldconfig /usr/local/lib (libfcgi.so默认生成路径)

四、编译cgi程序

例子代码如下:

[cpp] view plaincopy#include
#include
#include
#include
#include
int main(int argc, char** argv)
{
int count = 0;
while( FCGI_Accept() >= 0 )
{
printf( "Content-type:text/html\r\n\r\n" );
printf( " Hello FastCGI ! " );
printf( " Request number = [%d]", ++count );
printf( " CGI PID: %d ", getpid() );
}
return 0;
}

编译

g++ mycgi.c -o mycgi.out -L /usr/local/lib -lfcgi

五、启动cgi

spawn-fcgi -a 127.0.0.1 -p 9999 -f /root/mycgi/mycgi.out -F 3

-F指定启动的cgi进程个数,nginx会轮询访问

访问http://server:8880即可看到cgi程序的输出。



相关内容