nginx 命令参数

前面章节介绍了怎样安装 nginx,nginx 版本信息等。本章节将介绍怎样使用 nginx.exe 程序提供的命令参数去控制 nginx 的行为。

查看 nginx 帮助信息

在命令行执行 nginx -h 或者 nginx -? 命令查看 nginx 命令帮助信息,如下:

D:server
ginx-1.20.1>nginx -h
nginx version: nginx/1.20.1
Usage: nginx [-?hvVtTq] [-s signal] [-p prefix]
             [-e filename] [-c filename] [-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: NONE)
  -e filename   : set error log file (default: logs/error.log)
  -c filename   : set configuration file (default: conf/nginx.conf)
  -g directives : set global directives out of configuration file

下面将逐一介绍上面帮助信息的各个参数的用法。

-?,-h

显示 nginx.exe 使用帮助信息。

-v

显示版本信息,然后退出 nginx。例如:

D:server
ginx-1.20.1>nginx -v
nginx version: nginx/1.20.1

-V

显示 nginx 版本和配置项信息,然后退出 nginx。例如:

D:server
ginx-1.20.1>nginx -V
nginx version: nginx/1.20.1
built by cl 16.00.40219.01 for 80x86
built with OpenSSL 1.1.1k  25 Mar 2021
TLS SNI support enabled
configure arguments: --with-cc=cl --builddir=objs.msvc8 --with-debug --prefix= --conf-path=conf/nginx.conf --pid-path=logs/nginx.pid --http-log-path=logs/access.log --error-log-path=logs/error.log --sbin-path=nginx.exe --http-client-body-temp-path=temp/client_body_temp --http-proxy-temp-path=temp/proxy_temp --http-fastcgi-temp-path=temp/fastcgi_temp --http-scgi-temp-path=temp/scgi_temp --http-uwsgi-temp-path=temp/uwsgi_temp --with-cc-opt=-DFD_SETSIZE=1024 --with-pcre=objs.msvc8/lib/pcre-8.44 --with-zlib=objs.msvc8/lib/zlib-1.2.11 --with-http_v2_module --with-http_realip_module --with-http_addition_module --with-http_sub_module --with-http_dav_module --with-http_stub_status_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_auth_request_module --with-http_random_index_module --with-http_secure_link_module --with-http_slice_module --with-mail --with-stream --with-openssl=objs.msvc8/lib/openssl-1.1.1k --with-openssl-opt='no-asm no-tests -D_WIN32_WINNT=0x0501' --with-http_ssl_module --with-mail_ssl_module --with-stream_ssl_module

-t

用来测试 nginx 的配置文件 nginx.cfg 语法是否存在问题。例如:

D:server
ginx-1.20.1>nginx -t
nginx: the configuration file D:server
ginx-1.20.1/conf/nginx.conf syntax is ok
nginx: [emerg] bind() to 0.0.0.0:80 failed (10013: An attempt was made to access a socket in a way forbidden by its access permissions)
nginx: configuration file D:server
ginx-1.20.1/conf/nginx.conf test failed

上面结果显示,nginx 配置存在问题,不能正确监听 80 端口,因为该端口已经被监听了。打开 nginx.conf 配置文件,将 80 端口改为 8080 端口,再次执行 nginx -t,如下:

D:server
ginx-1.20.1>nginx -t
nginx: the configuration file D:server
ginx-1.20.1/conf/nginx.conf syntax is ok
nginx: configuration file D:server
ginx-1.20.1/conf/nginx.conf test is successful

-T

用来测试 nginx 的配置文件 nginx.cfg 语法是否存在问题,并且转存配置信息。例如:

D:server
ginx-1.20.1>nginx -T
nginx: the configuration file D:server
ginx-1.20.1/conf/nginx.conf syntax is ok
nginx: configuration file D:server
ginx-1.20.1/conf/nginx.conf test is successful
# configuration file D:server
ginx-1.20.1/conf/nginx.conf:

#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}
...

-q

在配置测试期间抑制非错误消息,例如;

D:server
ginx-1.20.1>nginx -q

注意:执行 nginx -q 命令后,命令行将处于等待状态;当有错误信息时,输出错误信息

-s signal

向 nginx 主进程发送信号。参数信号可以是以下之一:

  • stop — 快速关闭,不管当前 nginx 是否正在处理请求,直接关闭。

  • quit — 优雅地关闭,不直接关闭,停止接收新的请求,等待所有旧的请求完成后,才关闭。

  • reload — 重新加载配置,使用新配置启动新的工作进程,优雅地(依然继续处理旧的请求,新的请求使用新配置启动的工作进程)关闭旧的工作进程。

  • reopen — 重新打开日志文件。

例如:

# 重新加载 nginx,如果你修改了配置文件,则可以使用该命令
D:server
ginx-1.20.1>nginx -s reload
# 停止当前 nginx 服务
D:server
ginx-1.20.1>nginx -s stop

-p prefix

设置路径前缀(默认值:NONE),例如:将 nginx 主目录下面除了 nginx.exe 以外的其他目录和文件放到 %nginx_home%/hxstrive 目录下面。如下图:

使用 -p 参数指定前缀运行 nginx。命令如下:

D:server
ginx-1.20.1>nginx -p hxstrive

nginx 启动正常,访问正常。

-e filename

指定 nginx 的错误日志文件,默认为 logs/error.log。例如:

D:server
ginx-1.20.1>nginx -e hxstrive/logs/error.log

-c filename

指定 nginx 的配置文件,默认为:conf/nginx.conf。例如:

D:server
ginx-1.20.1>nginx -c hxstrive/conf/nginx.conf

-g directives

为 nginx 设置全局配置指令,例如:

nginx -g "pid /var/run/nginx.pid; worker_processes `sysctl -n hw.ncpu`;"

更多全局配置指令请参考“Nginx 核心功能”。

说说我的看法
全部评论(
没有评论
关于
本网站属于个人的非赢利性网站,转载的文章遵循原作者的版权声明,如果原文没有版权声明,请来信告知:hxstrive@outlook.com
公众号