您的位置:首页 > 理论基础 > 计算机网络

nginx的HTTP模块编写

2016-11-29 17:12 351 查看


开发HTTP模块流程

这里的HTTP模块是最简单最经常编写的模块,开发一个完整的简单的HTTP模块需要下面几个步骤(以模块名为ngx_http_mytest_module为例):


1 编写config文件(这是为了让nginx在configure过程能找到编写的模块)

下面是编写具体的模块代码结构


2 编写模块结构 ngx_http_mytest_module

这个是模块结构,其中起的作用是:

定义了模块的上下文结构

定义了模块命令结构


3 编写模块上下文结构 ngx_http_mytest_module_ctx

这个结构的意思就是nginx在触发了模块运行的时候,如何处理已经在其他http,server,location定义过的上下文


4 编写模块命令结构 ngx_http_mytest_commands

这个结构的意思就是nginx在配置文件中触发了哪些命令,其中指定了:

触发命令的回调函数


5 触发命令的回调函数 ngx_http_mytest

这个回调函数中可以设置对http请求的具体处理方法


6 对http请求的具体处理方法 ngx_http_mytest_handler

这个方法的参数中可以获取http请求结构,并且可以设置http返回

至此,一个http模块就可以完成了。


对应的各个步骤说明:


1 编写config文件

示例:

ngx_addon_name=ngx_http_mytest_module 

HTTP_MODULES="HTTPMODULESngxhttpmytestmodule"NGXADDONSRCS="HTTPMODULESngxhttpmytestmodule"NGXADDONSRCS="NGX_ADDON_SRCS
$ngx_addon_dir/ngx_http_mytest_module.c"

HTTP_MODULES是设置HTTP需要加载的模块列表,在具体编译的时候会生成modules的数组,然后根据数组的先后顺序一个一个加载


2 ngx_http_mytest_module的结构类型是ngx_module_t

它的结构说明看:

https://github.com/jianfengye/nginx-1.0.14_comment/blob/master/src/core/ngx_conf_file.h

里面的ngx_module_s的结构

最主要记得是要设置上下文结构ctx和命令集commands


3 某块上下文ngx_http_mytest_module_ctx的结构类型是ngx_http_module_t

它的结构说明看:

https://github.com/jianfengye/nginx-1.0.14_comment/blob/master/src/http/ngx_http_config.h

这个结构是如果需要的话在读取,重载配置文件的时候定义的8个阶段

create_main_conf 

create_srv_conf 

create_loc_conf 

preconfiguration 

init_main_conf 

merge_srv_conf 

merge_loc_conf 

postconfiguration


4 ngx_http_mytest_commands 是一个ngx_command_s的数组

ngx_command_s的结构说明看:

它的结构说明看:

https://github.com/jianfengye/nginx-1.0.14_comment/blob/master/src/core/ngx_conf_file.h

里面碰到的set回调函数,这个回调函数可以使用nginx预设的14个解析配置方法,或者使用自定义的方法

14个预设的解析配置方法有:

ngx_conf_set_flag_slot

ngx_conf_set_str_slot

ngx_conf_set_str_array_slot

ngx_conf_set_keyval_slot

ngx_conf_set_num_slot

ngx_conf_set_size_slog

ngx_conf_set_off_slot

ngx_conf_set_msec_slot

ngx_conf_set_sec_slot

ngx_conf_set_bufs_slot

ngx_conf_set_enum_slot

ngx_conf_set_bitmask_slot

ngx_conf_set_acccess_slot

ngx_conf_set_path_slot


5 触发命令的回调函数的解析配置方法格式如下:

char               *(*set)(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);

如果使用了上面的14个解析配置方法,就可以不用自己写这个方法了

如果是自己写这个配置解析方法,就需要写第六步

ngx_http_mytest_handler

它的函数定义如下:

static ngx_init_t ngx_http_mytest_handler(ngx_http_request_t *r)

使用ngx_http_request_t指针输入

在ngx_http_request指针中也可以设置HTTP返回

它的结构说明看:

https://github.com/jianfengye/nginx-1.0.14_comment/blob/master/src/http/ngx_http_request.h


一个具体的例子:

https://github.com/jianfengye/MyWorks/tree/master/nginx_module_mytest
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: