0

0

Nginx热部署如何实现

王林

王林

发布时间:2023-05-24 16:55:39

|

1424人浏览过

|

来源于亿速云

转载

关闭防火墙,让本地可以通过浏览器访问nginx服务。

[root@localhost ~]# systemctl stop firewalld

Nginx热部署如何实现

信号量

查看信号量:

[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一份副本)。

[root@localhost ~]# cd /usr/local/nginx/conf/
[root@localhost conf]# ll
总用量 68
-rw-r--r--. 1 root root 1077 12月 20 20:24 fastcgi.conf
-rw-r--r--. 1 root root 1077 12月 20 20:24 fastcgi.conf.default
-rw-r--r--. 1 root root 1007 12月 20 20:24 fastcgi_params
-rw-r--r--. 1 root root 1007 12月 20 20:24 fastcgi_params.default
-rw-r--r--. 1 root root 2837 12月 20 20:24 koi-utf
-rw-r--r--. 1 root root 2223 12月 20 20:24 koi-win
-rw-r--r--. 1 root root 5231 12月 20 20:24 mime.types
-rw-r--r--. 1 root root 5231 12月 20 20:24 mime.types.default
-rw-r--r--. 1 root root 2656 12月 20 21:26 nginx.conf
-rw-r--r--. 1 root root 2656 12月 20 20:24 nginx.conf.default
-rw-r--r--. 1 root root  636 12月 20 20:24 scgi_params
-rw-r--r--. 1 root root  636 12月 20 20:24 scgi_params.default
-rw-r--r--. 1 root root  664 12月 20 20:24 uwsgi_params
-rw-r--r--. 1 root root  664 12月 20 20:24 uwsgi_params.default
-rw-r--r--. 1 root root 3610 12月 20 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热部署如何实现

Codapp 扫码点餐小程序(含H5系统)
Codapp 扫码点餐小程序(含H5系统)

Codapp 外卖点餐系统是一款专为快餐店、奶茶店、咖啡店、糕点店等商户打造的移动点餐解决方案,支持自提与外卖两种模式,可快速部署上线使用。 该系统支持微信、支付宝支付,并接入腾讯地图与百度地图,支持第三方配送(如达达)与商家自主配送,助力门店实现智能点单与订单管理。 功能特点: 微信小程序&H5移动端双端点餐:无需下载 App,直接扫码下单 支持多门店管理:一套系统可管理多家门

下载

查看Nginx的进程:

[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进程的相关资源)。

[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)。

[root@localhost conf]# ll ../logs
总用量 16
-rw-r--r--. 1 root root 2729 12月 20 23:20 access.log
-rw-r--r--. 1 root root  708 12月 20 23:18 error.log
-rw-r--r--. 1 root root    6 12月 20 23:18 nginx.pid
-rw-r--r--. 1 root root    6 12月 20 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进程。

[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的热部署。

[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进程没有被关闭。

[root@localhost conf]# kill -s SIGHUP 15084
[root@localhost conf]# ps -ef | grep nginx
root     15084     1  0 12月20 ?      00:00:00 nginx: master process ./nginx
root     15106 15084  0 12月20 ?      00:00:00 nginx: master process ./nginx
nobody   15107 15106  0 12月20 ?      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进程,实现了回滚。

[root@localhost conf]# kill -s SIGQUIT 15106
[root@localhost conf]# ps -ef | grep nginx
root     15084     1  0 12月20 ?      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热部署如何实现

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

[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热部署如何实现

[root@localhost conf]# kill -s SIGHUP 15084

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

Nginx热部署如何实现

相关专题

更多
nginx 重启
nginx 重启

nginx重启对于网站的运维来说是非常重要的,根据不同的需求,可以选择简单重启、平滑重启或定时重启等方式。本专题为大家提供nginx重启的相关的文章、下载、课程内容,供大家免费下载体验。

229

2023.07.27

nginx 配置详解
nginx 配置详解

Nginx的配置是指设置和调整Nginx服务器的行为和功能的过程。通过配置文件,可以定义虚拟主机、HTTP请求处理、反向代理、缓存和负载均衡等功能。Nginx的配置语法简洁而强大,允许管理员根据自己的需要进行灵活的调整。php中文网给大家带来了相关的教程以及文章,欢迎大家前来学习阅读。

496

2023.08.04

nginx配置详解
nginx配置详解

NGINX与其他服务类似,因为它具有以特定格式编写的基于文本的配置文件。本专题为大家提供nginx配置相关的文章,大家可以免费学习。

498

2023.08.04

tomcat和nginx有哪些区别
tomcat和nginx有哪些区别

tomcat和nginx的区别:1、应用领域;2、性能;3、功能;4、配置;5、安全性;6、扩展性;7、部署复杂性;8、社区支持;9、成本;10、日志管理。本专题为大家提供相关的文章、下载、课程内容,供大家免费下载体验。

227

2024.02.23

nginx报404怎么解决
nginx报404怎么解决

当访问 nginx 网页服务器时遇到 404 错误,表明服务器无法找到请求资源,可以通过以下步骤解决:1. 检查文件是否存在且路径正确;2. 检查文件权限并更改为 644 或 755;3. 检查 nginx 配置,确保根目录设置正确、没有冲突配置等等。本专题为大家提供相关的文章、下载、课程内容,供大家免费下载体验。

333

2024.07.09

Nginx报404错误解决方法
Nginx报404错误解决方法

解决方法:只需要加上这段配置:try_files $uri $uri/ /index.html;即可。想了解更多Nginx的相关内容,可以阅读本专题下面的文章。

3507

2024.08.07

nginx部署php项目教程汇总
nginx部署php项目教程汇总

本专题整合了nginx部署php项目教程汇总,阅读专题下面的文章了解更多详细内容。

7

2026.01.13

nginx配置文件详细教程
nginx配置文件详细教程

本专题整合了nginx配置文件相关教程详细汇总,阅读专题下面的文章了解更多详细内容。

5

2026.01.13

Java 桌面应用开发(JavaFX 实战)
Java 桌面应用开发(JavaFX 实战)

本专题系统讲解 Java 在桌面应用开发领域的实战应用,重点围绕 JavaFX 框架,涵盖界面布局、控件使用、事件处理、FXML、样式美化(CSS)、多线程与UI响应优化,以及桌面应用的打包与发布。通过完整示例项目,帮助学习者掌握 使用 Java 构建现代化、跨平台桌面应用程序的核心能力。

36

2026.01.14

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
进程与SOCKET
进程与SOCKET

共6课时 | 0.3万人学习

nginx浅谈
nginx浅谈

共15课时 | 0.8万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号