Skip to content

Commit ade2055

Browse files
committed
定义echo模块指令和参数转化函数的代码
1 parent c8a8fcf commit ade2055

File tree

2 files changed

+45
-3
lines changed

2 files changed

+45
-3
lines changed

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# nginx_module_echo
22
echo string
33

4-
# Nginxx Version
4+
# Nginx Version
55
Nginx1.0.10 https://github.com/nginx/nginx/releases/tag/release-1.0.10
66
![image](https://wx1.sinaimg.cn/large/005LOzcmly1fgimmvpk3sj30mi04p3z9.jpg)
77

@@ -35,3 +35,7 @@ typedef struct {
3535
} ngx_http_echo_loc_conf_t;
3636
```
3737
![image](https://wx2.sinaimg.cn/large/005LOzcmly1fgin4at3rsj30rp04g74r.jpg)
38+
39+
#Define Nginx instruction and parameter conversion function
40+
![image](https://wx1.sinaimg.cn/large/005LOzcmly1fgjdis37udj30xj0bktan.jpg)
41+

src/nginx_moudle_echo.c

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,47 @@
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中的配置信息。
1318
typedef 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

Comments
 (0)