跳转至

Nginx 命令集

nginx 可执行文件命令行参数速查。详见 官方 switches 文档

nginx -? / -h

说明:

打印命令行参数帮助信息。

示例:

nginx -h
nginx -?

nginx -c file

说明:

使用指定的配置文件,替代默认配置文件。

示例:

nginx -c /app/nginx/custom_nginx.conf
nginx -t -c /usr/local/etc/nginx/nginx.conf

nginx -e file

说明:

指定错误日志文件路径,替代默认路径(1.19.5+)。特殊值 stderr 表示标准错误输出。

示例:

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

补充说明:

将错误日志写到命令行指定的路径,会覆盖配置文件中的 error_log(或未配置时的默认 stderr)。常用于调试、临时测试。注意:对该进程而言,会覆盖配置文件里的 error_log 设置。

nginx -g directives

说明:

在命令行设置 main 上下文 全局指令;多条指令用分号分隔。

示例:

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

补充说明:

启动时临时覆盖全局参数,无需改配置文件。适合快速试验;生产环境变更仍应改配置文件并执行 nginx -t 后再 nginx -s reload

nginx -p prefix

说明:

设置 nginx 前缀路径(prefix)——查找默认配置、日志等文件的根目录(常见默认值为安装目录,如 /usr/local/nginx)。

示例:

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

补充说明:

改变相对路径的解析基准(如默认 conf/nginx.conflogs/)。多实例部署或在新目录试跑时常用。

Note

配置文件不在默认 prefix 下时,请与 -c 一起使用。启动或 reload 前务必先 nginx -t

nginx -q

说明:

配置测试时抑制非错误输出(1.28+)。

示例:

nginx -t -q

补充说明:

配合 -t / -T 使用时仅打印错误,便于脚本与 CI 静默判断成功与否。

nginx -s signal

说明:

向 master 进程发送信号。signal 取值为:

  • stop — 快速停止
  • quit — 优雅退出(等待已有请求处理完)
  • reload — 重载配置;以新配置启动 worker,并优雅关闭旧 worker
  • reopen — 重新打开日志文件(日志轮转后常用)

示例:

nginx -s reload
nginx -s quit
nginx -s reopen

补充说明:

需要 nginx 已在运行,且具备相应权限(常为 root 或与启动用户相同)。改完配置后请先 nginx -t,再执行 nginx -s reload

nginx -t

说明:

测试配置文件:检查语法,并尝试打开配置中引用的文件。

示例:

nginx -t

# nginx: the configuration file /app/nginx/conf/nginx.conf syntax is ok
# nginx: configuration file /app/nginx/conf/nginx.conf test is successful
# 失败示例(如无权限绑定 80 端口):
# 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 相同,并额外将合并后的完整配置输出到标准输出(1.9.2+)。

示例:

nginx -T

补充说明:

用于核对 include 展开结果及实际生效配置;复杂部署下输出可能很长。

nginx -v

说明:

打印 nginx 版本号。

示例:

nginx -v
# nginx version: nginx/1.26.3

nginx -V

说明:

打印 nginx 版本、编译器版本及 ./configure 参数。

示例:

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 --with-http_v2_module ...

补充说明:

排查环境差异或确认编译模块(如 --with-stream--with-http_v3_module)时使用;提工单前建议先收集 -V 输出。