Nginx从1.9.11版本开始支持动态模块加载功能,这允许你在不重新编译Nginx主程序的情况下添加或移除模块。以下是实现Nginx动态模块加载的详细步骤:
首先确认你的Nginx版本支持动态模块:
nginx -v
需要1.9.11或更高版本。
nginx -V
这会显示编译时包含的模块和配置参数。
许多发行版提供了预编译的动态模块包,例如:
# Ubuntu/Debian
apt install nginx-module-<name>
# CentOS/RHEL
yum install nginx-module-<name>
apt source nginx # Debian/Ubuntu
# 或
yumdownloader --source nginx # CentOS/RHEL
./configure --with-compat --add-dynamic-module=../module-source
make modules
编译好的.so
文件会出现在objs/
目录下。
在nginx.conf中添加:
load_module modules/ngx_http_geoip_module.so;
load_module modules/ngx_stream_module.so;
路径可以是绝对路径或相对于--prefix
指定的路径。
nginx -t
systemctl restart nginx
# 或
service nginx restart
版本不匹配错误:
module is not binary compatible
解决方法:使用与Nginx主程序完全相同的版本编译模块。
模块依赖问题: 确保所有依赖模块都已加载,顺序正确。
符号冲突: 如果模块之间有符号冲突,可能需要重新编译。
ngx_http_image_filter_module
- 图像处理ngx_http_geoip_module
- GeoIP支持ngx_http_xslt_filter_module
- XSLT转换ngx_http_auth_pam_module
- PAM认证通过动态模块,你可以灵活扩展Nginx功能而无需频繁重新编译主程序,大大提高了维护效率。