Skip to content

Commit 0de8375

Browse files
committed
安装模块 并且运行成功 补充开发文档
1 parent bcb1b5a commit 0de8375

File tree

4 files changed

+59
-24
lines changed

4 files changed

+59
-24
lines changed

README.md

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Practical development Nginx Module
1+
# Practical development ginx Module
22
**[简体中文](http://git.oschina.net/wujunze/nginx_module_echo/blob/master/README_zh.md)**
33
## nginx_module_echo
44
echo string
@@ -55,6 +55,28 @@ typedef struct {
5555
## Combination Nginx module
5656
![image](https://ws2.sinaimg.cn/large/005LOzcmly1fgjjo2l11jj31en0g4gq1.jpg)
5757

58+
## Finishing module code According to the Nginx official specification
59+
![image](https://ws2.sinaimg.cn/large/005LOzcmly1fgjnxy9ikvj31yx12jwr3.jpg)
60+
61+
## Write the config file
62+
```shell
63+
ngx_addon_name=ngx_http_echo_module
64+
HTTP_MODULES="$HTTP_MODULES ngx_http_echo_module"
65+
NGX_ADDON_SRCS="$NGX_ADDON_SRCS $ngx_addon_dir/src/ngx_http_echo_module.c"
66+
```
67+
## make && make install
68+
```shell
69+
./configure --prefix=/usr/local/nginx/ --add-module=/root/ngx_dev && make && make install
70+
```
71+
## Successful installation
72+
![image](https://ws3.sinaimg.cn/large/005LOzcmly1fgjnvxyqx4j30w40f0tbw.jpg)
73+
74+
## Modify the Nginx configuration file test Module
75+
![image](https://ws2.sinaimg.cn/large/005LOzcmly1fgjnz17rh7j30jl07yaai.jpg)
76+
77+
## Nginx echo Module The successful running
78+
![image](https://ws2.sinaimg.cn/large/005LOzcmly1fgjo0r1a5dj30yf06tmxv.jpg)
79+
5880
>reference
5981
[1] Evan Miller, Emiller's Guide To Nginx Module Development. http://www.evanmiller.org/nginx-modules-guide.html, 2009
6082
[2] http://wiki.nginx.org/Configuration

README_zh.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,28 @@ typedef struct {
5454
## 组合Nginx Module
5555
![image](https://ws2.sinaimg.cn/large/005LOzcmly1fgjjo2l11jj31en0g4gq1.jpg)
5656

57+
## 整理模块代码 按照Nginx官方规范
58+
![image](https://ws2.sinaimg.cn/large/005LOzcmly1fgjnxy9ikvj31yx12jwr3.jpg)
59+
60+
## 编写config文件
61+
```shell
62+
ngx_addon_name=ngx_http_echo_module
63+
HTTP_MODULES="$HTTP_MODULES ngx_http_echo_module"
64+
NGX_ADDON_SRCS="$NGX_ADDON_SRCS $ngx_addon_dir/src/ngx_http_echo_module.c"
65+
```
66+
## 编译安装echo模块
67+
```shell
68+
./configure --prefix=/usr/local/nginx/ --add-module=/root/ngx_dev && make && make install
69+
```
70+
## 安装成功
71+
![image](https://ws3.sinaimg.cn/large/005LOzcmly1fgjnvxyqx4j30w40f0tbw.jpg)
72+
73+
## 修改Nginx配置文件测试Module
74+
![image](https://ws2.sinaimg.cn/large/005LOzcmly1fgjnz17rh7j30jl07yaai.jpg)
75+
76+
## Nginx echo Module 运行成功
77+
![image](https://ws2.sinaimg.cn/large/005LOzcmly1fgjo0r1a5dj30yf06tmxv.jpg)
78+
5779
>参考文献
5880
[1] Evan Miller, Emiller's Guide To Nginx Module Development. http://www.evanmiller.org/nginx-modules-guide.html, 2009
5981
[2] http://wiki.nginx.org/Configuration

config

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
ngx_addon_name=ngx_http_echo_module
2+
HTTP_MODULES="$HTTP_MODULES ngx_http_echo_module"
3+
NGX_ADDON_SRCS="$NGX_ADDON_SRCS $ngx_addon_dir/src/ngx_http_echo_module.c"

src/nginx_moudle_echo.c renamed to src/ngx_http_echo_module.c

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
/*
2-
* @file: nginx_moufle_echo.c
2+
* @file: nginx_http_echo_module.c
33
* @brief: Nginx echo command output a string
4-
* @author: Panda <vozlt@vozlt.com>
4+
* @author: Panda <itwujunze@163.com>
55
* @version: 1.0.1
66
* @date: 2017/6/12
77
*
88
* Compile:
9-
* shell> ./configure --add-module=/path/to/nginx_moudle_echo.c
9+
* shell> ./configure --add-module=/path/to/nginx_http_echo_module.c
1010
*
1111
*/
1212

@@ -34,7 +34,7 @@ static ngx_command_t ngx_http_echo_commands[] = {
3434
NGX_HTTP_LOC_CONF | NGX_CONF_TAKE1,
3535
ngx_http_echo,
3636
NGX_HTTP_LOC_CONF_OFFSET,
37-
offsetof(ngx_http_echo_oc_conf_t, ed),
37+
offsetof(ngx_http_echo_loc_conf_t, ed),
3838
NULL,
3939
},
4040
ngx_null_command,
@@ -123,11 +123,11 @@ ngx_http_echo_handler(ngx_http_request_t *r)
123123
b = ngx_pcalloc(r->pool, sizeof(ngx_buf_t));
124124
if(b == NULL)
125125
{
126-
ngx_log_error(NGX_LOG_ERROR, r->connection->log, 0, "Failed to allocate response buffer.");
126+
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "Failed to allocate response buffer.");
127127
return NGX_HTTP_INTERNAL_SERVER_ERROR;
128128
}
129129
out.buf = b;
130-
put.next = NULL;
130+
out.next = NULL;
131131
b->pos = elcf->ed.data;
132132
b->last = elcf->ed.data + (elcf->ed.len);
133133
b->memory = 1;
@@ -148,12 +148,12 @@ ngx_http_echo_handler(ngx_http_request_t *r)
148148
* @return ngx status code
149149
*/
150150
static char *
151-
ngx_http_echo(ngx_conf_t *f,ngx_command_t *cmd , void *conf)
151+
ngx_http_echo(ngx_conf_t *cf,ngx_command_t *cmd , void *conf)
152152
{
153153
ngx_http_core_loc_conf_t *clcf;
154-
clcf = ngx_http_conf_get_module_loc_conf(cf,ngx_http_core_module);
155-
clcf->handle = ngx_http_echo_handler; //修改核心模块配置(也就是当前location),将其handler替换为我们自己定义的ngx_http_echo_handler
156-
ngx_conf_set_str_slot(cf,cmf,conf);
154+
clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module);
155+
clcf->handler = ngx_http_echo_handler; //修改核心模块配置(也就是当前location),将其handler替换为我们自己定义的ngx_http_echo_handler
156+
ngx_conf_set_str_slot(cf,cmd,conf);
157157
return NGX_CONF_OK;
158158
}
159159

@@ -164,7 +164,7 @@ ngx_http_echo(ngx_conf_t *f,ngx_command_t *cmd , void *conf)
164164
* @param cf
165165
* @return
166166
*/
167-
static char *
167+
static void *
168168
ngx_http_echo_create_loc_conf(ngx_conf_t *cf)
169169
{
170170
ngx_http_echo_loc_conf_t *conf;
@@ -202,18 +202,6 @@ ngx_http_echo_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
202202
static ngx_int_t
203203
ngx_http_echo_init(ngx_conf_t *cf)
204204
{
205-
ngx_http_handler_pt *h;
206-
ngx_http_core_main_conf_t *cmcf;
207-
208-
cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module);
209-
210-
h = ngx_array_push(&cmcf->phases[NGX_HTTP_CONTENT_PHASE].handlers);
211-
if (h == NULL) {
212-
return NGX_ERROR;
213-
}
214-
215-
*h = ngx_http_echo_handler;
216-
217205
return NGX_OK;
218206
}
219207

0 commit comments

Comments
 (0)