NGINX 管理命令

内容纲要

查看「NGINX 专题」获取更多相关内容


使用 nginx 命令管理

解决 nginx: command not found

通过源代码编译安装的 NGINX 你可能无法直接使用 nginx 而是需要带上命令,否则会遇到提示:

nginx
nginx: command not found

在未配置 --prefix 时 NGINX 会默认安装到 /usr/local/nginx,所以可以带上完整路径来运行命令,如:

/usr/local/nginx/sbin/nginx -v

如果你接手的系统并不知道装在了哪里可以使用命令如在 /usr 目录下下:find /usr -type f -name "nginx"

在找到 NGINX 的二进制文件后,可以做个链接到 /usr/bin/,如:

sudo ln -sf /usr/local/nginx/sbin/nginx /usr/bin/nginx

然后就可以直接使用 nginx 命令了:

nginx -h
nginx version: nginx/1.22.0
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: /usr/local/nginx/)
  -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

版本号和编译信息

# 显示版本号
nginx -v
# 显示版本号和具体的编译信息
nginx -V

管理服务

使用 ps 命令查看 NGINX 进程信息:

ps -eLf | grep nginx
root       41307       1   41307  0    1 18:31 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
www        41313   41307   41313  0    1 18:31 ?        00:00:00 nginx: worker process

NGINX 有 master 和 worker 两种进程。master 读取并验证配置文件和管理 worker 进程,每个 worker 进程维护一个线程(避免线程切换),处理连接和请求。

NGINX 使用基于事件的模型和依赖于操作系统的机制在工作进程之间高效地分发请求。工作进程的数量在配置文件中定义,可以针对给定的配置进行固定,也可以自动调整为可用 CPU 核心的数量。

例如,要停止 NGINX 进程并等待 worker 进程完成服务当前请求时可以使用 nginx -s quit

# 使用 -s 选项向主进程发送信号
# nginx -s <signal>

# 立即停止
nginx -s stop

# 优雅地退出
nginx -s quit

# 重新加载配置,用新配置启动新的辅助进程,优雅地退出旧的 worker 进程
nginx -s reload

# 重新打开日志文件
nginx -s reopen

❓ 何为「优雅地退出」
NGINX 的进程空间由一个 master 进程和多个 worker 进程组成,master 进程读取和评估配置,worker 进程负责处理实际的请求,在执行 nginx -s quit 命令后,master 进程不会立即退出,而是等待 worker 进程处理完当前的连接后再行退出。

nginx -s quit 类似,在修改配置文件并使用 nginx -s reload 命令后,master 进程收到了重载配置的信号,它就会检查新配置文件的语法有效性,并尝试应用其中提供的配置。如果新的配置顺利成功,master 进程启动新的 worker 进程,并向旧的 worker 进程发送消息,要求它们关闭。否则,master 进程将回滚更改,并继续使用旧配置工作。旧 worker 进程在收到关闭命令后,会停止接受新的连接,并继续为当前的请求提供服务,直到所有这些请求都得到服务之后,旧 worker 进程退出。

另外,也可以借助如 kill 之类的工具将信号发送到 NGINX 进程。在这种情况下,会直接向具有给定进程 ID 的进程发送信号。默认情况下,NGINX 主进程的进程 ID 会写入 /usr/local/nginx/logs/var/run 目录下的 nginx.pid

例如假设主进程 ID 为 53495,发送 QUIT 信号让 NGINX 优雅地关闭:

sudo kill -s QUIT 53495

指定配置文件

可以使用 -c 选项指定配置文件,如

nginx -c /etc/nginx/nginx.conf

测试配置文件

$ nginx -t
# 或
$ nginx -T

指定配置指令

nginx -g 'daemon off;'

使用 systemd 管理

配置 NGINX 的 systemd 配置

sudo vim /lib/systemd/system/nginx.service

文件内容为:

[Unit]
Description=The NGINX HTTP and reverse proxy server
After=syslog.target network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target

[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true

[Install]
WantedBy=multi-user.target

使用 systemd 管理 NGINX:

# 查看状态
systemctl status nginx

# 设置 NGINX 开机启动
sudo systemctl enable nginx

# 关闭 NGINX 开机启动
sudo systemctl disable nginx

# 开启 NGINX
sudo systemctl start nginx

# 关闭 NGINX
sudo systemctl stop nginx

# 重载 NGINX 配置
sudo systemctl reload nginx