Nginx 发音为“engine x”,是一个免费、开源、高性能的 HTTP 和反向代理服务器,负责处理互联网上一些最大网站的负载。它可以用作独立的 Web 服务器,也可以用作反向代理适用于 Apache 和其他 Web 服务器。
如果您是开发人员或系统管理员,您很可能经常与 Nginx 打交道。
在本指南中,我们将介绍最重要和最常用的 Nginx 命令,包括启动、停止和重新启动 Nginx。
在你开始之前#
我们假设您以 root 或具有 sudo 权限的用户身份登录。 uide 这个 g 中的命令应该适用于任何现代 Linux 发行版,例如乌班图18.04
and CentOS 8
and Debian 10
.
启动 Nginx#
启动 Nginx 非常简单。只需运行以下命令:
sudo systemctl start nginx
成功后,该命令不会产生任何输出。
如果您运行的 Linux 发行版没有 systemd 来启动 Nginx,请输入:
sudo service nginx start
建议将其设置为在系统启动时启动,而不是手动启动 Nginx 服务:
sudo systemctl enable nginx
停止 Nginx#
即使存在打开的连接,停止 Nginx 也会快速关闭所有 Nginx 工作进程。
要停止 Nginx,请运行以下命令之一:
sudo systemctl stop nginx
sudo service nginx stop
重启 Nginx#
重新启动选项是停止然后启动 Nginx 服务器的快速方法。
使用以下命令之一执行Nginx 重启
:
sudo systemctl restart nginx
sudo service nginx restart
这是您可能最常使用的命令。
重新加载 Nginx#
每当您更改其配置时,您都需要重新加载或重新启动 Nginx。
reload 命令加载新配置,使用新配置启动新工作进程,并正常关闭旧工作进程。
要重新加载 Nginx,请使用以下命令之一:
sudo systemctl reload nginx
sudo service nginx reload
测试 Nginx 配置#
每当您更改 Nginx 服务器的配置文件时,最好在重新启动或重新加载服务之前测试配置。
使用以下命令测试 Nginx 配置是否存在任何语法或系统错误:
sudo nginx -t
输出如下所示:
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
如果有任何错误,该命令会打印详细消息。
查看 Nginx 状态#
要检查 Nginx 服务的状态,请使用以下命令:
sudo systemctl status nginx
输出将如下所示:
nginx.service - A high performance web server and a reverse proxy server
Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
Active: active (running) since Sun 2019-04-21 13:57:01 PDT; 5min ago
Docs: man:nginx(8)
Process: 4491 ExecStop=/sbin/start-stop-daemon --quiet --stop --retry QUIT/5 --pidfile /run/nginx.pid (code=exited, status=0/SUCCESS)
Process: 4502 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
Process: 4492 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
Main PID: 4504 (nginx)
Tasks: 3 (limit: 2319)
CGroup: /system.slice/nginx.service
|-4504 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
|-4516 nginx: worker process
`-4517 nginx: worker process
检查 Nginx 版本#
有时您可能需要知道 Nginx 的版本,以便调试问题或确定某个功能是否可用。
您可以通过运行以下命令来检查您的 Nginx 版本:
sudo nginx -v
nginx version: nginx/1.14.0 (Ubuntu)
The -V
选项显示 Nginx 版本以及配置选项。
sudo nginx -V
结论#
在本指南中,我们向您展示了一些最基本的 Nginx 命令。如果您想了解有关 Nginx 命令行选项的更多信息,请访问Nginx 文档
.
如果您有任何问题或反馈,请随时发表评论。