欢迎大家扫码关注我的微信公众号:
Nginx 服务器的安装部署(二)-目录
- 一、Nginx 服务的信号控制
- 1.1 获取 Nginx 服务主进程 PID
- 1.2 Nginx 服务可接收的信号
- 二、Nginx 服务的启动
- 三、Nginx 服务的停止
- 3.1 停止方式一:
- 3.2 停止方式二:
- 3.3 停止方式三:
- 四、Nginx 服务的重启
- 4.1 直接重启
- 4.2 平滑重启
- 五、Nginx 服务器的升级
Nginx 服务的启动、 重启和停止的方法有多种, 其中有一类是通过信号机制来实现的。
一、Nginx 服务的信号控制
Nginx 服务在运行时, 会保持一个主进程和一个或多个 worker process 工作进程。 我们通过给 Nginx 服务的主进程发送信号就能控制服务的启停了。 所以我们首先需要知道 Nginx 服务主进程的进程号 PID。
1.1 获取 Nginx 服务主进程 PID
获取 PID 的两种方法:
- 在 Nginx 服务启动后, 默认在 Nginx 服务器安装目录下的 logs 目录中会产生名为 nginx.pid 的文件, 此文件中保持的就是 Nginx 服务主进程的 PID:
# cat /Nginx/logs/nginx.pid
10472
- 通过 Linux 平台下查看进程的工具 ps:
# ps -ef|grep nginx
root 10472 1437 0 20:25 ? 00:00:00 nginx: master process ./sbin/nginx
nobody 10473 10472 0 20:25 ? 00:00:00 nginx: worker process
root 10475 2272 0 20:25 pts/4 00:00:00 grep --color=auto nginx
此时我们看到, 第一行是 master process, root 后面的 10472 就是主进程的 PID, 与 nginx.pid 文件中保持的主进程 PID 相同。
1.2 Nginx 服务可接收的信号
Nginx 服务可接收的信号如下表所示:
信号 | 作用 |
---|---|
TERM 或 INT | 快速停止 Nginx 服务 |
QUIT | 平缓停止 Nginx 服务 |
HUP | 使用新的配置文件启动进程, 之后平缓停止原有进程, 也就是所谓的 “平滑重启” |
USR1 | 重新打开日志文件, 常用于日志切割 |
USR2 | 使用新版本的 Nginx 文件启动服务, 之后平缓停止原有 Nginx 进程, 也就是所谓的 “平滑升级” |
WINCH | 平缓停止 worker process, 用于 Nginx 服务器平滑升级 |
向 Nginx 服务主进程发送信号, 我们可以使用 kill:
# kill SIGNAL PID
其中:
- SIGNAL: 用于指定信号, 即上表中的某一个信号;
- PID: Nginx 服务主进程的 PID;
PID 可以写真正的 Nginx 主进程 PID, 也可以指定 nginx.pid 文件的所在路径, 如:
# kill SIGNAL `/Nginx/logs/nginx.pid`
二、Nginx 服务的启动
我们可以在 /Nginx 目录下执行 ./sbin/nginx -h 来查看二进制文件 nginx 的相关用法:
root@ubuntu:/Nginx# sbin/nginx -h
nginx version: nginx/1.14.0
Usage: nginx [-?hvVtTq] [-s signal] [-c filename] [-p prefix] [-g directives]
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: /Nginx/)
-c filename : set configuration file (default: conf/nginx.conf)
-g directives : set global directives out of configuration file
其中:
-s signal: 向 Nginx 服务的主进程发送信号;
-p prefix: 用来改变 Nginx 的安装路径, 常用在平滑升级 Nginx 服务器的场合;
-c filename: 用来指定启动 Nginx 服务使用的配置文件;
-g directives: 用来补充 Nginx 的配置文件, 向 Nginx 服务指定启动时应用于全局的配置;
当我们使用默认的配置文件启动 Nginx 服务时, 我们可以这么做:
# ./sbin/nginx
如果没有任何错误信息输出, 就表示我们已经启动了 Nginx 服务。 此时我们可以使用 ps -ef|grep nginx 来查看Nginx 服务的进程状态:
root@ubuntu:/Nginx# ps -ef|grep nginx
root 10472 1437 0 20:25 ? 00:00:00 nginx: master process ./sbin/nginx
nobody 10473 10472 0 20:25 ? 00:00:00 nginx: worker process
root 10475 2272 0 20:25 pts/4 00:00:00 grep --color=auto nginx
三、Nginx 服务的停止
停止 Nginx 服务有两种方法:
- 快速停止: 立即停止当前 Nginx 服务正在处理的请求, 马上丢弃连接, 停止工作(比较粗鲁);
- 平缓停止: 允许 Nginx 服务将当前正在处理的网络请求处理完成, 但不再接收新的请求, 之后关闭连接, 停止工作(比较人性化);
3.1 停止方式一:
通过 kill 发送信号停止 Nginx 服务:
# kill TERM | INT | QUIT PID # PID 为 Nginx 服务主进程的进程号, 如: 10472
3.2 停止方式二:
通过 kill sigkill + 进程号的方式停止 Nginx 服务:
# kill sigkill PID # 其中 PID 是 Nginx 服务的主进程号, 如: 10472
3.3 停止方式三:
通过 kill 掉所有 Nginx 服务的方式:
# kill -9 `pidof nginx`
【注意】kill -9 的方式不建议使用
四、Nginx 服务的重启
4.1 直接重启
重启 Nginx 服务时, 我们可以先将 Nginx 服务停止, 然后再开启。(当前路径: /Nginx)
root@ubuntu:/Nginx# ps -ef|grep nginx
root 115741 1437 0 21:18 ? 00:00:00 nginx: master process ./sbin/nginx
nobody 115742 115741 0 21:18 ? 00:00:00 nginx: worker process
root 115777 2272 0 21:21 pts/4 00:00:00 grep --color=auto nginx
root@ubuntu:/Nginx# kill -9 `pidof nginx`
root@ubuntu:/Nginx# ./sbin/nginx
root@ubuntu:/Nginx# ps -ef|grep nginx
root 115780 1437 0 21:22 ? 00:00:00 nginx: master process ./sbin/nginx
nobody 115781 115780 0 21:22 ? 00:00:00 nginx: worker process
root 115783 2272 0 21:22 pts/4 00:00:00 grep --color=auto nginx
或者使用:
# ./sbin/nginx -s reload
也可以实现 Nginx 服务的重启。
【注意】直接重启也可以达到重启 Nginx 服务的目的, 但是这样做, 在 Nginx 服务重启的过程中用户无法访问服务器, 这是最大的缺点。
4.2 平滑重启
Nginx 服务进程接收到平滑重启的信号后, 首先读取新的 Nginx 配置文件, 如果配置语法正确, 则启动新的 Nginx 服务, 然后平缓关闭旧的服务进程; 如果新的 Nginx 配置有问题, 将显示错误, 仍然使用旧的 Nginx 进程提供服务。(当前路径: /Nginx)
# ./sbin/nginx -g HUP [-c newconfFile]
HUP 信号用于发送平滑重启信号
newconfFile, 可选项, 用于指定新的配置文件的路径
或者, 使用了新的配置文件替换了旧的配置文件后, 使用:
# kill HUP `/Nginx/logs/nginx.pid`
也可以实现平滑重启。
五、Nginx 服务器的升级
Nginx 服务器的升级, 我们同样可以先关闭服务, 然后使用升级后的配置文件重启 Nginx 服务, 缺点同上。
我们要说的是平滑升级。
平滑升级的过程: Nginx 服务接收到 USR2 (平滑升级信号)信号后,首先将旧的 nginx.pid 文件添加后缀 .oldbin, 变为 nginx.pid.oldbin; 然后执行新版本 Nginx 服务器的二进制文件启动服务。 如果新的服务启动成功, 系统中将有新旧两个 Nginx 服务共同提供 Web 服务。 之后需要向旧的 Nginx 服务进程发送 WINCH (平缓停止 worker process 信号)信号, 使旧的 Nginx 服务平滑停止, 并删除 nginx.pid.oldbin 文件。 在发送 WINCH 信号之前, 可以随时停止新的 Nginx 服务。
实现 Nginx 服务器平滑升级的命令:
# ./sbin/nginx -g USR2
或者使用:
# kill USR2 `/Nginx/logs/nginx.pid`
通过 ps -ef | grep nginx 查看新的 Nginx 服务启动正常, 再使用:
# ./sbin/nginx -g WINCH
或者使用:
# kill WINCH `/Nginx/logs/nginx.pid`
这样就在不停止提供 Nginx 服务的前提下完成了 Nginx 服务器的平滑升级。