在已编译安装nginx上动态添加模块

一、添加nginx模块

找到安装nginx的源码根目录,如果没有的话下载新的源码
wget http://nginx.org/download/nginx-1.8.1.tar.gz

查看ngixn版本极其编译参数
../sbin/nginx -V
nginx version: nginx/1.8.1
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-28) (GCC) 
built with OpenSSL 1.0.2k-fips  26 Jan 2017
TLS SNI support enabled
configure arguments: --user=www --group=www --prefix=/application/nginx-1.8.1 --with-http_stub_status_module --with-http_ssl_module

停止ngixn
systemctl stop nginx

进入nginx源码目录
cd nginx-1.8.1/

以下是重新编译的代码和模块(重新编译增加了--with-file-aio --with-http_realip_module模块)
./configure --user=www --group=www --prefix=/application/nginx-1.8.1 --with-http_stub_status_module --with-http_ssl_module --with-file-aio --with-http_realip_module

make 千万别make install,否则就覆盖安装了

make完之后在objs目录下就多了个nginx,这个就是新版本的程序了

备份旧的nginx程序
cp /application/nginx-1.8.1/sbin/nginx /application/nginx-1.8.1/sbin/nginx.20180826

把新的nginx程序覆盖旧的
 cp objs/nginx /application/nginx-1.8.1/sbin/nginx

 测试新的nginx程序是否正确
/application/nginx/sbin/nginx  -t
nginx: the configuration file /application/nginx-1.8.1/conf/nginx.conf syntax is ok
nginx: configuration file /application/nginx-1.8.1/conf/nginx.conf test is successful

平滑重启nginx
systemctl reload nginx

查看ngixn版本及其编译参数
[root@nginx nginx-1.8.1]# /application/nginx/sbin/nginx -V
nginx version: nginx/1.8.1
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-28) (GCC) 
built with OpenSSL 1.0.2k-fips  26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/application/nginx-1.8.1 --with-http_stub_status_module --with-http_ssl_module --with-file-aio --with-http_realip_module

 说明:如果使用/application/nginx/sbin/nginx -s reload启动的话可以正常启动,由于我使用的是systemctl reload nginx启动的,会提示“Job for nginx.service invalid.”,我这边的做法是重启下服务器就可以了。

二、添加nginx第三方模块

这里以安装第三方ngx_http_google_filter_module模块为例
nginx的模块是需要重新编译nginx,而不是像apache一样配置文件引用.so

下载第三方扩展模块ngx_http_google_filter_module
[root@nginx ~]# cd /home/dm/tools/
[root@nginx tools]# git clone https://github.com/cuber/ngx_http_google_filter_module
如果提示没有git命令,使用yum install -y git安装即可
 
查看ngixn版本极其编译参数
../sbin/nginx -V
nginx version: nginx/1.8.1
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-28) (GCC) 
built with OpenSSL 1.0.2k-fips  26 Jan 2017
TLS SNI support enabled
configure arguments: --user=www --group=www --prefix=/application/nginx-1.8.1 --with-http_stub_status_module --with-http_ssl_module

3. 加入需要安装的模块,重新编译,如这里添加-–add-module=/data/software/ngx_http_google_filter_module
--user=www --group=www --prefix=/application/nginx-1.8.1 --with-http_stub_status_module --with-http_ssl_module  -–add-module=/data/software/ngx_http_google_filter_module

make 千万别make install,否则就覆盖安装了

make完之后在objs目录下就多了个nginx,这个就是新版本的程序了

备份旧的nginx程序
cp /application/nginx-1.8.1/sbin/nginx /application/nginx-1.8.1/sbin/nginx.20180826

把新的nginx程序覆盖旧的
 cp objs/nginx /application/nginx-1.8.1/sbin/nginx

 测试新的nginx程序是否正确
/application/nginx/sbin/nginx  -t
nginx: the configuration file /application/nginx-1.8.1/conf/nginx.conf syntax is ok
nginx: configuration file /application/nginx-1.8.1/conf/nginx.conf test is successful

平滑重启nginx
systemctl reload nginx
原文地址:https://www.cnblogs.com/Mr-Ding/p/9539192.html