zoukankan      html  css  js  c++  java
  • 运行和控制Nginx——命令行参数和信号

    参考资料:

      Nginx中文文档: http://www.nginx.cn/nginxchscommandline 

      Nginx的启动、停止、平滑重启、信号控制和平滑升级:http://zachary-guo.iteye.com/blog/1358312

    命令行参数:

    常用命令:

      -c filename:设置配置文件。

      -t :不运行,而仅仅测试配置文件。nginx 将检查配置文件的语法的正确性,并尝试打开配置文件中所引用到的文件。

      -s :传递一个信号,stop快速关闭,quit从容关闭,reopen重新打开日志文件、用于切换日志文件,reload重载配置文件。

      -v :显示nginx的版本。

      -V:显示nginx的版本,编译器版本和配置参数

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

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

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

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

    控制信号:

      可以使用信号系统来控制主进程。默认,nginx 将其主进程的 pid 写入到 /usr/local/nginx/logs/nginx.pid 文件中。通过传递参数给 ./configure 或使用 pid 指令,来改变该文件的位置。 

    主进程可以处理以下的信号:

    TERM,INT 快速关闭
    QUIT 从容关闭
    HUP

    重新配置

    用新的配置开始新的工作进程

    从容关闭旧的工作进程

    USR1 重新打开日志文件,在切割日志时用途较大
    USR2 平滑升级可执行程序
    WINCH 从容关闭工作进程

      

    启动

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

    [root@localhost ~]# alias nginx='/usr/local/nginx/sbin/nginx'
    [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信号给旧的主进程,使其退出而只留下新的服务器运行。

  • 相关阅读:
    VIJOS-P1340 拯救ice-cream(广搜+优先级队列)
    uva 11754 Code Feat
    uva11426 GCD Extreme(II)
    uvalive 4119 Always an Interger
    POJ 1442 Black Box 优先队列
    2014上海网络赛 HDU 5053 the Sum of Cube
    uvalive 4795 Paperweight
    uvalive 4589 Asteroids
    uvalive 4973 Ardenia
    DP——数字游戏
  • 原文地址:https://www.cnblogs.com/shea/p/6898021.html
Copyright © 2011-2022 走看看