Skip to content

Commit c4281a0

Browse files
committed
组合Nginx模块
1 parent 0b475ec commit c4281a0

File tree

3 files changed

+37
-4
lines changed

3 files changed

+37
-4
lines changed

README.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Practical development Nginx
1+
# Practical development Nginx Module
22
**[简体中文](http://git.oschina.net/wujunze/nginx_module_echo/blob/master/README_zh.md)**
33
## nginx_module_echo
44
echo string
@@ -50,4 +50,7 @@ typedef struct {
5050
![image](https://wx3.sinaimg.cn/large/005LOzcmly1fgjes12fy5j30ya08qgn7.jpg)
5151

5252
## Write the Handler really work part of the module
53-
![image](https://ws2.sinaimg.cn/large/005LOzcmly1fgjfosnvf5j31hy0q6wlb.jpg)
53+
![image](https://ws2.sinaimg.cn/large/005LOzcmly1fgjfosnvf5j31hy0q6wlb.jpg)
54+
55+
## Combination Nginx module
56+
![image](https://ws2.sinaimg.cn/large/005LOzcmly1fgjjo2l11jj31en0g4gq1.jpg)

README_zh.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 实战开发一个Nginx拓展
1+
# 实战开发一个Nginx拓展 (Nginx Module)
22
## nginx_module_echo
33
使用echo指令输出一个字符串
44

@@ -49,4 +49,7 @@ typedef struct {
4949
![image](https://wx3.sinaimg.cn/large/005LOzcmly1fgjes12fy5j30ya08qgn7.jpg)
5050

5151
## 编写Handler 模块真正干活儿的部分
52-
![image](https://ws2.sinaimg.cn/large/005LOzcmly1fgjfosnvf5j31hy0q6wlb.jpg)
52+
![image](https://ws2.sinaimg.cn/large/005LOzcmly1fgjfosnvf5j31hy0q6wlb.jpg
53+
54+
## 组合Nginx Module
55+
![image](https://ws2.sinaimg.cn/large/005LOzcmly1fgjjo2l11jj31en0g4gq1.jpg)

src/nginx_moudle_echo.c

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,11 @@ ngx_http_echo_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
110110
* handler会接收一个ngx_http_request_t指针类型的参数,这个参数指向一个ngx_http_request_t结构体,此结构体存储了这次HTTP请求的一些信息,这个结构定义在https://github.com/nginx/nginx/blob/master/src/http/ngx_http_request.h中:
111111
*
112112
* 第一步是获取模块配置信息,这一块只要简单使用ngx_http_get_module_loc_conf就可以了。
113+
*
113114
* 第二步是功能逻辑,因为echo模块非常简单,只是简单输出一个字符串,所以这里没有功能逻辑代码。
115+
*
114116
* 第三步是设置response header。Header内容可以通过填充headers_out实现,我们这里只设置了Content-type和Content-length等基本内容,ngx_http_headers_out_t定义了所有可以设置的HTTP Response Header信息 这个结构体在https://github.com/nginx/nginx/blob/master/src/http/ngx_http_request.h 设置好头信息后使用ngx_http_send_header就可以将头信息输出,ngx_http_send_header接受一个ngx_http_request_t类型的参数。
117+
*
115118
* 第四步也是最重要的一步是输出Response body。 Nginx的I/O机制 Nginx允许handler一次产生一组输出,可以产生多次,Nginx将输出组织成一个单链表结构,链表中的每个节点是一个chain_t,定义在https://github.com/nginx/nginx/blob/master/src/core/ngx_buf.h
116119
* @param r ngx_http_request_t指针
117120
* @return
@@ -160,6 +163,30 @@ ngx_http_echo_handler(ngx_http_request_t *r)
160163
return ngx_http_output_filter(r, &out);
161164
}
162165

166+
/**
167+
* 组合Nginx Module
168+
*
169+
* 上面完成了Nginx模块各种组件的开发下面就是将这些组合起来了。一个Nginx模块被定义为一个ngx_module_t结构体https://github.com/nginx/nginx/blob/master/src/core/ngx_module.h,这个结构体的字段很多,不过开头和结尾若干字段一般可以通过Nginx内置的宏去填充
170+
*
171+
* 开头和结尾分别用NGX_MODULE_V1和NGX_MODULE_V1_PADDING 填充了若干字段,就不去深究了。
172+
* 这里主要需要填入的信息从上到下以依次为context、指令数组、模块类型以及若干特定事件的回调处理函数(不需要可以置为NULL),
173+
* 其中内容还是比较好理解的,注意我们的echo是一个HTTP模块,所以这里类型是NGX_HTTP_MODULE,其它可用类型还有NGX_EVENT_MODULE(事件处理模块)和NGX_MAIL_MODULE(邮件模块)。
174+
*
175+
*/
176+
ngx_module_t ngx_http_echo_module = {
177+
NGX_MODULE_V1,
178+
&ngx_http_echo_module_ctx, /* module context */
179+
ngx_http_echo_commands, /* module directives */
180+
NGX_HTTP_MODULE, /* module type */
181+
NULL, /* init master */
182+
NULL, /* init module */
183+
NULL, /* init process */
184+
NULL, /* init thread */
185+
NULL, /* exit thread */
186+
NULL, /* exit process */
187+
NULL, /* exit master */
188+
NGX_MODULE_V1_PADDING
189+
};
163190

164191

165192

0 commit comments

Comments
 (0)