Nginx 平滑重启,不停服务-2022-5-20

Nginx 平滑重启

主进程支持的信号
TERM ,INT : 立刻退出
QUIT : 等待工作进程结束后再退出
KILL : 强制终止进程
HUP : 重新加载配置文件,使用新的配置启动工作进程,逐步关闭旧进程。
USR1 : 重新打开日志文件
USR2 : 启动新的主进程,实现热升级
WINCH : 逐步关闭工作进程工作,进程支持的信号
TERM , INT : 立刻退出
QUIT : 等待请求处理结束后再退出
USR1 : 重新打开日志文件
查看当前环境nginx 模块
~]# nginx  -V
Tengine version: Tengine/2.2.0 (nginx/1.8.1)
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-4) (GCC) 
TLS SNI support enabled
configure arguments: --prefix=/data/ops/app/tengine --with-http_stub_status_module --with-http_ssl_module --with-pcre --with-openssl-opt=/usr/lib64 --with-zlib= --with-http_upstream_check_module --add-module=../ngx_cache_purge-2.3 --with-http_realip_module --with-http_auth_request_module --with-http_realip_module
查看新增模块
~]# nginx -v
./configure --prefix=/data/ops/app/tengine \
--with-http_stub_status_module \
--with-http_ssl_module --with-pcre \
--with-openssl-opt=/usr/lib64 --with-zlib= \
--add-module=./modules/ngx_http_upstream_check_module \
--add-module=./modules/ngx_http_upstream_session_sticky_module \
--add-module=./modules/ngx_http_upstream_dynamic_module \
--add-module=./modules/ngx_http_upstream_vnswrr_module \
--add-module=../ngx_cache_purge-2.3 \
--with-http_realip_module \
--with-http_auth_request_module \
--with-http_realip_module \
--with-http_gzip_static_module \
--with-http_sub_module \
--add-module=../headers-more-nginx-module-0.33/ \
--with-stream --with-stream_ssl_module \
--with-stream_ssl_preread_module \
--with-stream_realip_module \
--with-nginx_tcp_proxy_module
执行编译
~]# make
~]# objs/nginx -V
在线升级nginx

生成的二进制文件复制到升级的机器,替换当时nginx

~]# cd /data/ops/app/tengine/sbin
~]# mv nginx nginx_bak
~]# mv /root/nginx .
~]# nginx -t
nginx: the configuration file /data/ops/app/tengine/conf/nginx.conf syntax is ok
nginx: configuration file /data/ops/app/tengine/conf/nginx.conf test is successfu

查看当前nginx 主进程号:nginx: master process nginx

~]# ps aux |grep nginx
root    846990 0.0  0.0  48444 1564 ?     Ss  10:20  0:00 nginx: master process nginx
nobody   846992 0.0  0.1  52616 5792 ?     SN  10:20  0:00 nginx: worker process
nobody   846994 0.0  0.1  52616 5792 ?     SN  10:20  0:00 nginx: worker process

重新加载配置文件,使用新的配置启动工作进程,逐步关闭旧进程

~]# kill -HUP 846990
~]# ps aux |grep nginx
root    846990 0.0  0.0  48700 3868 ?     Ss  10:45  0:00 nginx: master process nginx
nobody   848896 0.0  0.1  52872 6048 ?     SN  10:45  0:00 nginx: worker process
nobody   848898 0.0  0.1  52872 6048 ?     SN  10:45  0:00 nginx: worker process

启动新的主进程,实现热升级,两个master,说明新的主进程启动成功

~]# kill -USR2 846990
~]# ps aux |grep nginx
root    846990 0.0  0.1  48700 3880 ?     Ss  10:45  0:00 nginx: master process nginx
nobody   848896 0.0  0.1  52872 6048 ?     SN  10:45  0:00 nginx: worker process
nobody   848898 0.0  0.1  52872 6048 ?     SN  10:45  0:00 nginx: worker process
nobody   848899 0.0  0.1  52872 6048 ?     SN  10:45  0:00 nginx: worker process
root    849851 0.0  0.1  48448 4812 ?     S   11:01  0:00 nginx: master process nginx
nobody   849123 0.0  0.1  52620 5792 ?     SN  11:01  0:00 nginx: worker process
nobody   849321 0.0  0.1  52620 5792 ?     SN  11:01  0:00 nginx: worker process

逐步关闭工作进程工作进程支持的信号,旧的工作进程已关闭

~]# kill -WINCH 846990
~]# ps aux |grep nginx
root    846990 0.0  0.1  48700 3880 ?     Ss  10:45  0:00 nginx: master process nginx
root    849851 0.0  0.1  48448 4812 ?     S   11:01  0:00 nginx: master process nginx
nobody   849123 0.0  0.1  52620 5792 ?     SN  11:01  0:00 nginx: worker process
nobody   849322 0.0  0.1  52620 5792 ?     SN  11:01  0:00 nginx: worker process

等待请求处理结束后再退出,只剩一个主进程

~]# kill -QUIT 846990
~]# ps aux |grep nginx
root    849851 0.0  0.1  48448 4812 ?     S   11:01  0:00 nginx: master process nginx
nobody   849123 0.0  0.1  52620 5792 ?     SN  11:01  0:00 nginx: worker process
nobody   849322 0.0  0.1  52620 5792 ?     SN  11:01  0:00 nginx: worker process

到此nginx 升级完成!

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
禁止转载,如需转载请通过简信或评论联系作者。

推荐阅读更多精彩内容