首页 > 技术文章 > nginx增加第三方模块

anttech 2019-04-11 00:20 原文

增加第三方模块


============================================================

一、概述
nginx文件非常小但是性能非常的高效,这方面完胜apache。
nginx文件小的一个原因之一是nginx自带的功能相对较少,好在nginx允许第三方模块,第三方模块使得
nginx越发的强大。

nginx已支持动态加载模块

二、安装第三方模块
./configure --prefix=源安装目录 --add-module=/第三方模块解压目录

以安装ngx_cache_purge模块实例
场景一: 在未安装nginx的情况下安装nginx第三方模块
# ./configure \
--prefix=/usr/local/nginx \
--with-http_stub_status_module \
--with-http_ssl_module \
--with-http_realip_module \
--with-http_image_filter_module \
--add-module=/nginx_module/ngx_cache_purge \
--add-module=/nginx_module/echo-nginx-module-0.58
# make
# make install
# /usr/local/nginx/sbin/nginx


场景二:在已安装nginx情况下安装nginx模块
# /usr/local/nginx/sbin/nginx -V
# ./configure --prefix=/usr/local/nginx \
--with-http_stub_status_module \
--with-http_ssl_module \
--with-http_realip_module \
--with-http_image_filter_module \
--add-module=/nginx_module/ngx_cache_purge
# make
# cp objs/nginx /usr/local/nginx/sbin/nginx
# /usr/local/nginx/sbin/nginx -s reload

注意:

安装nginx安装第三方模块实际上是使用–add-module重新编译nginx二进制文件,不要make
install 而是直接把编译目录下objs/nginx 文件直接覆盖老的 nginx文件


============================================================

例如:工作中我们还有这样的需求,配置的nginx文件需要测试结果时,如果能够打印出相应结果的话,就更有利于我们排错了。其实在nginx有个第三方的模可以实现此功能,现在我们来做下配置

(1)查看我们nginx 安装的模块

[root@localhost ~]# /opt/data/nginx/sbin/nginx -V
nginx version: nginx/1.17.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-36) (GCC) 
built with OpenSSL 1.0.2k-fips  26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/opt/data/nginx --with-http_stub_status_module --with-http_ssl_module --with-stream

(2)下载模块

[root@localhost ~]# wget https://github.com/openresty/echo-nginx-module/archive/v0.60.tar.gz
--2019-07-26 22:15:47--  https://github.com/openresty/echo-nginx-module/archive/v0.60.tar.gz
[root@localhost ~]# tar xf v0.60.tar.gz

(3)安装模块

到nginx安装的解压文件中

[root@localhost nginx-1.17.0]# ./configure --prefix=/opt/data/nginx 
--with-http_stub_status_module
--with-http_ssl_module
--with-stream
--add-module=/root/echo-nginx-module-0.60
[root@localhost nginx-1.17.0]# make
注意:不能 make install

(4)将生成的nginx新文件将旧文件覆盖

[root@localhost nginx-1.17.0]# systemctl stop nginx
[root@localhost nginx-1.17.0]# cp objs/nginx /opt/data/nginx/sbin/nginx 
[root@localhost nginx-1.17.0]# systemctl start nginx
[root@localhost nginx-1.17.0]# nginx -V
nginx version: nginx/1.17.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-36) (GCC) 
built with OpenSSL 1.0.2k-fips  26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/opt/data/nginx --with-http_stub_status_module --with-http_ssl_module --with-stream --add-module=/root/echo-nginx-module-0.61

 

推荐阅读