Nginx动态添加模块原理与准备
本示例主要演示如何为Nginx安装第三方模块ngx_http_google_filter_module。
与Apache允许在配置文件中直接加载.so动态库不同,Nginx的模块功能必须在编译过程中静态集成,因此无法像Apache那样仅通过修改配置文件来启用新模块。
第一步是获取所需的第三方扩展模块ngx_http_google_filter_module源码。
# cd /data/software/
# git clone https://github.com/cuber/ngx_http_google_filter_module
第二步需要确认当前Nginx编译时所使用的具体参数和已安装的模块列表。
将终端工作目录切换至Nginx可执行程序所在的路径,然后执行命令./nginx -V来获取详细信息,操作过程如下所示:
[root@liuyazhuang121 sbin]# ./nginx -V
nginx version: nginx/1.9.3
built by gcc 4.4.7 20120313 (Red Hat 4.4.7-17) (GCC)
built with OpenSSL 1.0.2 22 Jan 2026
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx-1.9.3 --with-openssl=/usr/local/src/openssl-1.0.2 --with-pcre=/usr/local/src/pcre-8.37 --with-zlib=/usr/local/src/zlib-1.2.8 --with-http_ssl_module
[root@liuyazhuang121 sbin]#
从输出结果中可以清晰看到,当前的编译安装使用了--prefix=/usr/local/nginx-1.9.3 --with-openssl=/usr/local/src/openssl-1.0.2 --with-pcre=/usr/local/src/pcre-8.37 --with-zlib=/usr/local/src/zlib-1.2.8 --with-http_ssl_module等一系列配置参数。
第三步是在保留原有编译参数的基础上,加入需要安装的新模块,并重新进行编译配置。
在此步骤中,我们需要在配置命令末尾追加-–add-module=/data/software/ngx_http_google_filter_module参数,具体配置方式如下:
复制代码 代码如下: ./configure --prefix=/usr/local/nginx-1.9.3 --with-openssl=/usr/local/src/openssl-1.0.2 --with-pcre=/usr/local/src/pcre-8.37 --with-zlib=/usr/local/src/zlib-1.2.8 --with-http_ssl_module -–add-module=/data/software/ngx_http_google_filter_module
如上所示,必须完整保留之前安装Nginx时的所有参数,并在最后追加-–add-module=/data/software/ngx_http_google_filter_module。完成配置后,接下来需要执行编译操作,具体步骤如下:
# make //千万不要make install,不然就真的覆盖
第四步是将新编译生成的Nginx二进制文件替换掉系统中的旧版本。
# 备份原来的nginx执行程序
# cp /usr/local/nginx-1.9.3/sbin/nginx /usr/local/nginx-1.9.3/sbin/nginx.bak
# 将新编译的nginx执行程序复制到/usr/local/nginx/sbin/目录下
# cp /opt/nginx/nginx /usr/local/nginx/sbin/
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持本站。