nginx 学习笔记(3) nginx管理

时间:2022-07-01 19:12:32

nginx可以通过向其发送信号来进行管理。默认情况下主进程的进程ID写到文件/usr/local/nginx/logs/nginx.pid中。当然也可以在配置文件中自定义该pid文件,自定义使用pid指令来进行修改。主进程支持如下信号:

TERM, INT fast shutdown
QUIT graceful shutdown
HUP changing configuration, keeping up with a changed time zone (only for FreeBSD and Linux), starting new worker processes with a new configuration, graceful shutdown of old worker processes
USR1 re-opening log files
USR2 upgrading an executable file
WINCH graceful shutdown of worker processes

尽管不是必须,单个的工作进程同样可以通过下列信号进行控制。工作进程支持如下信号:

TERM, INT fast shutdown
QUIT graceful shutdown
USR1 re-opening log files
WINCH abnormal termination for debugging (requires debug_points to be enabled)

改变配置文件

为使nginx重新读取配置文件,可以想主进程发送一个HUB信号。主进程首先检测语法的有效性,然后尝试应用到新的配置中即打开日志文件和新的监听套接字:若失败,主进程回滚到改变前的配置,并继续使用旧的配置运行。若成功则开启新的工作进程,给旧的工作进程发送完整性关闭命令,旧的工作进程关闭监听套接字,继续处理旧的请求并返回给客户端,当所有的客户端请求处理完后,旧的工作进程关闭。

举个例子,假定nginx运行在FreeBSD 4.X之上,命令如下:

ps axw -o pid,ppid,user,%cpu,vsz,wchan,command | egrep '(nginx|PID)'

打印出如下内容:

  PID  PPID USER    %CPU   VSZ WCHAN  COMMAND
33126 1 root 0.0 1148 pause nginx: master process /usr/local/nginx/sbin/nginx
33127 33126 nobody 0.0 1380 kqread nginx: worker process (nginx)
33128 33126 nobody 0.0 1364 kqread nginx: worker process (nginx)
33129 33126 nobody 0.0 1364 kqread nginx: worker process (nginx)

向主进程发送HUP信号后,打印出如下内容:

  PID  PPID USER    %CPU   VSZ WCHAN  COMMAND
33126 1 root 0.0 1164 pause nginx: master process /usr/local/nginx/sbin/nginx
33129 33126 nobody 0.0 1380 kqread nginx: worker process is shutting down (nginx)
33134 33126 nobody 0.0 1368 kqread nginx: worker process (nginx)
33135 33126 nobody 0.0 1368 kqread nginx: worker process (nginx)
33136 33126 nobody 0.0 1368 kqread nginx: worker process (nginx)

可以看到PID为33129的旧工作进程正在处理请求中。过一段时间后再观察,发现它已经关闭掉了:

 PID  PPID USER    %CPU   VSZ WCHAN  COMMAND
33126 1 root 0.0 1164 pause nginx: master process /usr/local/nginx/sbin/nginx
33134 33126 nobody 0.0 1368 kqread nginx: worker process (nginx)
33135 33126 nobody 0.0 1368 kqread nginx: worker process (nginx)
33136 33126 nobody 0.0 1368 kqread nginx: worker process (nginx)

 ---出去一段时间