77 *
88 * Compile:
99 * shell> ./configure --add-module=/path/to/nginx_moudle_echo.c
10+ *
1011 */
1112
12- //定义模块配置结构
13+ #include <ngx_config.h>
14+ #include <ngx_core.h>
15+ #include <ngx_http.h>
16+
17+ //定义模块配置结构 命名规则为ngx_http_[module-name]_[main|srv|loc]_conf_t。其中main、srv和loc分别用于表示同一模块在三层block中的配置信息。
1318typedef struct {
1419 ngx_str_t ed ; //该结构体定义在这里 https://github.com/nginx/nginx/blob/master/src/core/ngx_string.h
15- } ngx_http_echo_loc_conf_t ;
20+ } ngx_http_echo_loc_conf_t ;
21+
22+ /*//定义指令 ngx_command_s定义在core/ngx_config_file.h中
23+ struct ngx_command_s {
24+ ngx_str_t name; //词条指令的名称
25+ ngx_uint_t type; //使用掩码标志位方式配置指令参数 具体参考https://github.com/nginx/nginx/blob/master/src/core/ngx_conf_file.h
26+ char *(*set)(ngx_conf_t *cf, ngx_command_t *cmd, void *conf); //set是一个函数指针 用于指导一个参数化函数 具体参考https://github.com/nginx/nginx/blob/master/src/core/ngx_conf_file.h
27+ ngx_uint_t conf; //指定Nginx相应配置文件内存其实地址,一般可以通过内置常量指定,如NGX_HTTP_LOC_CONF_OFFSET,offset指定此条指令的参数的偏移量。
28+ ngx_uint_t offset;
29+ void *post;
30+ };*/
31+
32+ //定义echo模块的指令
33+ static ngx_command_t ngx_http_echo_commands [] = {
34+ {ngx_string ("echo" ),
35+ NGX_HTTP_LOC_CONF | NGX_CONF_TAKE1 ,
36+ ngx_http_echo ,
37+ NGX_HTTP_LOC_CONF_OFFSET ,
38+ offsetof(ngx_http_echo_oc_conf_t , ed ),
39+ NULL ,
40+ },
41+ ngx_null_command ,
42+ };
43+
44+ //参数转化函数
45+ static char *
46+ ngx_http_echo (ngx_conf_t * f ,ngx_command_t * cmd , void * conf )
47+ {
48+ ngx_http_core_loc_conf_t * clcf ;
49+ clcf = ngx_http_conf_get_module_loc_conf (cf ,ngx_http_core_module );
50+ clcf -> handle = ngx_http_echo_handler ; //修改核心模块配置(也就是当前location),将其handler替换为我们自己定义的ngx_http_echo_handler
51+ ngx_conf_set_str_slot (cf ,cmf ,conf );
52+ return NGX_CONF_OK ;
53+ }
0 commit comments