Ubuntu已装Nginx 1.18.0,如何安装headers_more动态模块?
给已装的Nginx 1.18.0(Ubuntu)装headers_more动态模块
别只想着跑./configure,得按完整流程来,步骤如下:
1. 先搞到headers_more的源码
直接拉取模块的源码仓库就行,或者找对应版本的压缩包:
git clone https://github.com/openresty/headers-more-nginx-module.git cd headers-more-nginx-module
后面--add-dynamic-module里的path/to,就是你这个模块目录的绝对路径,比如/root/headers-more-nginx-module,别写相对路径,容易出问题。
2. 拿到现有Nginx的编译参数
这步很关键,必须和你当前装的Nginx编译参数一致,不然模块加载会报错。执行:
nginx -V
把输出里configure arguments:后面的一长串参数全复制下来,存好备用。
3. 进入你已有的Nginx 1.18.0源码目录
你已经有这个版本的源码包了,直接cd进去就行,比如:
cd /home/xxx/nginx-1.18.0
4. 执行configure命令
把刚才复制的编译参数原封不动加上,再追加动态模块的路径:
./configure [这里粘贴你复制的所有参数] --add-dynamic-module=/root/headers-more-nginx-module
注意是--add-dynamic-module,不是普通的--add-module,因为我们要编译成动态模块,不是静态编译进去。
5. 只编译模块,别装!
千万别跑make install,不然会把你现有的Nginx覆盖掉!只执行:
make modules
编译完后,在源码目录的objs文件夹里,会生成ngx_http_headers_more_filter_module.so这个模块文件。
6. 把模块放到Nginx的模块目录并加载
Ubuntu默认的Nginx模块目录是/usr/lib/nginx/modules/,把刚才生成的.so文件复制过去:
cp objs/ngx_http_headers_more_filter_module.so /usr/lib/nginx/modules/
然后编辑/etc/nginx/nginx.conf,在文件最开头加一行:
load_module modules/ngx_http_headers_more_filter_module.so;
7. 验证配置并重启Nginx
先检查配置有没有错:
nginx -t
没问题的话重启Nginx:
systemctl restart nginx
之后可以再跑nginx -V,看看输出里有没有这个模块的信息,确认加载成功。
内容的提问来源于stack exchange,提问作者Sam Johnson
相关产品推荐
相关产品推荐

