nginx模块开发-hello world

参考 http://www.162cm.com/p/ngx_ext.html

nginx调试

1. configure --with-debug ...

2. 编译安装

3. nginx.conf error_log /path/to/errorlog debug;

此外,还可以增加daemon off的配置,让nginx把信息都打印到控制台。

hello world模块开发

1、在nginx.conf配置文件中增加location /hello { echo "hello world" }命令

2、创建模块目录,以及config文件、.c文件(其中.c文件的内容稍后介绍)

3、重新编译和安装nginx

auto/configure --with-debug --add-module=/root/03.nginx/ngx_module_echo/ --prefix=/root/03.nginx/nginx_bin

.c文件内容

1、ngx_module_t  ngx_module_echo。其中主要包含module context和module directives两个部分

2、ngx_http_module_t  ngx_echo_module_ctx。

其中包含preconfiguration/postconfiguration、create/init main configuration、create/merge server configuration、create/merge location configuration这4对回调。

因此hello world这个模块是对location中的命令处理,所以:实现最后一对回调,用于创建和合并location配置。

3、ngx_command_t  ngx_echo_commands[]。在其中的read_conf回调ngx_echo_readconf中处理echo命令。

4、在ngx_echo_readconf中注册ngx_http_core_loc_conf_t的handler。

后续在处理请求时会回调这个handler:static ngx_int_t ngx_echo_handler(ngx_http_request_t *r)。

在这个handler中获取本模块的配置,例如echo后面的”hello world“信息,写到输出中。

猜你喜欢

转载自chuqq.iteye.com/blog/1927273
今日推荐