nginx开发学习——handler模块(二)
来源:互联网 发布:淘宝限时抢在主页哪里 编辑:程序博客网 时间:2024/05/22 01:29
前面阐述了handler模块的基本概念,这里提供一个handler模块实例,进一步学习。
1、首先包含头文件
#include <ngx_config.h>#include <ngx_core.h>#include <ngx_http.h>
2、模块的配置结构
typedef struct{ ngx_str_t hello_string; ngx_int_t hello_counter;}ngx_http_hello_loc_conf_t;
该模块有两个配置项,分别是字符串型的hello_string和整形的hello_counter
3、模块的指令集结构
static ngx_command_t ngx_http_hello_commands[] = { { ngx_string("hello_string"), NGX_HTTP_LOC_CONF|NGX_CONF_NOARGS|NGX_CONF_TAKE1, ngx_http_hello_string, NGX_HTTP_LOC_CONF_OFFSET, offsetof(ngx_http_hello_loc_conf_t, hello_string), NULL }, { ngx_string("hello_counter"), NGX_HTTP_LOC_CONF|NGX_CONF_FLAG, ngx_http_hello_counter, NGX_HTTP_LOC_CONF_OFFSET, offsetof(ngx_http_hello_loc_conf_t, hello_counter), NULL }, ngx_null_command};指明了两个配置项的名称以及配置项的处理,最后要用ngx_null_command结尾
hello_string的处理
static char *ngx_http_hello_string(ngx_conf_t *cf, ngx_command_t *cmd, void *conf){ ngx_http_hello_loc_conf_t* local_conf; local_conf = conf; char* rv = ngx_conf_set_str_slot(cf, cmd, conf); ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "hello_string:%s", local_conf->hello_string.data); return rv;}其中cf为nginx.conf中对应该配置名称的配置信息,cmd为ngx_http_hello_command_t结构体,conf为ngx_http_hello_conf_t,而nginx提供了一系列函数来处理从cf到conf的转化,具体说明如下:
ngx_conf_set_flag_slot: 读取NGX_CONF_FLAG类型的参数。
ngx_conf_set_str_slot:读取字符串类型的参数。
ngx_conf_set_str_array_slot: 读取字符串数组类型的参数。
ngx_conf_set_keyval_slot: 读取键值对类型的参数。
ngx_conf_set_num_slot: 读取整数类型(有符号整数ngx_int_t)的参数。
ngx_conf_set_size_slot:读取size_t类型的参数,也就是无符号数。
ngx_conf_set_off_slot: 读取off_t类型的参数。
ngx_conf_set_msec_slot: 读取毫秒值类型的参数。
ngx_conf_set_sec_slot: 读取秒值类型的参数。
ngx_conf_set_bufs_slot: 读取的参数值是2个,一个是buf的个数,一个是buf的大小;例如: output_buffers 1 128k;
ngx_conf_set_enum_slot: 读取枚举类型的参数,将其转换成整数ngx_uint_t类型。
ngx_conf_set_bitmask_slot: 读取参数的值,并将这些参数的值以bit位的形式存储
注:其实在ngx_http_hello_command_t的set函数位置,同样可以写入以上这些函数,直接读取配置信息
hello_count的处理
static char *ngx_http_hello_counter(ngx_conf_t *cf, ngx_command_t *cmd, void *conf){ ngx_http_hello_loc_conf_t* local_conf; local_conf = conf; char* rv = NULL; rv = ngx_conf_set_flag_slot(cf, cmd, conf); ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "hello_counter:%d", local_conf->hello_counter); return rv;}
4、模块上下文结构
static ngx_http_module_t ngx_http_hello_module_ctx = { NULL, /* preconfiguration */ ngx_http_hello_init, /* postconfiguration */ NULL, /* create main configuration */ NULL, /* init main configuration */ NULL, /* create server configuration */ NULL, /* merge server configuration */ ngx_http_hello_create_loc_conf, /* create location configuration */ NULL /* merge location configuration */};其中ngx_http_hello_init为模块加载后调用,主要用来挂载handler的主要处理函数
static ngx_int_tngx_http_hello_init(ngx_conf_t *cf){ ngx_http_handler_pt *h; ngx_http_core_main_conf_t *cmcf; cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module); h = ngx_array_push(&cmcf->phases[NGX_HTTP_CONTENT_PHASE].handlers); if (h == NULL) { return NGX_ERROR; } *h = ngx_http_hello_handler; return NGX_OK;}处理函数挂载在哪个阶段就是通过
h = ngx_array_push(&cmcf->phases[NGX_HTTP_CONTENT_PHASE].handlers)实现的,cmcf应该是模块的核心数据结构,NGX_HTTP_CONTENT_PHASE为挂载的阶段,可变化挂载到其他阶段
创建模块loc配置结构体处理函数
static void *ngx_http_hello_create_loc_conf(ngx_conf_t *cf){ ngx_http_hello_loc_conf_t* local_conf = NULL; local_conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_hello_loc_conf_t)); if (local_conf == NULL) { return NULL; } ngx_str_null(&local_conf->hello_string); local_conf->hello_counter = NGX_CONF_UNSET; return local_conf;}主要就是申请内存并且进行loc配置的初始化工作
5、ngx模块
ngx_module_t ngx_http_hello_module = { NGX_MODULE_V1, &ngx_http_hello_module_ctx, /* module context */ ngx_http_hello_commands, /* module directives */ NGX_HTTP_MODULE, /* module type */ NULL, /* init master */ NULL, /* init module */ NULL, /* init process */ NULL, /* init thread */ NULL, /* exit thread */ NULL, /* exit process */ NULL, /* exit master */ NGX_MODULE_V1_PADDING};
6、主要处理函数
static ngx_int_tngx_http_hello_handler(ngx_http_request_t *r)
留待自己去实现,完成功能。
- nginx开发学习——handler模块(二)
- nginx开发学习——handler模块(一)
- Nginx开发从入门到精通(二):handler模块
- nginx开发学习——过滤模块
- nginx模块开发之handler函数
- nginx模块开发学习
- Nginx模块开发(二)(
- nginx 源码学习笔记(二)——nginx精粹-模块
- nginx 源码学习笔记(二)——nginx精粹-模块
- nginx 源码学习笔记(二)——nginx精粹-模块
- nginx 源码学习笔记(二十二)—— event 模块(三) ——epoll模块
- nginx 源码学习笔记(二十二)—— event 模块(三) ——epoll模块
- nginx handler模块
- Nginx模块开发指南(Emiller) 二
- Nginx模块开发—数组模型
- Nginx模块开发—队列模型
- Nginx模块开发 —进程模型
- nginx 模块开发 lua开发学习
- D3.js学习笔记(三)——创建基于数据的SVG元素
- 触摸事件处理
- HTML:介绍几个不常用的HTML标签
- [Digital][Hardware][Camera]CX: A Geeks Camera System[VCSA][2015.01.06]
- 动态生成文本框 input type="text"
- nginx开发学习——handler模块(二)
- 使用WakeLock使Android应用程序保持后台唤醒
- iOS8 Size Classes初探
- thinkphp 捕获异常 try catch
- mac 系统开发android,真机调试解决方案(无数的坑之后吐血总结)
- 详细介绍一下Anchor标记
- (4) Linux ----- Shell编程输入输出 ---- ②
- java.lang.OutOfMemoryError: Java heap space解决方法
- yii2gii之CURD使用,功能不要太强大