跳转至

Nginx 指令集

nginx -? 和 nginx -h

说明: 打印命令行参数的帮助信息,只读操作,无风险。

示例:

nginx -h

nginx -c file

说明: 使用指定配置文件而不是默认文件启动Nginx。

示例:

nginx -c /app/nginx/custom_nginx.conf

nginx -e file

说明: 用于将错误日志输出到指定的文件中,而不是使用配置文件中定义的日志路径或默认的标准错误输出(通常是终端)。常见用途是调试、测试、临时记录日志,风险会覆盖配置文件中。

示例:

nginx -e /app/nginx/custom_error.log
nginx -t -c /usr/local/etc/nginx/nginx.conf -e /tmp/nginx_errors.log

风险:实例会覆盖配置文件中配置

nginx -g

说明: 在启动 Nginx 时指定全局配置指令。覆盖或设置一些全局的配置参数。用于临时更改配置或者在命令行上快速测试某些配置,不重载配置文件。

示例:

nginx -g "worker_processes 4; error_log /var/log/nginx/error.log warn;";

nginx -p prefix_path

Official instructions: 指定 Nginx 运行时的前缀路径(prefix path)。这个前缀路径主要用于确定 Nginx 查找其默认配置文件、日志文件以及其他相关文件(如 MIME 类型定义文件等)的基础目录。默认情况下,Nginx 的前缀路径通常是安装目录,但是通过 -p 参数,你可以临时改变这一基础目录来指向任何你希望的位置。

测试新配置而不影响现有服务

示例:

nginx -p /home/user/nginx_test/ -c /path/to/custom/nginx.conf

nginx -q

说明:

在配置测试期间抑制非错误消息。

示例:

nginx -q

# @since 1.28

nginx -s signal

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

  • stop: 快速关闭
  • quit: 优雅地关闭
  • reload:重新加载配置,用新配置启动新的工作进程,优雅地关闭旧的工作进程。

示例:

nginx -s reload
nginx -s quit
nginx -s stop

nginx -t

测试配置文件:nginx检查配置的语法是否正确,然后尝试打开配置中引用的文件。

示例:

nginx -t

# nginx: the configuration file /app/nginx/conf/nginx.conf syntax is ok
# nginx: [emerg] bind() to 0.0.0.0:80 failed (13: Permission denied)
# nginx: configuration file /app/nginx/conf/nginx.conf test failed

nginx -T

与-t相同,检查配置的语法是否正确,然后尝试打开配置中引用的文件,但额外将配置文件转储到标准输出。

示例:

#@since 1.9.2

nginx -T

# nginx: the configuration file /app/nginx/conf/nginx.conf syntax is ok
# nginx: [emerg] bind() to 0.0.0.0:80 failed (13: Permission denied)
# nginx: configuration file /app/nginx/conf/nginx.conf test failed

nginx -v

打印Nginx版本信息

示例:

nginx -v

# nginx version: nginx/1.26.3

nginx -V

打印nginx版本、编译器版本和配置参数。

示例:

nginx -V

# nginx version: nginx/1.26.3
# built by gcc 13.3.0 (Ubuntu 13.3.0-6ubuntu2~24.04) 
# built with OpenSSL 3.0.13 30 Jan 2024
# TLS SNI support enabled
# configure arguments: --prefix=/app/nginx --user=ubuntu --with-http_stub_status_module --with-http_v2_module --with-http_ssl_module --with-stream --with-stream_ssl_module --with-http_gzip_static_module --with-http_realip_module