nginx subrequest演示示例程序
只有简单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演示示例程序的更多相关文章
- [演示示例程序]Objective-C受委托的设计模式(牛仔女孩)
今天整理电脑打开一次自我Objective-C当编写一个实践设计模式委托一个小程序,在po快来分享.也复习一下OC中的托付. Objective-C中的托付设计模式是和协议分不开的. 协议呢.就是使用 ...
- 通过Jexus 部署 dotnetcore版本MusicStore 示例程序
ASPNET Music Store application 是一个展示最新的.NET 平台(包括.NET Core/Mono等)上使用MVC 和Entity Framework的示例程序,本文将展示 ...
- 如何在Nginx下配置PHP程序环境
1.nginx与PHP的关系 首先来看nginx与php的关系, FastCGI的关系(而不是像apache那样安装成nginx的模块) FastCGI的意思是, 快速的通用网关接口:CGI Comm ...
- OSG中的示例程序简介
OSG中的示例程序简介 转自:http://www.cnblogs.com/indif/archive/2011/05/13/2045136.html 1.example_osganimate一)演示 ...
- LTE Module User Documentation(翻译15)——示例程序、参考场景以及故障检测和调试技巧
LTE用户文档 (如有不当的地方,欢迎指正!) 21 Examples Programs(示例程序) 路径 src/lte/examples/ 包含一些示例仿真程序,这些例子表明如何仿真不 ...
- OSG中的示例程序简介(转)
OSG中的示例程序简介 1.example_osganimate一)演示了路径动画的使用 (AnimationPath.AnimationPathCallback),路径动画回调可以作用在Camera ...
- java 添加一个线程、创建响应的用户界面 。 演示示例代码
javajava 添加一个线程.创建响应的用户界面 . 演示示例代码 来自thinking in java 4 21章 部分的代码 夹21.2.11 thinking in java 4免费下载: ...
- 部署Bookinfo示例程序详细过程和步骤(基于Kubernetes集群+Istio v1.0)
部署Bookinfo示例程序详细过程和步骤(基于Kubernetes集群+Istio v1.0) 部署Bookinfo示例程序 在下载的Istio安装包的samples目录中包含了示例应用程序. ...
- Nginx:subrequest的使用方式
参考资料<深入理解Nginx> subrequest是由HTTP框架提供的一种分解复杂请求的设计模式. 它可以把原始请求分解为许多子请求,使得诸多请求协同完成一个用户请求,并且每个请求只关 ...
随机推荐
- JVM学习笔记(一)------的基本结构
从Java视台的逻辑结构.我们能够从图中学习JVM: 您可以从图表清楚地看到Java该平台包括各种逻辑模块.还可以学习JDK与JRE差异 于JVM它们的物理结构,我们可以看一下从图计划: 为JVM学习 ...
- 选择屏幕中的下拉框和dialog中下拉框设计
REPORT YTEST014. PARAMETERS: auart LIKE vapma-auart AS LISTBOX VISIBLE LENGTH 6. AT SELECTION-SC ...
- c++ try throw catch
c++ try throw catch 这三者联合使用 , try { statement list; } catch( typeA arg ) { statement list; } catch( ...
- Ajax请求URL后加随机数原理
原文:Ajax请求URL后加随机数原理 例如: $.ajax({ type: "GET", url: "login.action?ran=& ...
- expect实现ssh自动登录
expect实现ssh自动登录 #!/usr/local/bin/expect set PASSWD [lindex $argv 1] set IP [lindex $argv 0] set CM ...
- html name id, 与服务器交互必须有name
html name id, 与服务器交互必须有name 在HTML中元素的ID和Name的区别和联系. 今天写了个测试,在php脚本里怎么也获取不到$_POST['userName']的值,经检查在h ...
- 02将代码开源到github(不会使用github的来看看吧)
github不多说了,新建一个repository.如图: 这个创建好了之后,我们在eclipse中新建项目WeatherPro,安装githubclient. 安装好了之后,打开git bash,进 ...
- hdu 5115 Dire Wolf
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5115 题目分类:区间dp 题意:有n只狼,每只狼有两种属性,一种攻击力一种附加值,我们没杀一只狼,那么 ...
- sort如何按指定的列排序·百家电脑学院
sort如何按指定的列排序·百家电脑学院 sort如何按指定的(9php.com)列排序 0000 27189 41925425065f ...
- scala 函数编程
scala 函数编程 Effective Scala.pdf: http://www.t00y.com/file/76767869 Functional_Programming_in_Scal ...