[ 知识是人生的灯塔,只有不断学习,才能照亮前行的道路 ]
📢 大家好,我是 WeiyiGeek,一名深耕安全运维开发(SecOpsDev)领域的技术从业者,致力于探索DevOps与安全的融合(DevSecOps),自动化运维工具开发与实践,企业网络安全防护,欢迎各位道友一起学习交流、一起进步 🚀,若此文对你有帮助,一定记得倒点个关注⭐与小红星❤️,收藏学习不迷路 😋 。
Nginx 模块编译使用介绍
Nginx 之所以有如此多的特性,因为有第三方开发者为其开发模块,Nginx 利用模块机制可极大程度上扩展丰富 Nginx 的使用,开发人员可自行开发一系列的模块,例如,后续要学习的 OpenResty 以及在Nginx中使用Javascript 代码(简述:NJS)进行请求处理。
自从 2004 年发布 Nginx 以来其模块没有发生什么大的变动。
原文连接: https://articles.zsxq.com/id_dertjh0239h3.html
使用模块的前置条件?模块配置项如何查看?模块变量如何查看?
描述:源码构建时需提前编译模块进行 Nginx,查看加入模块可访问 /objs/ngx_modules.c
文化以及 Nginx -V
命令查看,模块的配置项以及使用皆可参照官网:https://nginx.org/en/docs/
# 执行 ./configure 编译 Nginx 源代码后生成的目录
vim /usr/local/src/nginx-1.26.3/objs/ngx_modules.c
# 其中 ngx_modules 指针数组包含 Nginx 将安装的模块
ngx_module_t *ngx_modules[] = {
&ngx_core_module,
&ngx_errlog_module,
&ngx_conf_module,
&ngx_events_module,
&ngx_event_core_module,
&ngx_epoll_module,
&ngx_http_module,
&ngx_http_core_module,
........
&ngx_http_userid_filter_module,
&ngx_http_headers_filter_module,
&ngx_http_copy_filter_module,
&ngx_http_range_body_filter_module,
&ngx_http_not_modified_filter_module,
NULL
};
# 执行命令查看编译时参数,加载的外部模块
nginx -V
nginx version: nginx/1.26.3
built by gcc 12.3.1 (openEuler 12.3.1-38.oe2403) (GCC)
configure arguments: --prefix=/usr/local/nginx --user=nginx --group=nginx --sbin-path=/usr/sbin/nginx --conf-path=/usr/local/nginx/conf/nginx.conf --pid-path=/usr/local/nginx/nginx.pid --error-log-path=/var/log/nginx/logs/error.log --http-log-path=/var/log/nginx/logs/access.log --lock-path=/var/run/nginx.lock --modules-path=/usr/local/nginx/modules --without-http_rewrite_module

模块何时被使用?
Nginx 中有的模板在其运行时便会使用,而有得模块必须在配置文件中设置正确后才会被使用,例如 ngx_http_gzip_module
模块,其源码路径为/usr/local/src/nginx-1.26.3/src/http/module/ngx_http_gzip_filter_module.c
, 里面有个静态结构体 ngx_command_t,里面包含了器指令名称及参数,例如 "gzip"、"gzip_buffers",官方文档地址:https://nginx.org/en/docs/http/ngx_http_gzip_module.html
static ngx_command_t ngx_http_gzip_filter_commands[] = {
{ ngx_string("gzip"),
{ ngx_string("gzip_buffers"),
{ ngx_string("gzip_types"),
{ ngx_string("gzip_comp_level"),
{ ngx_string("gzip_window"),
{ ngx_string("gzip_hash"),
{ ngx_string("postpone_gzipping"),
{ ngx_string("gzip_no_buffer"),
{ ngx_string("gzip_min_length"),
static ngx_str_t ngx_http_gzip_ratio = ngx_string("gzip_ratio");

Nginx 模块源代码中,ngx_moudle t 结构体里 ngx_command 数组中,包含了模块配置指令名称,类型等等。不同的应用中每一类都有许多模块组成,例如 core 、http 、event 每一类子模块都定义了新的规则,特别注意一点模块加载是有顺序的,这个是在nginx_moudle t 结构体 *ctr index 决定的,当模块间有冲突时先生效的模块会阻碍后生效的模块。
高内聚:相应独立的功能是一个模块中。
抽象:利用结构体方式,在启动的阶段进行加载回调。
weiyigeek.top-Nginx 模板标准组成
在前面 Nginx 模块图中有一个ngx_module t
数据结构,其中它有一个数据成员 type
,其定义了模块属于哪一个类型的模块。
例如,Nginx 大类核心模块及核心子模块如下所示:
NGX_CORE_MODULE
核心模块: 包含了 core、events、errlog、thread_pool、openssl、http、mail、stream
,其中 events、http、mail、stream
又会定义新的子类型模块。
NGX_CONF_MODULE
配置模块:包含了 ngx_conf_moudule 子模块,负责解析我们的 nginx.conf
配置文件。
weiyigeek.top-Nginx 大类核心模块及核心子模块
Nginx 动态模块
描述:nginx 自从 1.9.11 以后就支持动态加载模块,在 Nginx 中使用动态模块可以减少编译环节,提高运维效率。例如,在现有、升级 Nginx 时使用动态模块可以减少构建参数,直接指定动态模块的代码库文件,代码发生变动时也可单独编译
动态模块,并复制到指定的动态库目录中,所以好处不言而喻减少在编译安装过程中错误,以及生产的二进制产物体积,但又不是所有模块都能以动态模块进行加载(这里需要特别注意一下),下面是其静态模块、动态模块编译及使用的大致流程。
静态库:文件以
.a
结尾,会直接将所有的源代码编译进最终的二进制可执行文件中。动态库:文件以
.so
结尾,则是在二进制文件中只保留了调用它的地址,通过配置的加载指令在启动 Nginx 时,将会去加载调用动态库。

查看那些内置模块是支持动态调用以及 --add-dynamic-module
,使用如下命令查看:
./configure --help | grep "dynamic"
--with-http_xslt_module=dynamic enable dynamic ngx_http_xslt_module
--with-http_image_filter_module=dynamic enable dynamic ngx_http_image_filter_module
--with-http_geoip_module=dynamic enable dynamic ngx_http_geoip_module
--with-http_perl_module=dynamic enable dynamic ngx_http_perl_module
--with-mail=dynamic enable dynamic POP3/IMAP4/SMTP proxy module
--with-stream=dynamic enable dynamic TCP/UDP proxy module
--with-stream_geoip_module=dynamic enable dynamic ngx_stream_geoip_module
# 第三方模块
--add-dynamic-module=PATH enable dynamic external module
# 启用动态模块兼容,通常与上述构建参数配置使用。
--with-compat dynamic modules compatibility
实践,以 ngx_http_image_filter_module 模板编译为动态模块为例,使用 --with-compat 和 --add-dynamic-module 参数来配置编译环境
yum install pcre pcre-devel gd-devel
cd nginx-1.26.3
# 编译
./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --sbin-path=/usr/sbin/nginx --conf-path=/usr/local/nginx/conf/nginx.conf --pid-path=/usr/local/nginx/nginx.pid --error-log-path=/var/log/nginx/logs/error.log --http-log-path=/var/log/nginx/logs/access.log --lock-path=/var/run/nginx.lock --modules-path=/usr/local/nginx/modules --without-http_rewrite_module --with-compat --with-http_image_filter_module=dynamic
# 构建 完毕后将会生产 nginx 二进制 以及 ngx_http_image_filter_module 动态模块
make
ls objs/
nginx ngx_http_image_filter_module.so(关键点) ngx_modules.o
ngx_http_image_filter_module_modules.o
# 安装
make install
# 查看构建命令参数
nginx -V
编译构建完毕后,将会在nginx 安装目录中 modules 目录查看编译后的动态模块了。
ls -alg modules
-rwxr-xr-x 1 root 105464 Aug 4 05:03 ngx_http_image_filter_module.so
-rwxr-xr-x 1 root 106272 Aug 4 04:56 ngx_http_image_filter_module.so.old
修改nginx配置文件,加载使用 ngx_http_image_filter 动态模块, 完成使用使用 nginx -t
命令校验配置。
load_module modules/ngx_http_image_filter_module.so;
http {
...
server {
....
location / {
root html;
image_filter resize 180 180;
}
}
}
启动 Nginx 查看动态加载模块的效果,可看到图片已经变成了 180x180 了。

若此时将 nginx.conf 配置文件中的# load_module modules/ngx_http_image_filter_module.so;
注释掉,校验配置文件时将会报 nginx: [emerg] unknown directive "image_filter"
错误。
若编译时出现 error: the HTTP rewrite module requires the PCRE library.
或者./configure: error: the HTTP image filter module requires the GD library.
错误解决办法如下:
./configure: error: the HTTP rewrite module requires the PCRE library.
You can either disable the module by using --without-http_rewrite_module
option, or install the PCRE library into the system, or build the PCRE library
statically from the source with nginx by using --with-pcre=<path> option.
./configure: error: the HTTP image filter module requires the GD library.
You can either do not enable the module or install the libraries.
# Redhat 系列
yum install pcre pcre-devel gd-devel
# Debian 系列
apt-get install libpcre3 libpcre3-dev libgd
至此,Nginx 动态模块的构建、加载、调用模块实践完毕!
Nginx 模块开发学习书籍推荐:
END
加入:作者【全栈工程师修炼指南】知识星球
『 全栈工程师修炼指南』星球,主要涉及全栈工程师(Full Stack Development)实践文章,包括但不限于企业SecDevOps和网络安全等保合规、安全渗透测试、编程开发、云原生(Cloud Native)、物联网工业控制(IOT)、人工智能Ai,从业书籍笔记,人生职场认识等方面资料或文章。
Q: 加入作者【全栈工程师修炼指南】星球后有啥好处?
✅ 将获得作者最新工作学习实践文章以及网盘资源。
✅ 将获得作者珍藏多年的全栈学习笔记(需连续两年及以上老星球友,也可单次购买) ✅ 将获得作者专门答疑学习交流群,解决在工作学习中的问题。 ✅ 将获得作者远程支持(在作者能力范围内且合规)。
目前新人仅需 69 元即可加入作者星球,数量有限,期待你的加入!
获取:作者工作学习全栈笔记
作者整理了10年的工作学习笔记(涉及网络、安全、运维、开发),需要学习实践笔记的看友,可添加作者微信或者回复【工作学习实践笔记】,当前价格¥299,除了获得从业笔记的同时还可进行问题答疑以及每月远程技术支持,希望大家多多支持,收获定大于付出!
知识推荐 往期文章
若文章对你有帮助,请将它转发给更多的看友,若有疑问的小伙伴,可在评论区留言你想法哟 💬!