计算机网络/计算机科学与应用/系统/运维/开发

Nginx 启动与停止

1、Nginx 启停控制

(Nginx 服务运行时,会保持一个主进程或多个worker process工作进程)

通过给Nginx发送信号的方式控制,发送信号,首先要知道主进程的PID号


获取主进程PID号的2种方式:

第一种:默认在nginx安装目录下logs目录中会产生文件名为nginx.pid文件 保存有主进程PID

[root@localhost nginx]# cd logs/
[root@localhost logs]# ll
total 16
-rw-r--r--. 1 root root 4260 Sep  9 00:38 access.log
-rw-r--r--. 1 root root 1301 Sep  9 00:38 error.log
-rw-r--r--. 1 root root    5 Sep  9 00:38 nginx.pid
[root@localhost logs]# cat nginx.pid 
3269

第二种:查看进程ps

[root@localhost sbin]# ps -ef| grep nginx
root       3269      1  0 00:38 ?        00:00:00 nginx: master process ./nginx
www        3270   3269  0 00:38 ?        00:00:00 nginx: worker process
root       3272   3208  0 00:38 pts/1    00:00:00 grep --color=auto nginx


2、Nginx服务信号值

TERM或INT 快速停止Nginx服务

QUIT 平缓停止Nginx服务

HUP 使用新的配置文件启动进程,之后平缓停止原有进程,平滑启动

USR1 重新打开日志文件,用于日志切割

USR2 使用新版本的Nginx文件启动服务,之后平缓停止原有Nginx进程 平滑升级

WINCH 平缓停止worker process 用于Nginx服务器平滑升级


3、向Nginx服务主进程发送信号的2种方法

第一种:二进制方式

第二种:kill命令发送信号

kill SIGNAL PID


说明:

SIGNAL 指定信号

PID Nginx主进程号 或者 动态使用nginx.pid PID号

kill SIGNAL `filepath` (filepath 为nginx.pid路径)


实例:

[root@localhost sbin]# kill QUIT 3613


4、Nginx服务启动

[root@localhost sbin]# ./nginx -h
nginx version: nginx/1.16.1
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: /usr/local/nginx/)
  -c filename   : set configuration file (default: conf/nginx.conf)
  -g directives : set global directives out of configuration file


启动nginx服务:

./sbin/nginx


5、Nginx停止的2种方式

第一种:快速停止 立即停止当前服务正在处理的所有网络请求,丢弃链接

第二种:平缓停止  将当前工作完成,不在接受新的请求 之后关闭链接

./sbin/nginx -g TERM | INT | QUIT


TERM 和 INT 用于快速停止,QUIT用于平缓停止,或者

kill TERM | INT | QUIT `/nginx/logs/nginx.pid`
kill -9 | SIGKILL `/nginx/logs/nginx.pid`


6、Nginx重启

./sbin/nginx -g HUP [-c newConfFile]

newConfFile 指定新配置文件的路径

或者 使用新的配置文件代替旧的配置文件后

kill HUP `/nginx/logs/nginx.pid`



你努力了什么,也就成就了什么,与其羡慕别人,不如蜕变自己。

评论

^