nginx通过spawn-fcgi调用C++写的cgi程序

时间:2022-02-15 04:57:51

通过apt-get install 安装nginx和spawn-fcgi

/usr/local/nginx/sbin/spawn-fcgi -a 127.0.0.1 -p 9002 -C 25 -f /usr/local/nginx/cgibin/lzgFastCGI

参数含义如下:

-f 指定调用FastCGI的进程的执行程序位置,根据系统上所装的PHP的情况具体设置
-a 绑定到地址addr
-p 绑定到端口port
-s 绑定到unix socket的路径path
-C 指定产生的FastCGI的进程数,默认为5(仅用于PHP)
-P 指定产生的进程的PID文件路径
-u和-g FastCGI使用什么身份(-u 用户 -g用户组)运行,Ubuntu下可以使用www-data,其他的根据情况配置,如nobody、apache等

 

配置nginx.conf

location ~ ^/charge-cgi/.*$ {


location ~ charge-cgi$ {

# With php5-cgi alone:
fastcgi_pass 127.0.0.1:9000;
# With php5-fpm:
#fastcgi_pass unix:/var/run/php5-fpm.sock;

if ($document_uri ~ "^/fbtestadmin/(.*)") {
set $apache_path_info /$1;
}
fastcgi_param PATH_INFO $apache_path_info;
fastcgi_param GATEWAY_INTERFACE CGI/1.1;
fastcgi_param SERVER_SOFTWARE nginx;
fastcgi_param QUERY_STRING $query_string;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_param REQUEST_URI $request_uri;
fastcgi_param DOCUMENT_URI $document_uri;
fastcgi_param DOCUMENT_ROOT $document_root;
fastcgi_param SERVER_PROTOCOL $server_protocol;
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;

}

 

 

使用curl测试:

curl -d "param1=value1&param2=value2" "http://192.168.15.226/charge-cgi" -G

curl -d "param1=value1&param2=value2" "http://192.168.15.226/charge-cgi" 

 

 

reference:

http://redmine.webtoolkit.eu/boards/2/topics/6065

http://wiki.nginx.org/FcgiExample

http://wiki.nginx.org/NginxHttpCoreModule#location