每个 Linux 用户都应该知道的 10 个最常用的 Nginx 命令

yanding 2023-07-26 270

Nginx(发音为Engine x)是一个免费、开放、高性能、可扩展、可靠、全面且流行的HTTP和反向代理服务器、邮件代理服务器和通用TCP/UDP代理服务器。

Nginx 以其易于配置和高性能而降低资源消耗而闻名,被用来为网络上的许多高流量网站提供支持,例如 GitHub、SoundCloud、Dropbox、Netflix、WordPress 等。

在本指南中,我们将解释一些最常用的 Nginx 服务管理命令,作为服务器或系统管理员,您应该保留。我们将展示 Systemd 和 SysVinit 的命令。

整个流行 Nginx 命令列表必须以 root 或 sudo 用户身份运行,并且必须在任何现代 Linux 发行版上运行,例如 CentOS、RHEL、Debian、Ubuntu 和 Fedora。

安装 Nginx 服务器

要安装 Nginx Web 服务器,请使用默认的分发角色管理器,如图所示。

$ sudo yum install epel-release && yum install nginx [在 CentOS/RHEL 上]$ sudo dnf install nginx [在 Fedora 上]$ sudo apt install nginx [在 Debian/Ubuntu 上]

检查Nginx的版本

要检查 Linux 系统上安装的 Nginx Web 服务器的版本,请运行以下命令。

$ nginx -vnginx 版本:nginx/1.12.2

上面的命令只显示版本号。如果您想查看更新的版本和选项,请使用-V如图所示的标志。

$ nginx -V
nginx 版本:nginx/1.12.2由 gcc 4.8.5 20150623 (Red Hat 4.8.5-16) (GCC) 构建使用 OpenSSL 1.0.2k-fips 构建 2017 年 1 月 26 日启用 TLS SNI 支持配置参数: --prefix=/usr/share/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/ nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --http-client-body-temp-path =/var/lib/nginx/tmp/client_body --http-proxy-temp-path=/var/lib/nginx/tmp/proxy --http-fastcgi-temp-path=/var/lib/nginx/tmp/ fastcgi --http-uwsgi-temp-path=/var/lib/nginx/tmp/uwsgi --http-scgi-temp-path=/var/lib/nginx/tmp/scgi --pid-path=/run/ nginx。pid --lock-path=/run/lock/subsys/nginx --user=nginx --group=nginx --with-file-aio --with-ipv6 --with-http_auth_request_module --with-http_ssl_module --with -http_v2_module --with-http_realip_module --with-http_addition_module --with-http_xslt_module=dynamic --with-http_image_filter_module=dynamic --with-http_geoip_module=dynamic --with-http_sub_module --with-http_dav_module --with-http_flv_module -- with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_degradation_module --with-http_slice_module --with-http_stub_status_module --with-http_perl_module=dynamic --with-mail =动态 --with-mail_ssl_module --with-pcre --with-pcre-jit --with-stream=dynamic --with-stream_ssl_module --with-google_perftools_module --with-debug --with-cc-opt=' -O2 -g -管道-Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -specs=/usr/lib/rpm/redhat/redhat-hardened- cc1 -m64 -mtune=generic' --with-ld-opt='-Wl,-z,relro -specs=/usr/lib/rpm/redhat/redhat-hardened-ld -Wl,-E'

请参阅 Nginx 配置字典

在实际启动Nginx服务之前,您可以检查其配置语法是否正确。当您对现有配置层次结构进行更改或添加新策略时,这尤其有用。

要测试 Nginx 配置,请运行以下命令。

$ sudo nginx -tnginx:配置文件/etc/nginx/nginx.conf语法没问题nginx:配置文件/etc/nginx/nginx.conf测试成功

您可以测试 Nginx 配置,转储它并-T使用如图所示的标志退出。

$ sudo nginx -T
nginx:配置文件/etc/nginx/nginx.conf语法没问题nginx:配置文件/etc/nginx/nginx.conf测试成功# 配置文件/etc/nginx/nginx.conf:# 有关配置的更多信息,请参阅:# * 官方英文文档:http://nginx.org/en/docs/# * 俄语官方文档:http://nginx.org/ru/docs/用户 nginx;自动worker_processes;error_log /var/log/nginx/error.log;pid /run/nginx.pid;# 加载动态模块。请参阅/usr/share/nginx/README.dynamic。包含/usr/share/nginx/modules/*.conf;事件{
    工人连接1024;}http{
    log_format main '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent“$http_referer”'
                      '“$http_user_agent”“$http_x_forwarded_for”';
    access_log /var/log/nginx/access.log main;
    发送文件;
    tcp_nopush 开启;
    tcp_nodelay 开启;
    keepalive_timeout 65;
    types_hash_max_size 2048;
    包括/etc/nginx/mime.types;
    default_type 应用程序/八位字节流;
    # 从 /etc/nginx/conf.d 目录加载模块化配置文件。
    # 参见http://nginx.org/en/docs/ngx_core_module.html#include
    # 了解更多信息。
    包括/etc/nginx/conf.d/*.conf;
    服务器 {
        监听 80 默认服务器;
        监听[::]:80 default_server;
        服务器名称 _;
        根/usr/share/nginx/html;
        # 加载默认服务器块的配置文件。
        包括/etc/nginx/default.d/*.conf;
        地点 / {
        }
        error_page 404 /404.html;
            位置= /40x.html {
        }
        error_page 500 502 503 504 /50x.html;
            位置= /50x.html {
        }
    }....

启动 Nginx 服务

要启动 Nginx 服务,请运行以下命令。请注意,如果配置语法错误,此操作可能会失败。

$ sudo systemctl 启动 nginx #systemd或者$ sudo 服务 nginx 启动 #sysvinit

启用 Nginx 服务

前面的命令只在当时启动该服务,要使其在启动时自动启动,请执行以下命令。

$ sudo systemctl 启用 nginx #systemd或者$ sudo 服务 nginx 启用 #sysv init

重启 Nginx 服务

重新启动 Nginx 服务,该进程将停止然后启动服务。

$ sudo systemctl 重新启动 nginx #systemd或者$ sudo 服务 nginx restart #sysv init

查看Nginx服务环境

您可以按如下方式检查Nginx服务的状态。此命令显示有关服务的工作会话状态信息。

$ sudo systemctl 状态 nginx #systemd或者$ sudo 服务 nginx 状态#sysvinit
创建从 /etc/systemd/system/multi-user.target.wants/nginx.service 到 /usr/lib/systemd/system/nginx.service 的符号链接。
 nginx 的 systemctl 状态● nginx.service - nginx HTTP 和反向代理服务器
   已加载:已加载(/usr/lib/systemd/system/nginx.service;启用;供应商预设:禁用)
   活跃:自 2019-03-05 星期二 05:27:15 EST 起活跃(运行);2分59秒前
 主PID:31515(nginx)
   CGroup:/system.slice/nginx.service
           ├─31515 nginx:主进程/usr/sbin/nginx
           └─31516 nginx:工作进程三月 05 05:27:15 linux-console.net systemd[1]:启动 nginx HTTP 和反向代理服务器...Mar 05 05:27:15 linux-console.net nginx[31509]: nginx: 配置文件 /etc/nginx/nginx.conf 语法没问题Mar 05 05:27:15 linux-console.net nginx[31509]: nginx: 配置文件 /etc/nginx/nginx.conf 测试成功三月 05 05:27:15 linux-console.net systemd[1]:无法从文件 /run/nginx.pid 读取 PID:参数无效Mar 05 05:27:15 linux-console.net systemd[1]:启动了 nginx HTTP 和反向代理服务器。

重新加载 Nginx 服务

要告诉 Nginx 重新加载其配置,请使用以下命令。

$ sudo systemctl 重新加载 nginx #systemd或者$ sudo 服务 nginx 重新加载 #sysvinit

停止 Nginx 服务

如果您出于某种原因想要停止 Nginx 服务,请使用以下命令。

$ sudo systemctl 停止 nginx #systemd或者$ sudo 服务 nginx 停止#sysvinit

显示 Nginx 命令提示符

要查看 Nginx 命令和选项的简单指南,请使用以下命令。

$ systemctl -h nginx
systemctl [选项...] {命令} ...查询或发送控制命令到 systemd 管理器。
  -h --help 显示此帮助
     --version 显示软件包版本
     --system 连接到系统管理器
  -H --host=[[电子邮件]主机
                      在远程主机上操作
  -M --machine=容器
                      在本地容器上操作
  -t --type=TYPE 列出特定类型的单位
     --state=STATE 列出具有特定 LOAD 或 SUB 或 ACTIVE 状态的单元
  -p --property=NAME 仅显示该名称的属性
  -a --all 显示所有加载的单元/属性,包括死/空
                      那些。要列出系统上安装的所有单元,请使用
                      而是使用“list-unit-files”命令。
  -l --full 不要省略输出中的单元名称
  -r --recursive 显示主机和本地容器的单元列表
     --reverse 使用“list-dependencies”显示反向依赖关系
     --job-mode=MODE 指定何时处理已排队的作业
                      排队新工作
     --show-types 显示套接字时,显式显示其类型
  -i --忽略抑制剂...

您还可以阅读以下有关 Nginx 的文章。


  1. 提高 Nginx Web 服务安全性、稳健性和性能的终极指南

  2. Amplify – 简化 NGINX 监控

  3. ngxtop – 在 Linux 中实时监控 Nginx 日志文件

  4. 如何使用 SSL 证书安装带有虚拟主机的 Nginx

  5. 如何在 Linux 中隐藏 Nginx 服务器版本


目前为止就这样了!在本指南中,我们解释了您应该了解的一些最常用的 Nginx 服务管理命令,包括启动、运行、重新启动和停止 Nginx。如果您有任何意见或疑问,请使用下面的评论表。

言鼎科技

The End