nginx平滑升级时,需要在运行的nginx是以全路径启动的

在平滑升级nginx的时候,如果不给出nginx程序的全路径的话,会出现USR2信号没起作用的情况

具体如下

验证

用nginx命令直接启动,不给全路径

[root@vm2 ~]# ll /usr/local/nginx/sbin/
total 30556
-rwxr-xr-x 1 nginx nginx 5175248 Feb  7 11:32 nginx
-rwxr-xr-x 1 nginx nginx 7934392 Feb  6 20:27 nginx.old
-rwxr-xr-x 1 nginx nginx 7934392 Feb  7 11:09 nginx.old2
-rwxr-xr-x 1 nginx nginx 5175248 Feb  7 11:32 nginx.old3
-rwxr-xr-x 1 nginx nginx 5055968 Feb  7 11:39 nginx.old4

[root@vm2 ~]# ll /usr/sbin/nginx
lrwxrwxrwx 1 root root 27 Feb  6 20:31 /usr/sbin/nginx -> /usr/local/nginx/sbin/nginx

[root@vm2 ~]# nginx -V
nginx version: nginx/1.20.2
built by gcc 8.5.0 20210514 (Red Hat 8.5.0-4) (GCC)
configure arguments: --prefix=/usr/local/nginx --user=nginx --group=nginx

# 启动nginx,不给全路径

[root@vm2 ~]# nginx
[root@vm2 ~]# pstree -p | grep nginx
           |-nginx(12096)---nginx(12097)

[root@vm2 ~]# cat /usr/local/nginx/logs/nginx.pid
12096

# 升级二进制

[root@vm2 ~]# rm -f /usr/local/nginx/sbin/nginx
[root@vm2 ~]# cp -p /usr/local/nginx/sbin/nginx.old4 /usr/local/nginx/sbin/nginx

[root@vm2 ~]# nginx -V
nginx version: nginx/1.21.6
built by gcc 8.5.0 20210514 (Red Hat 8.5.0-4) (GCC)
configure arguments: --prefix=/usr/local/nginx --user=nginx --group=nginx

# 发送USR2。没有新的,master

[root@vm2 ~]# kill -USR2 12096
[root@vm2 ~]# ps -ef | grep nginx
root       12096       1  0 12:10 ?        00:00:00 nginx: master process nginx
nginx      12097   12096  0 12:10 ?        00:00:00 nginx: worker process
root       12109    1124  0 12:12 pts/1    00:00:00 grep --color=auto nginx
[root@vm2 ~]#


非软连接的全路径

[root@vm2 ~]# /usr/local/nginx/sbin/nginx -V
nginx version: nginx/1.20.2
built by gcc 8.5.0 20210514 (Red Hat 8.5.0-4) (GCC)
configure arguments: --prefix=/usr/local/nginx --user=nginx --group=nginx

# 用不是软连接的全路径
[root@vm2 ~]# /usr/local/nginx/sbin/nginx
[root@vm2 ~]# ps -ef | grep nginx
root       12119       1  0 12:15 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx      12120   12119  0 12:15 ?        00:00:00 nginx: worker process
root       12122    1124  0 12:16 pts/1    00:00:00 grep --color=auto nginx

[root@vm2 ~]# cat /usr/local/nginx/logs/nginx.pid
12119

# 换新程序

[root@vm2 ~]# rm -f /usr/local/nginx/sbin/nginx
[root@vm2 ~]# cp -p /usr/local/nginx/sbin/nginx.old4 /usr/local/nginx/sbin/nginx

[root@vm2 ~]# /usr/local/nginx/sbin/nginx -V
nginx version: nginx/1.21.6
built by gcc 8.5.0 20210514 (Red Hat 8.5.0-4) (GCC)
configure arguments: --prefix=/usr/local/nginx --user=nginx --group=nginx

# 发送USR2,有新master进程

[root@vm2 ~]# kill -USR2 12119
[root@vm2 ~]# ps -ef | grep nginx
root       12119       1  0 12:15 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx      12120   12119  0 12:15 ?        00:00:00 nginx: worker process
root       12127   12119  0 12:17 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx      12128   12127  0 12:17 ?        00:00:00 nginx: worker process
root       12130    1124  0 12:17 pts/1    00:00:00 grep --color=auto nginx

全路径的软连接

[root@vm2 ~]# which nginx
/usr/sbin/nginx
[root@vm2 ~]# ll /usr/sbin/nginx
lrwxrwxrwx 1 root root 27 Feb  6 20:31 /usr/sbin/nginx -> /usr/local/nginx/sbin/nginx
[root@vm2 ~]# /usr/sbin/nginx -V
nginx version: nginx/1.20.2
built by gcc 8.5.0 20210514 (Red Hat 8.5.0-4) (GCC)
configure arguments: --prefix=/usr/local/nginx --user=nginx --group=nginx

# 使用全路径软连接
[root@vm2 ~]# /usr/sbin/nginx
[root@vm2 ~]# pstree -p | grep nginx
           |-nginx(12147)---nginx(12148)

[root@vm2 ~]# ps -ef | grep nginx
root       12147       1  0 12:22 ?        00:00:00 nginx: master process /usr/sbin/nginx
nginx      12148   12147  0 12:22 ?        00:00:00 nginx: worker process
root       12152    1124  0 12:22 pts/1    00:00:00 grep --color=auto nginx

# 换新程序

[root@vm2 ~]# rm -f /usr/local/nginx/sbin/nginx
[root@vm2 ~]# cp -p /usr/local/nginx/sbin/nginx.old4 /usr/local/nginx/sbin/nginx

# 发送USR2,有新的master进程

[root@vm2 ~]# kill -USR2 12147
[root@vm2 ~]# ps -ef | grep nginx
root       12147       1  0 12:22 ?        00:00:00 nginx: master process /usr/sbin/nginx
nginx      12148   12147  0 12:22 ?        00:00:00 nginx: worker process
root       12155   12147  0 12:23 ?        00:00:00 nginx: master process /usr/sbin/nginx
nginx      12156   12155  0 12:23 ?        00:00:00 nginx: worker process
root       12158    1124  0 12:23 pts/1    00:00:00 grep --color=auto nginx

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
nginx平滑升级的原理是通过动态加载新的nginx二进制文件,并将新旧版本的进程逐渐切换过渡,从而实现在不停止服务的情况下进行升级。 具体步骤如下: 1. 首先,安装新版本的nginx,并确保新版本的nginx二进制文件与旧版本的nginx二进制文件路径相同。可以通过下载新版本的nginx源码,编译并安装新的nginx二进制文件。 2. 在nginx的配置文件中,使用"include"指令将旧版本的配置文件与新版本的配置文件包含在一起。这样,在升级过程中,新旧版本的配置文件都会被加载。可以通过修改nginx.conf文件来实现。 3. 使用nginx平滑升级命令(通常是向旧版本的nginx主进程发送HUP信号)触发升级过程。例如,使用命令"kill -HUP <nginx主进程ID>"来通知nginx主进程进行平滑升级。 4. 在接收到HUP信号后,nginx主进程会启动一个新的子进程,该子进程会以新版本的nginx二进制文件执行,并加载新版本的配置文件。同,旧版本的子进程会继续处理当前正在进行的请求。 5. 当新版本的子进程启动完成并成功加载新的配置文件后,nginx主进程会向旧版本的子进程发送QUIT信号,通知其停止接收新的连接。 6. 旧版本的子进程在处理完当前正在进行的请求后,会自动退出。 通过这样的过程,nginx实现了平滑升级,保证了在升级过程中服务的持续可用性。同,由于新版本的nginx二进制文件和配置文件在升级过程中逐渐加载和替换,所以对服务的影响较小。 引用: https://nginx.org/en/docs/beginners_guide.html#conf_structure https://www.nginx.com/resources/wiki/start/topics/tutorials/commandline/#stopping-or-restarting-nginx http://nginx.org/en/docs/configure.html
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值