只有简单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. vscode编写插件

    vscode编写插件详细过程 前言 之前编写了一个vscode插件用vscode写博客和发布,然后有园友要求写一篇来介绍如何开发一个vscode扩展插件,或者说介绍开发这个插件的过程.然而文章还没有写 ...

  2. 450A - Jzzhu and Children 找规律也能够模拟

    挺水的一道题.规律性非常强,在数组中找出最大的数max,用max/m计算出倍数t,然后再把数组中的书都减去t*m,之后就把数组从后遍历找出第一个大于零的即可了 #include<iostream ...

  3. 不是技术牛人,如何拿到国内IT巨头的Offer(转)

    不久前,byvoid面阿里星计划的面试结果截图泄漏,引起无数IT屌丝的羡慕敬仰.看看这些牛人,NOI金牌,开源社区名人,三年级开始写Basic…在跪拜之余我们不禁要想,和这些牛人比,作为绝大部分技术屌 ...

  4. [C++]指针浅析

    Date: 2014-1-4 summary: 指针的简单理解,概念性的东西会比较多(100个人有100种理解,此处只代表我个人看法) Contents: 1.什么是指针 c++ primer plu ...

  5. Android中关于JNI 的学习(六)JNI中注冊方法的实现

    在前面的样例中,我们会发现,当在Java类中定义一个方法的时候,例如以下: public class ParamTransferTest { public static int testval = 1 ...

  6. 搭建自己的XenServer+CloudStack云平台,提供IaaS服务(一)环境搭建

    目标 搭建一个完整的基于XenServer和CloudStack的虚拟化平台,提供IaaS服务. 搭建三台安装了XenServer的服务器 搭建一台安装了CloudStack的服务器用以管理云平台 搭 ...

  7. java自己主动打开包装盒很容易导致两个误区

    从J2SE 5.0开始提供基本数据类型的自己主动装箱(autoboxing).拆箱(unboxing)功能. 何为自己主动装箱: 当我们创建一个Integer对象时,却能够这样: Integer i ...

  8. 在后台运行erlang;在需要时连回交互模式

    * 1. 启动后台运行的erlang环境 按以下命令: erl -detached -name a@127.0.0.1 注意,-name的值必须是xxxx@ip的形式.其中xxxx是英文名,ip必须是 ...

  9. 在MVC应用程序中动态加载PartialView

    原文:在MVC应用程序中动态加载PartialView 有时候,我们不太想把PartialView直接Render在Html上,而是使用jQuery来动态加载,或是某一个事件来加载.为了演示与做好这个 ...

  10. 性能测试开源小工具——http_load介绍

    淘测试 性能测试开源小工具——http_load介绍 meizhu 发表于:2009-07-02 浏览:3552次 评论:1次 所属分类: 性能测试 性能测试开源小工具——http_load介绍 ht ...