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框架提供的一种分解复杂请求的设计模式. 它可以把原始请求分解为许多子请求,使得诸多请求协同完成一个用户请求,并且每个请求只关 ... 
随机推荐
- flexbox语法(摘抄)
			flex 容器上的属性: flex-direction: row | row-reverse | column | column-reverse (项目的排列方向) flex-wrap : nowra ... 
- 与众不同 windows phone (9) - Push Notification(推送通知)之概述, 推送 Toast 通知
			原文:与众不同 windows phone (9) - Push Notification(推送通知)之概述, 推送 Toast 通知 [索引页][源码下载] 与众不同 windows phone ( ... 
- Linux入门基础#2:Linux文件系统基本结构
			本文出自 http://blog.csdn.net/shuangde800 ------------------------------------------------------------ ... 
- 1038. Recover the Smallest Number (30) - 字符串排序
			题目例如以下: Given a collection of number segments, you are supposed to recover the smallest number from ... 
- 利用SQL语句实现分页
			1.概述 在网页中如果显示的数据太多就会占据过多的页面,而且显示速度也会很慢.为了控制每次在页面上显示数据的数量,就可以利用分页来显示数据. 2.技术要点 在SQL Server中要实现SQL分页,需 ... 
- 与众不同 windows phone (24) - Input(输入)之软键盘类型, XNA 方式启动软键盘, UIElement 的 Touch 相关事件, 触摸涂鸦
			原文:与众不同 windows phone (24) - Input(输入)之软键盘类型, XNA 方式启动软键盘, UIElement 的 Touch 相关事件, 触摸涂鸦 [索引页][源码下载] ... 
- Dom4j和Xpath(转)
			1.DOM4J简介 DOM4J是 dom4j.org 出品的一个开源 XML 解析包.DOM4J应用于 Java 平台,采用了 Java 集合框架并完全支持 DOM,SAX 和JAXP. DOM4J使 ... 
- centos下chm阅读器
			xchm kchmviewer chmsee 
- js中substring或split方法取得URL中的域名
			1.split方式 <html> <head></head> <body onload="convertTemp()"> <s ... 
- HNCU1099:堆积木
			http://hncu.acmclub.com/index.php?app=problem_title&id=111&problem_id=1099 题目描述 小明的侄子要过生日了,小 ... 
