2025年2月25日 星期二 甲辰(龙)年 腊月廿五 设为首页 加入收藏
rss
您当前的位置:首页 > 计算机 > 服务器 > Nginx

Nginx基本命令&不停机版本升级

时间:11-09来源:作者:点击数:12
城东书院 www.cdsy.xyz

在上一篇文章Ubuntu安装Nginx(在线安装&源码编译安装)中,我们已经通过源码编译安装nginx,其nginx执行文件的地址为:/usr/local/nginx

1.Nginx工作模型

我们先来看看nginx启动之后的进程情况:

  • ps -ef|grep nginx
  • # 输出结果
  • root 1036 1 0 02:33 ? 00:00:00 nginx: master process ./nginx
  • nobody 1037 1036 0 02:33 ? 00:00:00 nginx: worker process

可以看到,目前运行的nginx存在一个master进程和一个worker进程,它们有着明确的分工:

  • master进程:读取和评估配置。
  • worker进程:处理请求,工作进程的数量可以在配置文件中定义。

nginx采用基于事件的模型并依赖于操作系统的机制高效地在工作进程之间分发请求。

2.Nginx基本命令

2.1 nginx命令

一般所有的软件,都有类似于help的指令,nginx中我们使用nginx -h或者nginx -?即可进行查看。

  • # 先进入nginx执行文件目录
  • cd /usr/local/nginx
  • # ./nginx -h
  • Options:
  • -?,-h : this help
  • -v : show version and exit
  • -V : show version and configure options then exit
  • -t : test configuration and exit
  • -T : test configuration, dump it and exit
  • -q : suppress non-error messages during configuration testing
  • -s signal : send signal to a master process: stop, quit, reopen, reload
  • -p prefix : set prefix path (default: /usr/local/nginx/)
  • -c filename : set configuration file (default: /usr/local/nginx/nginx.conf)
  • -g directives : set global directives out of configuration file

可以看到,nginx为我们列出了参数指令的含义。

参数 含义
-?,-h 查看帮助信息
-v 查看nginx版本
-V 查看nginx版本以及配置选项
-t 测试配置文件语法是否正确
-T 测试配置文件语法是否正确,并输出配置内容
-q 测试配置文件过程中不显示非错误信息
-s 向master进程发送命令,stop:快速关闭,quit:优雅关闭,reopen:重新打开日志,reload:重新加载配置文件
-p 设置nginx执行路径,默认为/usr/local/nginx/
-c 以指定配置文件启动(/usr/local/nginx/nginx.conf)
-g 从设置配置文件中全局指令(./nginx -g “pid logs/new.pid”,便可以在启动nginx时修改配置文件中的默认配置)

在工作中,可能经常涉及到配置文件的修改,因此,使用比较多的命令:

  • nginx -t

修改配置文件后,先测试配置文件语法的正确性

  • nginx -s reload

修改配置文件后,通知master进程重新加载配置文件,master进程会启动新的worker进程,并向旧的worker进程发送关闭命令,旧的worker进程收到关闭命令后,停止接收新的连接直到当前工作结束后退出。

2.2 kill命令

除了使用nginx命令,我们还可以通过kill命令向nginx的master进程和worker进程发送信号,其中某些命令的效果与执行nginx -s signal的效果是一样的。

master进程支持的信号:

信号 含义
TERM, INT 快速关闭。与nginx -s stop效果一致。kill -s TERM pidkill -s INT pid
QUIT 优雅关闭。与nginx -s quit效果一致。kill -s QUIT pid
HUP 重新加载配置,与nginx -s reload效果一致。kill -s HUP pid
USR1 重新打开日志。kill -s USR1 pid
USR2 升级nginx可执行文件,可以用来不停机升级nginx版本。kill -s USR2 pid
WINCH 优雅关闭worker进程。kill -s WINCH pid

worker进程支持的信号:

信号 含义
TERM, INT 快速关闭。kill -s TERM pidkill -s INT pid
QUIT 优雅关闭。kill -s QUIT pid
USR1 重新打开日志。kill -s USR1 pid
WINCH 调试异常终止。kill -s WINCH pid

3.Nginx版本升级

1)查看当前版本

  • ./nginx -v
  • nginx version: nginx/1.18.0

2)准备新版本

下载链接:http://nginx.org/download/nginx-1.22.1.tar.gz

3)解压缩

  • tar zxvf nginx-1.22.1.tar.gz

4)设置configure

  • cd /home/stone/nginx-1.22.1/
  • sudo ./configure --sbin-path=/usr/local/nginx/nginx --conf-path=/usr/local/nginx/nginx.conf --pid-path=/usr/local/nginx/nginx.pid

5)编译

  • sudo make

升级过程,只需要编译,不需要安装,否则会覆盖已经安装的版本

6)查看新编译的版本

  • cd objs/
  • ls
  • autoconf.err Makefile nginx nginx.8 ngx_auto_config.h ngx_auto_headers.h ngx_modules.c ngx_modules.o src

可以看到,该目录下也存在一个nginx文件,运行nginx -v查看

  • ./nginx -v
  • nginx version: nginx/1.22.1

7)备份原nginx可执行文件

如果后续升级失败,还可以进行版本回滚

  • cd /usr/local/nginx
  • sudo mv nginx nginx.bak

8)复制新的nginx可执行文件进行替换

  • cd /home/stone/nginx-1.22.1/objs
  • sudo cp nginx /usr/local/nginx/

9)查看当前nginx进程

  • ps -ef|grep nginx
  • # 输出结果
  • root 3253 1 0 11:07 ? 00:00:00 nginx: master process ./nginx
  • nobody 3254 3253 0 11:07 ? 00:00:00 nginx: worker process
  • stone 24596 3181 0 13:50 pts/1 00:00:00 grep --color=auto nginx

10)执行升级命令

  • sudo kill -s USR2 `more /usr/local/nginx/nginx.pid`

再次查看nginx进程,可以看到创建了新的master进程和worker进程,与此同时,也是将旧版本的nginx的pid保存到nginx.pid.oldbin中。

  • ps -ef|grep nginx
  • # 输出结果
  • root 3253 1 0 11:07 ? 00:00:00 nginx: master process ./nginx
  • nobody 3254 3253 0 11:07 ? 00:00:00 nginx: worker process
  • root 25675 3253 0 13:53 ? 00:00:00 nginx: master process ./nginx
  • nobody 25676 25675 0 13:53 ? 00:00:00 nginx: worker process

我们可以查看一下这个文件,确实,其记录了旧版本的master进程pid

  • cat /usr/local/nginx/nginx.pid.oldbin
  • 3253

此时,所有的worker进程(包括新的和旧的)继续接收请求,我们可以向旧版本的master进程发送WINCH命令以关闭其worker进程。

  • sudo kill -s winch `cat /usr/local/nginx/nginx.pid.oldbin`

再次查看nginx进程,旧版本的worker进程已经优雅退出,只剩下master进程。

  • ps -ef|grep nginx
  • root 3253 1 0 11:07 ? 00:00:00 nginx: master process ./nginx
  • root 25675 3253 0 13:53 ? 00:00:00 nginx: master process ./nginx
  • nobody 25676 25675 0 13:53 ? 00:00:00 nginx: worker process

如果新版本的worker进程运行正常,那么我们就可以向旧版本的master进程发送QUIT命令优雅退出。

  • sudo kill -s quit `cat /usr/local/nginx/nginx.pid.oldbin`

到这里,我们的nginx版本也就在不停机的情况下平滑升级完成了。

城东书院 www.cdsy.xyz
方便获取更多学习、工作、生活信息请关注本站微信公众号城东书院 微信服务号城东书院 微信订阅号
推荐内容
相关内容
栏目更新
栏目热门
本栏推荐