实战开发一个 Nginx 扩展 (Nginx Module)
实战开发一个 Nginx 扩展 (Nginx Module)#
如果这个 repo 对你有帮助 欢迎 star fork#
repo 地址 https://github.com/wujunze/nginx-http-echo-module #
nginx_module_echo#
使用 echo 指令输出一个字符串
Nginx 版本#
Nginx1.0.10 https://github.com/nginx/nginx/releases/ta...
开发环境#
OS : CentOS Linux release 7.2.1511 (Core)
安装一个干净的 Nginx#
-
下载 Nginx10.10 并且解压它
-
安装 gcc 和 Nginx 需要的 lib
-
./configure --prefix=/usr/local/nginx && make && make install
-
运行 Nginx
定义模块配置结构#
typedef struct {
ngx_str_t ed; //该结构体定义在这里 https://github.com/nginx/nginx/blob/master/src/core/ngx_string.h
} ngx_http_echo_loc_conf_t;
定义 echo 模块的指令和参数转化函数#
定义模块 Context#
- 定义 ngx_http_module_t 类型的结构体变量
- 初始化一个配置结构体
- 将其父 block 的配置信息合并到此结构体 实现了配置的继承
编写 Handler 模块真正干活儿的部分#
组合 Nginx Module#
整理模块代码 按照 Nginx 官方规范#
编写 config 文件#
ngx_addon_name=ngx_http_echo_module
HTTP_MODULES="$HTTP_MODULES ngx_http_echo_module"
NGX_ADDON_SRCS="$NGX_ADDON_SRCS $ngx_addon_dir/src/ngx_http_echo_module.c"
编译安装 echo 模块#
./configure --prefix=/usr/local/nginx/ --add-module=/root/ngx_dev && make && make install
安装成功#
修改 Nginx 配置文件测试 Module#
Nginx echo Module 运行成功#
Thanks#
参考文档#
本作品采用《CC 协议》,转载必须注明作者和本文链接
推荐文章: