微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

nginx添加模块与平滑升级

Nginx 添加第三方模块

众所周知Nginx是分成一个个模块的,比如core模块,gzip模块,proxy模块,每个模块负责不同的功能,除了基本的模块,有些模块可以选择编译或不编译进Nginx。官网文档中的Modules reference部分列出了Nginx源码包的所有模块。我们可以按照自己服务器的需要来定制出一个最适合自己的Nginx服务器。

除了Nginx官网源码包提供了各种模块,Nginx还有各种各样的第三方模块。官方文档Nginx 3rd Party Modules也列出了Nginx的很多第三方模块,除此官网列出的之外,还有很多很有用的模块也能在Github等网站上找到。

这些模块提供着各种各样意想不到的功能,灵活使用Nginx的第三方模块,可能会有非常大的意外收获。
本篇文章以GitHub上的Nginx-module-vts作为例子,此模块可以监控Nginx虚拟主机流量以及状态,下面我们来看一下第三模块的安装以及简单的使用。

下载第三方模块
下载的模块存放在/home/Nginx_conf/中

cd /home/Nginx_conf/
git clone git://github.com/vozlt/Nginx-module-vts.git

添加模块编译Nginx

查看当前Nginx编译参数
shell> Nginx -V 
Nginx version: Nginx/1.14.0
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=/usr/local/Nginx \
--user=Nginx \
--group=Nginx \
--with-debug \
--with-http_ssl_module \
--with-http_realip_module \
--with-http_image_filter_module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_stub_status_module \
--http-log-path=/var/log/Nginx/access.log \
--error-log-path=/var/log/Nginx/error.log
添加模块编译
在获取的编译参数中再添加需要的模块

 --add-module= PATH
 # 这里具体路径为
 --add-module=/home/Nginx_conf/module/Nginx-module-vts

最终的配置如下

shell>./configure \
--prefix=/usr/local/Nginx \
--user=Nginx \
--group=Nginx \
--with-debug \
--with-http_ssl_module \
--with-http_realip_module \
--with-http_image_filter_module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_stub_status_module \
--http-log-path=/var/log/Nginx/access.log \
--error-log-path=/var/log/Nginx/error.log \
--add-module=/home/Nginx_conf/module/Nginx-module-vts

执行编译命令:make,注意编译之后千万不能执行make install

编译完后,当前Nginx源码目录下生成objs目录则说明编译成功

覆盖Nginx执行脚本

  • 备份当前Nginx执行脚本,命令:cp /usr/local/Nginx/sbin/Nginx /usr/local/Nginx/sbin/Nginx.bak。如果拷贝出错,则将Nginx进行杀掉再进行,命令:killall Nginx 或者 Nginx -s stop

  • 拷贝上一步骤编译后的新Nginx脚本,命令:cp /home/software/Nginx-1.14.0/objs/Nginx /usr/local/Nginx/sbin/

  • 在 home/software/Nginx-1.14.0/下载执行make upgrade 平滑升级Nginx

  • 查看编译参数,命令:Nginx -V,如果编译参数中存在刚添加的模块,则说明编译成功

使用第三方模块

http {
    vhost_traffic_status_zone;

    ...

    server {

        ...

        location /status {
            vhost_traffic_status_display;
            vhost_traffic_status_display_format html;
        }
    }
}

浏览器输入http://your_ip/status

Nginx平滑升级

1 下载更新版本的Nginx

wget http://nginx.org/download/nginx-1.16.1.tar.gz

2 查看老版本的配置

Nginx -V

Nginx version: Nginx/1.14.0
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=/usr/local/Nginx \
--user=Nginx \
--group=Nginx \
--with-debug \
--with-http_ssl_module \
--with-http_realip_module \
--with-http_image_filter_module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_stub_status_module \
--http-log-path=/var/log/Nginx/access.log \
--error-log-path=/var/log/Nginx/error.log

复制Nginx -V的结果进行输出编译

./configure --prefix=/usr/local/Nginx1.15.1 \
--user=Nginx \
--group=Nginx \
--with-debug \
--with-http_ssl_module \
--with-http_realip_module \
--with-http_image_filter_module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_stub_status_module \
--http-log-path=/var/log/Nginx/access.log \
--error-log-path=/var/log/Nginx/error.log

//编译 
make && make install

3 重启Nginx的进程

ngins -s stop

/usr/local/Nginx1.15.1/sbin/Nginx -tc /usr/local/Nginx/conf/Nginx.conf

/usr/local/Nginx1.15.1/sbin/Nginx -s start -c /usr/local/Nginx/conf/Nginx.conf

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐