nginx fastcgi C++ 开发框架配置说明

来源:互联网 发布:大数据 数据挖掘 编辑:程序博客网 时间:2024/06/05 16:07

前言

网上一搜罗一大堆相关文章,之所以写明是原创是因为绝大多数文章都有细节问题,再则关于php较多,C++较少。
只是比较粗地说一下安装步骤,例如nginx.conf 格式 以及每个命令参数 并不需要去深究~

关于开发框架

1. nginx服务器只是负责转发请求,请求转发给自己开发的fastcgi服务程序处理。
2. spawn-fcgi 用于管理fastcgi应用程序,他是lighttpd的一子项目。
3. fastcgi 服务程序接收请求,处理然后 nginx 服务器负责将fastcgi 服务返回结果转发给客户端。
一个url:http://10.1.144.142:8090/fcgi_test?a=1
nginx应用程序看到的是http://10.1.144.142:8090/fcgi_test,根据fcgi_test  “location”字段将问号后面参数部分"a=1"转发给fastcgi服务处理。因此fastcgi服务程序看到的是”a=1“

nginx安装

1. nginx安装依赖于openssl,pcre两个库。
分别下载三个库:nginx-0.8.53 , openssl-1.0.1c , pcre-8.31 解压到相应位置,进入nginx文件夹,configure的时候需要指定两个依赖库的路径,blog1中的my_configure脚本:
./configure --prefix=/usr/local/nginx \      --with-pcre=/home/nginx_software/pcre-8.31 \      --with-openssl=/home/nginx_software/openssl-1.0.1c \      --with-http_stub_status_module \      --with-http_ssl_module
nginx的安装目录:/usr/local/nginx,pcre解压路径是/home/nginx_software/pcre-8.31
安装步骤如下:
sh my_configuremake && make install

2. 安装spawn-fcgi,安装lighttpd-1.4.19  

nginx配置

进入nginx安装目录 /usr/local/nginx/ 进入conf目录,打开nginx.conf配置文件,conf文件结构是server 监听不同端口,server下面有分location,在server内部添加如下code:
location /fcgi_test {            fastcgi_pass 127.0.0.1:9000;            include fastcgi_params;        }
表示接收到 /fcgi_test 类型的请求 即分配给 监听9000的fastcgi服务程序。

nginx 启动、重启

1. 启动直接 ./nginx 即可
2. 其他操作:
在usr/local/nginx/sbin/ 目录下 ./nginx -h 帮助有一行: 
-s signal : send signal to a master process: stop, quit, reopen, reload 
./nginx -s reopen 重启。

spawn-fcgi

拉起自己开发的fastcgi 服务程序,监听9000端口,然后处理请求。
blog1中 
/usr/local/nginx/sbin/spawn-fcgi -a 127.0.0.1 -p 9000 -C 25 -f /usr/local/nginx/myFastCGI/helloFastCGI -F 1000
-F 参数,实测不正确去掉即可。

fast-cgi服务程序开发

就是再简单不过的C++程序开发,使用第三方库fast-cgi,生成的时候需要在Makefile中指定include,lib目录。
参见blog2中的类似hello-fcgi的代码框架。

测试

本地调用curl "http:// 127.0.0.1:8090/fcgi_test?a=1"  
在客户端浏览器中键入 http://10.1.144.142:8090/fcgi_test?a=1
测试成功:

本人写的nginx服务器监听8090端口,没有使用默认端口,如果80端口可以不写端口号。

blog2中给出的Makefile 实测make时有错误,可采用此处的通用Makefile框架:http://blog.csdn.net/dizuo/article/details/7989411
TARGET改下服务名字即可。

参考:
1. blog1: http://www.cnblogs.com/xiaouisme/archive/2012/08/01/2618398.html
2. blog2: http://blog.csdn.net/yonghengkxh/article/details/8084460

原创粉丝点击