FastCGI:快速通用網關接口(Fast Common Gateway Interface/FastCGI)是一種讓交互程序與Web服務器通訊的協議。
FastCGI像是一個常駐(long-live)型的CGI,它能夠一直執行着,只要激活後,不會每次都要花費時間去fork一次。它還支持分佈式的運算, 即 FastCGI 程序能夠在網站服務器之外的主機上執行而且接受來自其它網站服務器來的請求。
FastCGI具備穩定,安全,高性能,方便擴展等優勢。
利用nginx、spawn-fcgi、fcgi就可搭建一個高併發高性能的FastCGI框架。本文將淺析框架的搭建過程。html
下載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 installlinux
nginx.conf的配置爲:
daemon on;nginx
user root root;c++
worker_processes 2;web
worker_rlimit_nofile 204800;vim
events {安全
use epoll;服務器
worker_connections 204800;cookie
}多線程
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;
}
}
}
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。
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
解決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
/root/spawn-fcgi -a 127.0.0.1 -p 9000 -f /root/CGI -F 10
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
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.