实战开发一个 Nginx 扩展 (Nginx Module)

您所在的位置:网站首页 nginx开发公司 实战开发一个 Nginx 扩展 (Nginx Module)

实战开发一个 Nginx 扩展 (Nginx Module)

2022-06-04 13:08| 来源: 网络整理| 查看: 265

实战开发一个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...image

开发环境 OS : CentOS Linux release 7.2.1511 (Core)

imageimage

安装一个干净的 Nginx

下载 Nginx10.10 并且解压它image

安装gcc和Nginx需要的libimageimage

./configure --prefix=/usr/local/nginx && make && make installimageimageimage

运行Nginximageimage

定义模块配置结构 typedef struct { ngx_str_t ed; //该结构体定义在这里 https://github.com/nginx/nginx/blob/master/src/core/ngx_string.h } ngx_http_echo_loc_conf_t;

image

定义echo模块的指令和参数转化函数

image

定义模块Context 定义ngx_http_module_t类型的结构体变量image 初始化一个配置结构体image 将其父block的配置信息合并到此结构体 实现了配置的继承image 编写Handler 模块真正干活儿的部分

image

组合Nginx Module

image

整理模块代码 按照Nginx官方规范

image

编写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 安装成功

image

修改Nginx配置文件测试Module

image

Nginx echo Module 运行成功

image

Thanks 参考文档 http://wiki.nginx.org/Configuration http://tengine.taobao.org/book/ http://blog.codinglabs.org/articles/intro-... https://www.nginx.com/resources/wiki/modul... 本作品采用《CC 协议》,转载必须注明作者和本文链接 我的GitHub


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3