18 Nginx服务的命令行控制

18 Nginx服务的命令行控制

   此方式是通过Nginx安装目录的sbin下的可执行文件nginx来进行Nginx状态的控制,通过 nginx -h 查看参数

18.1 nginx -?/h

  -?和-h:显示帮助信息

[root@nginx-100 ~]# cd /usr/local/nginx/sbin/
[root@nginx-100 /usr/local/nginx/sbin]# ls -ltr
total 3736
-rwxr-xr-x 1 root root 3824976 Mar 12 23:40 nginx
[root@nginx-100 /usr/local/nginx/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: /usr/local/nginx/conf/nginx.conf)
  -g directives : set global directives out of configuration file

18.2 nginx -v/V

  -v:打印版本号信息并退出

  -V:打印版本号信息和配置信息并退出

# 仅输出版本信息
[root@nginx-100 /usr/local/nginx/sbin]# ./nginx -v
nginx version: nginx/1.16.1

# 不仅输出版本信息,还输出 gcc 构建版本和配置信息
[root@nginx-100 /usr/local/nginx/sbin]# ./nginx -V
nginx version: nginx/1.16.1
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC) 
configure arguments: --prefix=/usr/local/nginx --sbin-path=/usr/local/nginx/sbin/nginx \
--modules-path=/usr/local/nginx/modules --conf-path=/usr/local/nginx/conf/nginx.conf \
--error-log-path=/usr/local/nginx/logs/error.log --http-log-path=/usr/local/nginx/logs/access.log \
--pid-path=/usr/local/nginx/logs/nginx.pid --lock-path=/usr/local/nginx/logs/nginx.lock

18.3 nginx -t/T

  -t:测试nginx的配置文件语法是否正确并退出

  -T:测试nginx的配置文件语法是否正确并列出用到的配置文件信息然后退出

# 演示 配置语法成功时,-t 仅检查配置语法
[root@nginx-100 /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

# 演示 配置语法成功时,-T 不仅检查配置语法,并打印配置信息,主要是 nginx.conf 和 mime.types 文件
[root@nginx-100 /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
# configuration file /usr/local/nginx/conf/nginx.conf:
#user  nobody;
worker_processes  1;
..........
# configuration file /usr/local/nginx/conf/mime.types:

types {
    text/html                                        html htm shtml;
    text/css                                         css;
..........
# 演示 配置语法出错时,-t 与 -T 效果一致,提示配置出错
[root@nginx-100 /usr/local/nginx/sbin]# head -2 /usr/local/nginx/conf/nginx.conf
abc
#user  nobody;
[root@nginx-100 /usr/local/nginx/sbin]# ./nginx -t
nginx: [emerg] unknown directive "abc" in /usr/local/nginx/conf/nginx.conf:3
nginx: configuration file /usr/local/nginx/conf/nginx.conf test failed
[root@nginx-100 /usr/local/nginx/sbin]# ./nginx -T
nginx: [emerg] unknown directive "abc" in /usr/local/nginx/conf/nginx.conf:3
nginx: configuration file /usr/local/nginx/conf/nginx.conf test failed

18.4 nginx -q

  -q:在配置测试期间禁止显示非错误消息

# 演示 配置语法成功时,不显示信息,静默
[root@nginx-100 /usr/local/nginx/sbin]# ./nginx -tq
# 演示 配置语法出错时,提示配置出错
[root@nginx-100 /usr/local/nginx/sbin]# head -2 /usr/local/nginx/conf/nginx.conf
abc
#user  nobody;
[root@nginx-100 /usr/local/nginx/sbin]# ./nginx -tq
nginx: [emerg] unknown directive "abc" in /usr/local/nginx/conf/nginx.conf:3
nginx: configuration file /usr/local/nginx/conf/nginx.conf test failed

18.5 nginx -s

  -s:signal信号,后面参数

    stop [快速关闭,类似于TERM/INT信号的作用]

    quit [优雅的关闭,类似于QUIT信号的作用]

    reopen [重新打开日志文件类似于USR1信号的作用]

    reload [类似于HUP信号的作用]

# 演示 stop 立即退出
[root@nginx-100 /usr/local/nginx/sbin]# ps -ef|grep nginx
root       1552      1  0 12:19 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
nobody     1553   1552  0 12:19 ?        00:00:00 nginx: worker process
root       1893   1451  0 17:06 pts/0    00:00:00 grep --color=auto nginx
[root@nginx-100 /usr/local/nginx/sbin]# ./nginx -s stop
[root@nginx-100 /usr/local/nginx/sbin]# ps -ef|grep nginx
root       1896   1451  0 17:06 pts/0    00:00:00 grep --color=auto nginx
# 演示 quit "优雅"退出
[root@nginx-100 /usr/local/nginx/sbin]# ./nginx 
[root@nginx-100 /usr/local/nginx/sbin]# ps -ef|grep nginx
root       1898      1  0 17:07 ?        00:00:00 nginx: master process ./nginx
nobody     1899   1898  0 17:07 ?        00:00:00 nginx: worker process
root       1901   1451  0 17:07 pts/0    00:00:00 grep --color=auto nginx
[root@nginx-100 /usr/local/nginx/sbin]# ./nginx -s quit
[root@nginx-100 /usr/local/nginx/sbin]# ps -ef|grep nginx
root       1904   1451  0 17:08 pts/0    00:00:00 grep --color=auto nginx
# 演示 reopen
[root@nginx-100 /usr/local/nginx/sbin]# ps -ef|grep nginx
root       1906      1  0 17:09 ?        00:00:00 nginx: master process ./nginx
nobody     1911   1906  0 17:09 ?        00:00:00 nginx: worker process
root       1917   1451  0 17:15 pts/0    00:00:00 grep --color=auto nginx
[root@nginx-100 /usr/local/nginx/sbin]# ll /usr/local/nginx/logs/
total 8
-rw-r--r-- 1 nobody root   0 Mar 16 23:10 access.log
-rw-r--r-- 1 nobody root 741 Mar 18 17:09 error.log
-rw-r--r-- 1 root   root   5 Mar 18 17:09 nginx.pid
[root@nginx-100 /usr/local/nginx/sbin]# rm -rf /usr/local/nginx/logs/access.log /usr/local/nginx/logs/error.log 
[root@nginx-100 /usr/local/nginx/sbin]# ./nginx -s reopen
[root@nginx-100 /usr/local/nginx/sbin]# ll /usr/local/nginx/logs/
total 8
-rw-r--r-- 1 nobody root  0 Mar 18 17:16 access.log
-rw-r--r-- 1 nobody root 60 Mar 18 17:16 error.log
-rw-r--r-- 1 root   root  5 Mar 18 17:09 nginx.pid
[root@nginx-100 /usr/local/nginx/sbin]# ps -ef|grep nginx
root       1906      1  0 17:09 ?        00:00:00 nginx: master process ./nginx
nobody     1911   1906  0 17:09 ?        00:00:00 nginx: worker process
root       1948   1451  0 17:16 pts/0    00:00:00 grep --color=auto nginx
# 演示 reload 加载配置,配置重新生效(重新打开worker进程)
[root@nginx-100 /usr/local/nginx/sbin]# ./nginx 
[root@nginx-100 /usr/local/nginx/sbin]# ps -ef|grep nginx
root       1906      1  0 17:09 ?        00:00:00 nginx: master process ./nginx
nobody     1907   1906  0 17:09 ?        00:00:00 nginx: worker process
root       1909   1451  0 17:09 pts/0    00:00:00 grep --color=auto nginx
[root@nginx-100 /usr/local/nginx/sbin]# ./nginx -s reload
[root@nginx-100 /usr/local/nginx/sbin]# ps -ef|grep nginx
root       1906      1  0 17:09 ?        00:00:00 nginx: master process ./nginx
nobody     1911   1906  0 17:09 ?        00:00:00 nginx: worker process
root       1913   1451  0 17:09 pts/0    00:00:00 grep --color=auto nginx

18.6 nginx -p

  -p:prefix,指定Nginx的prefix路径 (默认为: /usr/local/nginx/)

 

18.7 nginx -c

  -c:filename,指定Nginx的配置文件路径 (默认为: /usr/local/nginx/conf/nginx.conf)

# 演示 -c
[root@nginx-100 /usr/local/nginx/sbin]# cp /usr/local/nginx/conf/nginx.conf /usr/local/nginx/abc.conf
# 默认找  /usr/local/nginx/conf/nginx.conf 配置文件
[root@nginx-100 /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
# 配合nginx.conf 的配置文件不全
[root@nginx-100 /usr/local/nginx/sbin]# ./nginx -tc /usr/local/nginx/abc.conf 
nginx: [emerg] open() "/usr/local/nginx/mime.types" failed (2: No such file or directory) in /usr/local/nginx/abc.conf:17
nginx: configuration file /usr/local/nginx/abc.conf test failed
# 拷贝文件后,可以指定检测配置文件
[root@nginx-100 /usr/local/nginx/sbin]# cp /usr/local/nginx/conf/mime.types /usr/local/nginx/
[root@nginx-100 /usr/local/nginx/sbin]# ./nginx -tc /usr/local/nginx/abc.conf 
nginx: the configuration file /usr/local/nginx/abc.conf syntax is ok
nginx: configuration file /usr/local/nginx/abc.conf test is successful
# 指定 nginx 启动的配置文件
[root@nginx-100 /usr/local/nginx/sbin]# ./nginx -c /usr/local/nginx/abc.conf 
[root@nginx-100 /usr/local/nginx/sbin]# ps -ef|grep nginx
root       2072      1  0 17:33 ?        00:00:00 nginx: master process ./nginx -c /usr/local/nginx/abc.conf
nobody     2073   2072  0 17:33 ?        00:00:00 nginx: worker process
root       2077   1451  0 17:34 pts/0    00:00:00 grep --color=auto nginx

 

18.8 nginx -g

  -g:用来补充Nginx配置文件,向Nginx服务指定启动时所应用的全局配置

# 演示 -g
[root@nginx-100 /usr/local/nginx/sbin]# grep 'pid' /usr/local/nginx/conf/nginx.conf
#pid        logs/nginx.pid;
# 修改全局变量,例如,修改启动的进程文件名
[root@nginx-100 /usr/local/nginx/sbin]# ./nginx -g "pid logs/abc.pid;"
[root@nginx-100 /usr/local/nginx/sbin]# ll /usr/local/nginx/logs/
total 8
-rw-r--r-- 1 root   root   5 Mar 18 17:41 abc.pid
-rw-r--r-- 1 nobody root   0 Mar 18 17:16 access.log
-rw-r--r-- 1 nobody root 642 Mar 18 17:40 error.log
# 停止时,没办法停止 默认是发送信号到 nginx.pid,需通过进程关闭
[root@nginx-100 /usr/local/nginx/sbin]# ./nginx -s stop
nginx: [error] open() "/usr/local/nginx/logs/nginx.pid" failed (2: No such file or directory)
[root@nginx-100 /usr/local/nginx/sbin]# ps -ef|grep nginx 
root       2103      1  0 17:41 ?        00:00:00 nginx: master process ./nginx -g pid logs/abc.pid;
nobody     2104   2103  0 17:41 ?        00:00:00 nginx: worker process
root       2121   1451  0 17:43 pts/0    00:00:00 grep --color=auto nginx
[root@nginx-100 /usr/local/nginx/sbin]# kill -TERM 2103
[root@nginx-100 /usr/local/nginx/sbin]# ps -ef|grep nginx 
root       2123   1451  0 17:44 pts/0    00:00:00 grep --color=auto nginx

 

———————————————————————————————————————————————————————————————————————————

                                                                                                                         无敌小马爱学习

posted on 2026-03-16 23:24  马俊南  阅读(3)  评论(0)    收藏  举报