Home  >  Article  >  Operation and Maintenance  >  Running and controlling Nginx - command line parameters and signals

Running and controlling Nginx - command line parameters and signals

巴扎黑
巴扎黑Original
2017-06-23 14:15:081759browse

Reference materials:

Nginx Chinese documentation:

Nginx start, stop, smooth restart, signal control and smooth upgrade:

Command line parameters:

Common commands:

-c filename: Set the configuration file.

 -t: Do not run, but only test the configuration file. nginx will check the syntax of the configuration file for correctness and try to open the files referenced in the configuration file.

-s: Pass a signal, stop closes quickly, quit closes calmly, reopen reopens the log file, switches log files, and reloads the configuration file.

 -v: Display the nginx version.

 -V: Display nginx version, compiler version and configuration parameters

------------------------ -------------------------------------------------- -------------------------------------------------- -------------------------------------------------- ---

[root@localhost /]# alias nginx='/usr/local/nginx/sbin/nginx'<br>[root@localhost /]# alias

<br>
[root@localhost /]# nginx -h 
nginx version: nginx/1.11.13Usage: 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[root@localhost /]#

---------------------------------- -------------------------------------------------- -------------------------------------------------- ------------------------------------------

control signal :

You can use the signal system to control the main process. By default, nginx writes the pid of its main process to the /usr/local/nginx/logs/nginx.pid file. Change the location of this file by passing arguments to ./configure or using the pid command.

The main process can handle the following signals:

TERM,INT Quick shutdown
QUIT Close gracefully
HUP

Reconfigure

Start a new worker process with a new configuration

Close the old worker process calmly

USR1 Reopen the log file, which is more useful when cutting logs
USR2 Smoothly upgrade the executable program
WINCH Close the worker process gracefully

 

  

 

 

 

 

 

 

 

启动

  启动代码格式:nginx安装目录地址 -c nginx配置文件地址

[root@localhost ~]# alias nginx=&#39;/usr/local/nginx/sbin/nginx&#39;
[root@localhost ~]# nginx -c /usr/local/nginx/conf/nginx.conf
[root@localhost ~]# ps -ef|grep nginx
root      2073     1  0 10:37 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
nobody    2074  2073  0 10:37 ?        00:00:00 nginx: worker process                                          
root      2076  2022  0 10:37 pts/0    00:00:00 grep nginx
[root@localhost ~]#   

 停止

  从容停止:

# ` 字符是数字键盘 1 字符左边的那个字符
[root@localhost ~]# kill -QUIT `cat /usr/local/nginx/logs/nginx.pid`

  快速停止:

[root@localhost ~]# kill -TERM `cat /usr/local/nginx/logs/nginx.pid`

    或

[root@localhost ~]# kill -INT `cat /usr/local/nginx/logs/nginx.pid`

  强行停止

[root@localhost ~]# kill -9 nginx

重启

  1.普通重启:关闭进程,修改配置后,重启进程

  2.重新加载配置文件,不重启进程,不会停止处理请求

  3.平滑更新nginx二进制,不会停止处理请求

=========================================================================

注意:在重载前,测试一下配置文件

# -t 参数将检查配置文件的语法是否正确,默认会检查 /usr/local/nginx/conf/nginx.conf 文件
[root@localhost ~]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
# 如果要对指定配置文件进行语法检查,可以继续添加 -c 参数
[root@localhost ~]# /usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@localhost ~]#

=========================================================================

nginx的平滑重启

  如果改变了nginx的配置文件,想重启nginx,同样可以发送系统型号给nginx主进程的方式来进行,重启之前,要确认配置文件是否正确

[root@localhost ~]# kill -HUP 【nginx主进程号】

  当 nginx 接收到 HUP 信号时,它会尝试先解析配置文件,如果成功,就应用新的配置文件(例如,重新打开日志文件或监听的套接字)。之后,nginx 运行新的工作进程并从容关闭旧的工作进程。通知工作进程关闭监听套接字,但是继续为当前连接的客户提供服务。所有的客户端的服务完成后,旧的工作进程被关闭。如果新的配置文件应用失败,nginx 将继续使用旧的配置文件进行工作。

nginx的平滑升级

  当需要将正在运行中的nginx升级、添加/删除服务器模块时,可以在不中断服务的情况下,使用新的nginx可执行程序替换旧的:

  1.使用新的可执行程序替换旧的可执行程序,对于编译安装的nginx,可以将新版本编译安装到nginx安装路径中,替换之前,备份一下旧的可执行文件

  2.发送以下指令:

[root@localhost ~]# kill -USR2 【旧版本的nginx主进程号】

  3.旧版本的nginx的主进程将重命名它的pid文件为.oldbin(例如:/usr/local/nginx/logs/nginx.pid.oldbin),然后执行新版本的nginx可执行程序,依次启动新的主进程和新的工作进程。

  4.此时,新、旧版本的nginx实例会同时运行,共同处理输入的请求,要逐步停止旧版本的nginx,必须发送WINCH信号给旧的主进程,然后,它的工作进程就开始从容关闭:

[root@localhost ~]# kill -WINCH 【旧版本的nginx主进程号】

  5.一段时间后,旧的工作进程(worker process)处理了所有已连接的请求后退出,仅由新的工作进程来处理输入的请求了。

  6.这时候,可以决定是使用新版本,还是恢复到旧版了:

    kill -HUP  【旧的主进程号】:nginx将在不重载配置文件的情况下启动它的工作进程

    kill -QUIT 【新的主进程号】:从容关闭其工作进程(worker process)

    kill -TERM 【新的主进程号】:强制退出

    kill 【新的主进程号或旧的主进程号】:如果因为某些原因新的工作进程不能退出,则向其发送kill信号

  新的主进程退出后,旧的主进程会移除.oldbin前缀,恢复为它的.pid文件,这样,一切就都恢复到升级之前了。

   如果尝试升级成功,而你也希望保留新的服务器时,可发送QUIT信号给旧的主进程,使其退出而只留下新的服务器运行。

The above is the detailed content of Running and controlling Nginx - command line parameters and signals. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn