Nginx在已经安装好的Nginx上增加新模块不重启生效
以同时添加echo模块以及nginx-module-vts模块为例子。
文件:echo-nginx-module-0.62.tar.gz、nginx-module-vts-0.1.18.tar.gz
查看已安装的模块
1.查看默认安装模块。
cat /soft/nginx-1.20.1/auto/options | grep YES
2.编译时附带的可选模块或三方模块
nginx -V nginx version: nginx/1.20.1 built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC) built with OpenSSL 1.1.1k 25 Mar 2021 TLS SNI support enabled configure arguments: --prefix=/usr/local/nginx --sbin-path=/usr/local/nginx/sbin/nginx --conf-path=/usr/local/nginx/conf/nginx.conf --pid-path=/usr/local/nginx/logs/nginx.pid --with-http_gzip_static_module --with-http_stub_status_module --with-file-aio --with-http_realip_module --with-http_ssl_module --with-pcre=/soft/pcre-8.45 --with-zlib=/soft/zlib-1.2.11 --with-openssl=/soft/openssl-1.1.1k
解压
tar -zxvf echo-nginx-module-0.62.tar.gz tar -zxvf nginx-module-vts-0.1.18.tar.gz
添加新的模块,生成Makefile
进入原来安装nginx的安装包(若已删除,则重新下载与当前nginx版本一致的安装包)。
记得带上以上nginx -V中的原始配置。
[root@dm8 soft]# cd nginx-1.20.1
[root@dm8 nginx-1.20.1]# ./configure --add-module=/soft/nginx-module-vts-0.1.18 --add-module=/soft/echo-nginx-module-0.62 --prefix=/usr/local/nginx --sbin-path=/usr/local/nginx/sbin/nginx --conf-path=/usr/local/nginx/conf/nginx.conf --pid-path=/usr/local/nginx/logs/nginx.pid --with-http_gzip_static_module --with-http_stub_status_module --with-file-aio --with-http_realip_module --with-http_ssl_module --with-pcre=/soft/pcre-8.45 --with-zlib=/soft/zlib-1.2.11 --with-openssl=/soft/openssl-1.1.1k
编译
make
千万不要执行make install,会覆盖原来的nginx文件。
备份并替换nginx可执行文件
[root@dm8 nginx-1.20.1]# mv /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.bak [root@dm8 nginx-1.20.1]# cp objs/nginx /usr/local/nginx/sbin/
查看master进程号并开始热部署
[root@dm8 logs]# cat nginx.pid 740 或者 [root@dm8 logs]# ps -ef | grep nginx root 740 1 0 00:04 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf nobody 742 740 0 00:04 ? 00:00:00 nginx: worker process root 17497 1660 0 01:32 pts/1 00:00:00 grep --color=auto nginx
告诉nginx开始热部署,并且新启一个master(用新的nginx),但是老的worker也在运行,新的master也会生成新的worker。新的请求只会进去新的worker。
[root@dm8 logs]# kill -USR2 740 [root@dm8 logs]# ps -ef | grep nginx root 740 1 0 00:04 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf nobody 742 740 0 00:04 ? 00:00:00 nginx: worker process root 17494 740 0 01:32 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf nobody 17495 17494 0 01:32 ? 00:00:00 nginx: worker process root 17497 1660 0 01:32 pts/1 00:00:00 grep --color=auto nginx
告诉老的master进程,请优雅的关闭worker进程,不过老的master进程是不会关闭的,为回退做准备。
[root@dm8 logs]# kill -WINCH 740 [root@dm8 logs]# ps -ef | grep nginx root 740 1 0 00:04 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf root 17494 740 0 01:32 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf nobody 17495 17494 0 01:32 ? 00:00:00 nginx: worker process root 17500 1660 0 01:33 pts/1 00:00:00 grep --color=auto nginx [root@dm8 logs]# ll total 24 -rw-r--r-- 1 root root 8238 Aug 29 01:34 access.log -rw-r--r-- 1 root root 1115 Aug 29 01:32 error.log -rw-r--r-- 1 root root 6 Aug 29 01:32 nginx.pid -rw-r--r-- 1 root root 4 Aug 28 19:50 nginx.pid.oldbin [root@dm8 logs]# cat nginx.pid 17494 [root@dm8 logs]# cat nginx.pid.oldbin 740
确定不需要回退则关闭老的master进程
优雅的停止,处理完已经接受的请求, 相当于nginx -s quit。
[root@dm8 logs]# ps -ef | grep nginx root 740 1 0 00:04 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf root 17494 740 0 01:32 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf nobody 17495 17494 0 01:32 ? 00:00:00 nginx: worker process root 17511 1660 0 01:48 pts/1 00:00:00 grep --color=auto nginx [root@dm8 logs]# kill -QUIT 740 [root@dm8 logs]# ps -ef | grep nginx root 17494 1 0 01:32 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf nobody 17495 17494 0 01:32 ? 00:00:00 nginx: worker process root 17514 1660 0 01:51 pts/1 00:00:00 grep --color=auto nginx [root@dm8 logs]# ll total 20 -rw-r--r-- 1 root root 8238 Aug 29 01:34 access.log -rw-r--r-- 1 root root 1115 Aug 29 01:32 error.log -rw-r--r-- 1 root root 6 Aug 29 01:32 nginx.pid
如果需要回退
- cp -f nginx.bak nginx
- kill -HUP 旧版本的进程号 (拉起旧版本的worker进程)
- kill -WINCH 新版本的主进程号 (关闭新版本的worker进程)
- kill -QUIT 新版本的主进程号