FastCGI技术介绍

FastCGI技术介绍

1         FastCGI介绍

    FastCGI:快速通用网关接口(Fast Common Gateway Interface/FastCGI)是一种让交互程序与Web服务器通信的协议。
    FastCGI像是一个常驻(long-live)型的CGI,它可以一直执行着,只要激活后,不会每次都要花费时间去fork一次。它还支持分布式的运算, 即 FastCGI 程序可以在网站服务器以外的主机上执行并且接受来自其它网站服务器来的请求。
    FastCGI具有稳定,安全,高性能,方便扩展等优点。
    利用nginx、spawn-fcgi、fcgi就可搭建一个高并发高性能的FastCGI框架。本文将浅析框架的搭建过程。

2         FastCGI技术框架

3         FastCGI搭建

3.1 nginx安装配置

下载nginx源码
./configure \
--prefix=/usr/local/nginx \
--sbin-path=/usr/local/ nginx / nginx \
--conf-path=/usr/local/ nginx / nginx.conf \
--pid-path=/usr/local/ nginx /nginx.pid \
--error-log-path=/usr/local/ nginx /logs/error.log \
--with-pcre=/root/pcre-8.31 \
--with-zlib=/root/zlib-1.2.7 \
--with-http_dav_module \
--with-http_flv_module  \
--with-http_stub_status_module \
make && make install

nginx.conf的配置为:
daemon  on;

user  root root;

worker_processes  2;

worker_rlimit_nofile 204800;

 

events {

use epoll;

worker_connections 204800;

}

http {

    include       mime.types;

    default_type  application/octet-stream;

    charset utf-8;

    sendfile on;

    tcp_nopush     on;

    keepalive_timeout 60;

    server {

        listen       80;

        server_name  localhost;

        location / {

            root   html;

            index  index.html index.htm;

        }

        location /status {

            stub_status on;

            access_log on;

            allow all;

        }

        location /index.cgi {

        fastcgi_pass 127.0.0.1:9000;

        fastcgi_index index.cgi;

        fastcgi_param SCRIPT_FILENAME fcgi$fastcgi_script_name;

        fastcgi_param  REMOTE_ADDR        $remote_addr;

        fastcgi_param  REMOTE_PORT        $remote_port;

        fastcgi_param  SERVER_ADDR        $server_addr;

        fastcgi_param  SERVER_PORT        $server_port;

        fastcgi_param  SERVER_NAME        $server_name;

        include fastcgi_params;

        }           

        error_page   500 502 503 504  /50x.html;

        location = /50x.html {

         root   html;

        }

    }

}

3.2 CGI程序

CGI程序可以使用fcgi和cgicc库来实现。

fcgi源码下载:http://www.fastcgi.com/dist/fcgi.tar.gz 
tar –xzvf fcgi.tar.gz
./configure && make
fcgi-2.4.1/libfcgi/.libs 目录会生成:libfcgi.a libfcgi++.a

多线程CGI
参考examples/threaded.c
编译 threaded.c 生成CGI程序。

实现http重定向(http返回码 301 302)可使用cgicc库, cgicc是c++语言的CGI,cgicc支持fastcgi,有重定向的功能。
源码下载: ftp://ftp.gnu.org/gnu/cgicc/cgicc-3.2.9.tar.gz
cgicc与fcgi结合使用,可参考cgicc-3.2.9/contrib./fcgi-test.cpp。

3.3 spawn-fcgi

spawn-fcgi是lighttpd的一个子项目,能够fork多个CGI子进程,并调度完成nginx的fastcgi指令,作为CGI程序的调度管理器。
源码下载: http://www.lighttpd.net/download/spawn-fcgi-1.6.3.tar.gz
tar –xzvf spawn-fcgi-1.6.3.tar.gz
./configure && make 
cp ./src/spawn-fcgi  /root/ 
spawn-fcgi命令说明:
   -a <address>   bind to IPv4/IPv6 address (defaults to 0.0.0.0)
   -p <port>      bind to TCP-port
   -f <path>      filename of the fcgi-application
   -F <children>   number of children to fork (default 1)
注意:spawn-fcgi fork CGI程序以后,自己就退出了。

下面的命令的意思是,spawn-fcgi监听端口9000,并fork出10个CGI子进程。
/root/spawn-fcgi -a 127.0.0.1 -p 9000 -f /root/CGI  -F 10

4         FastCGI测试

4.1 环境配置

解决TCP TIME_WAIT过多的问题,vim /etc/sysctl.conf 加入以下内容:

net.ipv4.tcp_syncookies = 1

net.ipv4.tcp_tw_reuse = 1

net.ipv4.tcp_tw_recycle = 1

net.ipv4.tcp_fin_timeout = 30

/sbin/sysctl -p 让参数生效。

修改最多可打开的文件数: ulimit -n 10000

4.2 CGI进程管理器spawn-fcgi

/root/spawn-fcgi -a 127.0.0.1 -p 9000 -f /root/CGI  -F 10

4.3 安装webbench

webbench最多可以模拟3万个并发连接去测试服务器的负载能力,编译和配置简单,仅基于TCP协议上对服务器进行测试。

源码下载: http://blog.s135.com/soft/linux/webbench/webbench-1.5.tar.gz
tar zxvf webbench-1.5.tar.gz
cd webbench-1.5
make

4.4 测试用例

1分钟1000个客户端测试

[root@localhost webbench-1.5]# ./webbench -c 1000 -t 60 http://192.168.2.119:80/index.cgi

Webbench - Simple Web Benchmark 1.5

Copyright (c) Radim Kolar 1997-2004, GPL Open Source Software.

 

Benchmarking: GET http://192.168.39.56:80/index.cgi

1000 clients, running 60 sec.

 

Speed=864321 pages/min, 13456215 bytes/sec.

Requests: 864321 susceed, 0 failed.


转载地址: http://www.cnblogs.com/aHuner/archive/2013/05/27/3102423.html
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值