nginx第三方模块管理需自主建立稳定目录约定:源码存于/usr/src/nginx-modules/,编译后.so文件放--modules-path指定路径(如/usr/lib64/nginx/modules/),配置通过include /etc/nginx/modules/*.conf引入。

Nginx 本身不自动管理第三方模块源码,规范路径的核心在于编译前准备清晰、安装后配置可追溯、运行时加载有依据。关键不是“Nginx规定”,而是你主动建立一套稳定、可复现的目录约定。
第三方模块源码存放路径要便于编译引用
模块源码不参与运行,只在 ./configure --add-module= 阶段被读取,因此重点是路径稳定、易定位:
- 推荐统一放在独立目录下,例如
/usr/src/nginx-modules/或~/nginx-modules/ - 每个模块用子目录隔离(避免混杂),如
/usr/src/nginx-modules/headers-more-nginx-module/ - 确保模块根目录包含
config文件(这是 Nginx 编译识别模块的必要条件) - 使用绝对路径传给
--add-module=,避免因工作目录切换导致路径解析失败
编译安装后的模块文件存放路径由 --modules-path 决定
模块编译成功后生成 .so 文件,需明确指定其落盘位置:
- 默认路径通常是
$prefix/modules/(如/usr/local/nginx/modules/) - 建议显式指定:
./configure --modules-path=/usr/lib64/nginx/modules - 该路径后续会被
load_module指令引用,所以需与配置中路径一致
模块配置片段的存放遵循 Nginx 的 include 机制
模块启用不靠“放对位置”,而靠主配置中显式加载:
- 将模块相关指令(如
more_set_headers,geoip2等)写入单独配置文件,例如/etc/nginx/modules/headers-more.conf - 在
nginx.conf的http块中加入:include /etc/nginx/modules/*.conf; - 目录结构示例:
-
/etc/nginx/modules/→ 存放各模块的启用配置(.conf) -
/usr/lib64/nginx/modules/→ 存放编译生成的.so文件 -
/usr/src/nginx-modules/→ 存放原始模块源码(仅编译期需要)
-
宝塔等面板环境需适配其路径习惯
宝塔把站点配置放在 /www/server/panel/vhost/nginx/,但模块本身仍走标准路径:
- 它不会改变
load_module加载.so文件的逻辑,仍依赖--modules-path和load_module路径 - 若你在宝塔里手动加模块,需确保:
-
.so文件已复制到宝塔默认模块路径(如/www/server/nginx/modules/) -
nginx.conf中load_module指向该路径,例如load_module modules/ngx_http_geoip2_module.so; - 对应的模块配置仍可通过
include /www/server/panel/vhost/nginx/modules/*.conf统一管理
-
不复杂但容易忽略











