只有简单subrequest应用演示示例。

nginx.conf文件:

#user  nobody;
worker_processes 1; #error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info; #pid logs/nginx.pid; events {
worker_connections 1024;
} http {
include mime.types;
default_type application/octet-stream; #log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"'; #access_log logs/access.log main;
sendfile on;
#tcp_nopush on; #keepalive_timeout 0;
keepalive_timeout 65; #gzip on; server {
listen 80;
server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / {
root html;
index index.html index.htm;
} # location /test2 {
# test_str "hello my dear HUST!";
# test_flag on;
# test_num 10;
# test_size 1000;
# mytest;
# }
location /list {
proxy_pass http://hq.sinajs.cn;
proxy_set_header Accept-Encoding "";
} location /query {
mytest;
}
# location /test2 {
# test_str "hello my dear ye.";
# test_flag on;
# test_num 10;
# test_size 1000;
# mytest;
# } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
} } server {
listen 80 default_server;
server_name www8.test.com ;
root /home/src/index;
index index.html;
location /list {
proxy_pass http://hq.sinajs.cn;
proxy_set_header Accept-Encoding "";
} location /query {
mytest;
} # location /test2 {
# test_str "hello my dear ye.";
# test_flag on;
# test_num 10;
# test_size 1000;
# mytest;
# } error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
} } }

config文件  注意与你写的模块放在同一个目录以下。

ngx_addon_name=ngx_http_mytest_module
HTTP_MODULES="$HTTP_MODULES ngx_http_mytest_module"
NGX_ADDON_SRCS="$NGX_ADDON_SRCS $ngx_addon_dir/ngx_http_mytest_subrequest_module.c"

ngx_http_mytest_subrequest_module.c文件

#include<ngx_http.h>
#include<ngx_config.h>
#include<ngx_core.h> static void mytest_post_handler(ngx_http_request_t *r);
static ngx_int_t mytest_subrequest_post_handler(ngx_http_request_t *r,void * data,ngx_int_t rc);
static ngx_int_t ngx_http_mytest_handler(ngx_http_request_t *r);
//save gupiao data
typedef struct {
ngx_str_t stock[6];
}ngx_http_mytest_ctx_t; static ngx_http_module_t ngx_http_mytest_module_ctx = {
NULL,NULL,
NULL,NULL,
NULL,NULL,
NULL,NULL
};
static char * ngx_http_mytest(ngx_conf_t *cf,ngx_command_t *cmd,void *conf){
ngx_http_core_loc_conf_t *clcf;
clcf = ngx_http_conf_get_module_loc_conf(cf,ngx_http_core_module);
clcf->handler = ngx_http_mytest_handler;
return NGX_CONF_OK;
} static ngx_command_t ngx_http_mytest_commands[] = {
{
ngx_string("mytest"),
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LMT_CONF|NGX_CONF_NOARGS,
ngx_http_mytest,
NGX_HTTP_LOC_CONF_OFFSET,
0,
NULL
},
ngx_null_command
}; ngx_module_t ngx_http_mytest_module = {
NGX_MODULE_V1,
&ngx_http_mytest_module_ctx,
ngx_http_mytest_commands,
NGX_HTTP_MODULE,
NULL,NULL,
NULL,NULL,
NULL,NULL,
NULL,
NGX_MODULE_V1_PADDING
}; //start subrequest
static ngx_int_t ngx_http_mytest_handler(ngx_http_request_t *r){
//creat HTTP ctx
ngx_http_mytest_ctx_t *myctx = ngx_http_get_module_ctx(r,ngx_http_mytest_module);
if(myctx == NULL){
myctx = ngx_palloc(r->pool,sizeof(ngx_http_mytest_ctx_t));
if(myctx == NULL) return NGX_ERROR;
ngx_http_set_ctx(r,myctx,ngx_http_mytest_module);
}
ngx_http_post_subrequest_t *psr = ngx_palloc(r->pool,sizeof(ngx_http_post_subrequest_t));
if(psr == NULL) return NGX_HTTP_INTERNAL_SERVER_ERROR;
psr->handler = mytest_subrequest_post_handler;
psr->data = myctx; ngx_str_t sub_prefix = ngx_string("/list=");
ngx_str_t sub_location;
sub_location.len = sub_prefix.len + r->args.len;
sub_location.data = ngx_palloc(r->pool,sub_location.len);
ngx_snprintf(sub_location.data,sub_location.len,"%V%V",&sub_prefix,&r->args);
ngx_http_request_t *sr;
ngx_int_t rc = ngx_http_subrequest(r,&sub_location,NULL,&sr,psr,NGX_HTTP_SUBREQUEST_IN_MEMORY);
if(rc != NGX_OK) return NGX_ERROR;
return NGX_DONE;
}
// subrequest end handler
static ngx_int_t mytest_subrequest_post_handler(ngx_http_request_t *r,void *data,ngx_int_t rc){
ngx_http_request_t *pr = r->parent;
ngx_http_mytest_ctx_t *myctx = ngx_http_get_module_ctx(pr,ngx_http_mytest_module);
pr->headers_out.status = r->headers_out.status;
if(r->headers_out.status == NGX_HTTP_OK){
int flag = 0;
ngx_buf_t *pRecvBuf = &r->upstream->buffer;
for(;pRecvBuf->pos != pRecvBuf->last;pRecvBuf->pos++){
if(*pRecvBuf->pos == ','||*pRecvBuf->pos == '\"'){
if(flag>0) myctx->stock[flag-1].len = pRecvBuf->pos - myctx->stock[flag-1].data;
flag++;
myctx->stock[flag-1].data = pRecvBuf->pos + 1;
} if(flag>6) break;
}
}
pr->write_event_handler = mytest_post_handler;
return NGX_OK;
}
static void mytest_post_handler(ngx_http_request_t *r){
if(r->headers_out.status != NGX_HTTP_OK){
ngx_http_finalize_request(r,r->headers_out.status);
return;
}
ngx_http_mytest_ctx_t *myctx = ngx_http_get_module_ctx(r,ngx_http_mytest_module);
ngx_str_t output_format = ngx_string("stock[%V],Today current price: %V,volum: %V");
int bodylen = output_format.len + myctx->stock[0].len + myctx->stock[1].len+myctx->stock[4].len - 6;
r->headers_out.content_length_n = bodylen;
ngx_buf_t *b = ngx_create_temp_buf(r->pool,bodylen);
ngx_snprintf(b->pos,bodylen,(char*)output_format.data,&myctx->stock[0],&myctx->stock[1],&myctx->stock[4]);
b->last = b->pos + bodylen;
b->last_buf = 1;
ngx_chain_t out;
out.buf = b;
out.next = NULL;
static ngx_str_t type = ngx_string("text/plain; charset=GBK");
r->headers_out.content_type = type;
r->headers_out.status = NGX_HTTP_OK;
r->connection->buffered |= NGX_HTTP_WRITE_BUFFERED;
ngx_int_t ret = ngx_http_send_header(r);
ret = ngx_http_output_filter(r,&out);
ngx_http_finalize_request(r,ret);
}

输入您的浏览器。 http://localhost/query?s_sh000001    ,当天的交易信息将显示。

无话可说。由于需要人。自然就明白了。

nginx subrequest演示示例程序的更多相关文章

  1. [演示示例程序]Objective-C受委托的设计模式(牛仔女孩)

    今天整理电脑打开一次自我Objective-C当编写一个实践设计模式委托一个小程序,在po快来分享.也复习一下OC中的托付. Objective-C中的托付设计模式是和协议分不开的. 协议呢.就是使用 ...

  2. 通过Jexus 部署 dotnetcore版本MusicStore 示例程序

    ASPNET Music Store application 是一个展示最新的.NET 平台(包括.NET Core/Mono等)上使用MVC 和Entity Framework的示例程序,本文将展示 ...

  3. 如何在Nginx下配置PHP程序环境

    1.nginx与PHP的关系 首先来看nginx与php的关系, FastCGI的关系(而不是像apache那样安装成nginx的模块) FastCGI的意思是, 快速的通用网关接口:CGI Comm ...

  4. OSG中的示例程序简介

    OSG中的示例程序简介 转自:http://www.cnblogs.com/indif/archive/2011/05/13/2045136.html 1.example_osganimate一)演示 ...

  5. LTE Module User Documentation(翻译15)——示例程序、参考场景以及故障检测和调试技巧

    LTE用户文档 (如有不当的地方,欢迎指正!)     21 Examples Programs(示例程序)   路径 src/lte/examples/ 包含一些示例仿真程序,这些例子表明如何仿真不 ...

  6. OSG中的示例程序简介(转)

    OSG中的示例程序简介 1.example_osganimate一)演示了路径动画的使用 (AnimationPath.AnimationPathCallback),路径动画回调可以作用在Camera ...

  7. java 添加一个线程、创建响应的用户界面 。 演示示例代码

    javajava 添加一个线程.创建响应的用户界面 . 演示示例代码 来自thinking in java 4 21章  部分的代码  夹21.2.11 thinking in java 4免费下载: ...

  8. 部署Bookinfo示例程序详细过程和步骤(基于Kubernetes集群+Istio v1.0)

    部署Bookinfo示例程序详细过程和步骤(基于Kubernetes集群+Istio v1.0) 部署Bookinfo示例程序   在下载的Istio安装包的samples目录中包含了示例应用程序. ...

  9. Nginx:subrequest的使用方式

    参考资料<深入理解Nginx> subrequest是由HTTP框架提供的一种分解复杂请求的设计模式. 它可以把原始请求分解为许多子请求,使得诸多请求协同完成一个用户请求,并且每个请求只关 ...

随机推荐

  1. windows socket----select模型

    一般我们的网络编程都是用bind ,listen,accept,send/sendto,recv/recvfrom.在创建套接字的时候,是默认使用阻塞模式的,每当我们调用send/sendto等方法时 ...

  2. Swift - 给表格添加编辑功能(删除,插入)

    1,下面的样例是给表格UITableView添加编辑功能: (1)给表格添加长按功能,长按后表格进入编辑状态 (2)在编辑状态下,第一个分组处于删除状态,第二个分组处于插入状态 (3)点击删除图标,删 ...

  3. 系统没有“internet信息服务(IIS)管理器”

    系统没有“internet信息服务(IIS)管理器” | 浏览:8981 | 更新:2014-06-19 14:43 1 2 3 4 5 6 7 分步阅读 很多用户都在咨询:系统控制面板的管理工具中没 ...

  4. Bootstrap表格的使用

    先定义前端table <table class="table table-striped table-bordered table-hover" id="expan ...

  5. ThinkPhp学习07

    原文:ThinkPhp学习07 简单CRUD操作 public function show() { $m=M('User'); // $arr=$m->find(2); //查找id=2的数据, ...

  6. poj 2409+2154+2888(Burnside定理)

    三道burnside入门题: Burnside定理主要理解置换群置换后每种不动点的个数,然后n种不动点的染色数总和/n为answer. 对于旋转,旋转i个时不动点为gcd(n,i). 传送门:poj ...

  7. 用Python对体积较大的CSV文件进行比较的经验

    用Python对体积较大的CSV文件进行比较的经验 » 进化的测试 | 进化的测试 用Python对体积较大的CSV文件进行比较的经验  python  Add comments 八 032010   ...

  8. WCF(1)----服务创建

    本例中,我们通过一个关于Camera的服务为例子来说明WCF的开发流程,该服务比较简单,只是用来实现对Camera的添加,枚举,删除等操作. 详细步骤如下: 1:创建一个WCF Service Lib ...

  9. Wi-Fi万能钥匙:说是破解,其实有危险(转)

    Wi-Fi 万能钥匙如此危险,怎样做才能让这种可能严重侵害公众利益的 app 在中国消失? 这个“钥匙”为什么能够破解 Wi-Fi?它真的是“破解” Wi-Fi 吗?两年前我就有这个疑问了,原谅我对一 ...

  10. Andriod中绘(画)图----Canvas的使用具体解释

    转载请注明出处:http://blog.csdn.net/qinjuning     因为在网络上找到关于Canvas的使用都比較抽象,或许是我的逻辑思维不太好吧,总是感觉理解起来比較困难, 尤其是对 ...