服务器之家:专注于服务器技术及软件下载分享
分类导航

云服务器|WEB服务器|FTP服务器|邮件服务器|虚拟主机|服务器安全|DNS服务器|服务器知识|Nginx|IIS|Tomcat|

服务器之家 - 服务器技术 - Nginx - Nginx热部署的实现

Nginx热部署的实现

2022-01-17 17:13ITKaven Nginx

本文主要介绍了Nginx热部署的实现,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

跟着上面这篇博客进行操作即可。关闭防火墙,让本地可以通过浏览器访问nginx服务。

?
1
[root@localhost ~]# systemctl stop firewalld

Nginx热部署的实现

信号量

查看信号量:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
[root@localhost ~]# kill -l
 1) sighup   2) sigint   3) sigquit  4) sigill   5) sigtrap
 6) sigabrt  7) sigbus   8) sigfpe   9) sigkill 10) sigusr1
11) sigsegv 12) sigusr2 13) sigpipe 14) sigalrm 15) sigterm
16) sigstkflt   17) sigchld 18) sigcont 19) sigstop 20) sigtstp
21) sigttin 22) sigttou 23) sigurg  24) sigxcpu 25) sigxfsz
26) sigvtalrm   27) sigprof 28) sigwinch    29) sigio   30) sigpwr
31) sigsys  34) sigrtmin    35) sigrtmin+1  36) sigrtmin+2  37) sigrtmin+3
38) sigrtmin+4  39) sigrtmin+5  40) sigrtmin+6  41) sigrtmin+7  42) sigrtmin+8
43) sigrtmin+9  44) sigrtmin+10 45) sigrtmin+11 46) sigrtmin+12 47) sigrtmin+13
48) sigrtmin+14 49) sigrtmin+15 50) sigrtmax-14 51) sigrtmax-13 52) sigrtmax-12
53) sigrtmax-11 54) sigrtmax-10 55) sigrtmax-9  56) sigrtmax-8  57) sigrtmax-7
58) sigrtmax-6  59) sigrtmax-5  60) sigrtmax-4  61) sigrtmax-3  62) sigrtmax-2
63) sigrtmax-1  64) sigrtmax   

64种信号量,以下是几种常用的信号量:

  • sigintsigterm:快速关闭。
  • sigquit:从容关闭(优雅的关闭进程,即等请求结束后再关闭)。
  • sighup:平滑重启,重新加载配置文件 (平滑重启,修改配置文件之后不用重启服务器)。
  • sigusr1 :重新读取日志文件,在切割日志文件时用途较大。
  • sigusr2:平滑升级可执行程序 ,nginx升级时候用。
  • sigwinch :从容关闭工作进程。

nginx热部署

nginx是一个多进程的高性能反向代理服务器,包含一个master进程和多个worker进程(worker进程的数量可以通过nginx.conf配置文件中的worker_processes参数进行设置,默认1个),这样可以充分利用多核处理器。

Nginx热部署的实现

默认1worker进程。

Nginx热部署的实现

并且master进程和worker进程是父子进程关系。

Nginx热部署的实现

nginx工作模式为多进程,nginx在启动之后会有一个master进程和多个worker进程(默认1个),多个worker子进程将监听master父进程监听的端口(参考父子进程的关系),并行处理请求。master父进程主要用来管理worker子进程(管理真正提供服务的worker进程,向worker进程发送信号,监控worker进程的运行状态,当worker进程异常退出后,会重新启动新的worker进程),读取并验证配置信息,master进程不会对用户请求提供服务,而用户请求是由worker进程进行处理。

nginx是通过信号量来控制,比如停止和重启nginx。信号量是进程间通信的一种机制,master主进程控制多个worker子进程,也是通过信号量。

Nginx热部署的实现

现在来演示nginx是怎么实现热部署的,博主通过修改nginx的配置文件来模拟nginx的升级(先copy一份副本)。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
[root@localhost ~]# cd /usr/local/nginx/conf/
[root@localhost conf]# ll
总用量 68
-rw-r--r--. 1 root root 1077 1220 20:24 fastcgi.conf
-rw-r--r--. 1 root root 1077 1220 20:24 fastcgi.conf.default
-rw-r--r--. 1 root root 1007 1220 20:24 fastcgi_params
-rw-r--r--. 1 root root 1007 1220 20:24 fastcgi_params.default
-rw-r--r--. 1 root root 2837 1220 20:24 koi-utf
-rw-r--r--. 1 root root 2223 1220 20:24 koi-win
-rw-r--r--. 1 root root 5231 1220 20:24 mime.types
-rw-r--r--. 1 root root 5231 1220 20:24 mime.types.default
-rw-r--r--. 1 root root 2656 1220 21:26 nginx.conf
-rw-r--r--. 1 root root 2656 1220 20:24 nginx.conf.default
-rw-r--r--. 1 root root  636 1220 20:24 scgi_params
-rw-r--r--. 1 root root  636 1220 20:24 scgi_params.default
-rw-r--r--. 1 root root  664 1220 20:24 uwsgi_params
-rw-r--r--. 1 root root  664 1220 20:24 uwsgi_params.default
-rw-r--r--. 1 root root 3610 1220 20:24 win-utf
[root@localhost conf]# cp nginx.conf nginx_old.conf
[root@localhost conf]# vim nginx.conf

Nginx热部署的实现

由于还没有给nginx进行热部署,现在访问http://192.168.1.199/还是原来的nginx页面。

Nginx热部署的实现

查看nginx的进程:

?
1
2
3
4
[root@localhost conf]# ps -ef | grep nginx
root     14964     1  0 22:25 ?        00:00:00 nginx: master process ./nginx
nobody   14965 14964  0 22:25 ?        00:00:00 nginx: worker process
root     15016  1521  0 23:07 pts/0    00:00:00 grep --color=auto nginx

master进程发送sigusr2信号,让nginx平滑升级可执行程序。可以看到nginx重新启动了一组master进程和worker进程,而新master进程是旧master进程的子进程(通过父子进程的继承关系,新master进程可以很方便地继承旧master进程的相关资源)。

?
1
2
3
4
5
6
7
[root@localhost conf]# kill -s sigusr2 14964
[root@localhost conf]# ps -ef | grep nginx
root     14964     1  0 22:25 ?        00:00:00 nginx: master process ./nginx
nobody   14965 14964  0 22:25 ?        00:00:00 nginx: worker process
root     15019 14964  0 23:18 ?        00:00:00 nginx: master process ./nginx
nobody   15020 15019  0 23:18 ?        00:00:00 nginx: worker process
root     15022  1521  0 23:19 pts/0    00:00:00 grep --color=auto nginx

并且nginx在日志目录中存储了新旧pid文件(保存了新旧master进程的id)。

?
1
2
3
4
5
6
7
8
9
10
[root@localhost conf]# ll ../logs
总用量 16
-rw-r--r--. 1 root root 2729 1220 23:20 access.log
-rw-r--r--. 1 root root  708 1220 23:18 error.log
-rw-r--r--. 1 root root    6 1220 23:18 nginx.pid
-rw-r--r--. 1 root root    6 1220 22:25 nginx.pid.oldbin
[root@localhost conf]# cat ../logs/nginx.pid
15019
[root@localhost conf]# cat ../logs/nginx.pid.oldbin
14964

给旧master进程发送sigwinch信号,让旧master进程关闭旧worker进程。

?
1
2
3
4
5
6
[root@localhost conf]# kill -s sigwinch 14964
[root@localhost conf]# ps -ef | grep nginx
root     14964     1  0 22:25 ?        00:00:00 nginx: master process ./nginx
root     15019 14964  0 23:18 ?        00:00:00 nginx: master process ./nginx
nobody   15020 15019  0 23:18 ?        00:00:00 nginx: worker process
root     15030  1521  0 23:27 pts/0    00:00:00 grep --color=auto nginx

现在访问http://192.168.1.199/,会响应404

Nginx热部署的实现

而访问http://192.168.1.199/nacos,会访问到nacos服务。

Nginx热部署的实现

如果升级版本没有问题,就可以给旧master进程发送sigquit信号,让旧master进程关闭,这样就只剩下新master进程和新worker进程,实现了nginx的热部署。

?
1
2
3
4
5
[root@localhost conf]# kill -s sigquit 14964
[root@localhost conf]# ps -ef | grep nginx
root     15019     1  0 23:18 ?        00:00:00 nginx: master process ./nginx
nobody   15020 15019  0 23:18 ?        00:00:00 nginx: worker process
root     15034  1521  0 23:31 pts/0    00:00:00 grep --color=auto nginx

如果升级版本有问题,需要回滚到之前的版本,就可以给旧master进程发送sighup信号,因为博主重新进行了测试,所以进程号都变了,但很显然旧master进程重新创建了旧worker进程,并且进行版本升级的masterworker进程没有被关闭。

?
1
2
3
4
5
6
7
[root@localhost conf]# kill -s sighup 15084
[root@localhost conf]# ps -ef | grep nginx
root     15084     1  0 1220 ?      00:00:00 nginx: master process ./nginx
root     15106 15084  0 1220 ?      00:00:00 nginx: master process ./nginx
nobody   15107 15106  0 1220 ?      00:00:00 nginx: worker process
nobody   15131 15084  0 00:02 ?        00:00:00 nginx: worker process
root     15141  1521  0 00:09 pts/0    00:00:00 grep --color=auto nginx

给新master进程发送sigquit信号,让新master进程关闭,这样就只剩下旧master进程和新创建的旧worker进程,实现了回滚。

?
1
2
3
4
5
[root@localhost conf]# kill -s sigquit 15106
[root@localhost conf]# ps -ef | grep nginx
root     15084     1  0 1220 ?      00:00:00 nginx: master process ./nginx
nobody   15131 15084  0 00:02 ?        00:00:00 nginx: worker process
root     15159  1521  0 00:25 pts/0    00:00:00 grep --color=auto nginx

回滚成功。

Nginx热部署的实现

还需要对版本回滚(即博主这里的配置文件回滚,不然下次重启就会出问题)。

?
1
2
[root@localhost conf]# cp -f nginx_old.conf nginx.conf
cp:是否覆盖"nginx.conf"? y

为什么给旧master进程发送sighup信号,旧master进程重新创建的worker进程没有重新读取配置文件?下面是官方的说明:

send the hup signal to the old master process. the old master process will start new worker processes without re-reading the configuration. after that, all new processes can be shut down gracefully, by sending the quit signal to the new master process.

向旧master进程发送sighup信号。旧master进程将启动新worker进程,而无需重新读取配置。之后,通过向新master进程发送sigquit信号,所有新进程都可以正常关闭。

如果不存在新进程的情况下(只有一组masterworker进程),修改配置文件,再向master进程发送sighup信号,看是否会重新加载配置文件。

Nginx热部署的实现

?
1
[root@localhost conf]# kill -s sighup 15084

很显然配置文件被重新加载了,由于博主还没有看源码,只能猜测nginx的实现(如果说错了,请大家评论补充),nginx应该是根据当前是否在进行热部署(存在新master进程),来决定sighup信号是否需要重新加载配置文件。

Nginx热部署的实现

到此这篇关于nginx热部署的实现的文章就介绍到这了,更多相关nginx热部署内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://blog.csdn.net/qq_37960603/article/details/122050480

延伸 · 阅读

精彩推荐