Greenhats の Blog.

Nginx 常用命令

2025/02/01
loading

Nginx 常用命令

Nginx的常用命令与配置管理整理,按功能分类说明:


服务管理

  1. 启动Nginx

    1
    2
    3
    # 通用启动方式(根据系统)
    sudo systemctl start nginx # systemd系统(Ubuntu 16.04+/CentOS 7+)
    sudo service nginx start # SysVinit系统(旧版Ubuntu/CentOS)
  2. 停止Nginx

    1
    2
    sudo systemctl stop nginx     # 强制停止服务
    sudo service nginx stop # 旧版系统
  3. 重启/重载配置

    1
    2
    3
    sudo systemctl reload nginx   # 平滑重载(不中断连接,推荐)
    sudo systemctl restart nginx # 强制重启(中断连接)
    sudo nginx -s reload # 直接通过Nginx主进程重载
  4. 检查服务状态

    1
    2
    sudo systemctl status nginx   # 查看运行状态和日志
    sudo service nginx status # 旧版系统

配置验证与调试

  1. 检查配置文件语法

    1
    2
    sudo nginx -t                 # 测试配置文件语法
    sudo nginx -t -c /path/to/nginx.conf # 测试指定配置文件
  2. 查看Nginx版本与编译参数

    1
    2
    nginx -v                      # 显示版本
    nginx -V # 显示版本及编译模块信息

日志管理

  1. 查看日志文件

    1
    2
    3
    # 默认日志路径(根据安装方式可能不同)
    tail -f /var/log/nginx/access.log # 实时查看访问日志
    tail -f /var/log/nginx/error.log # 实时查看错误日志
  2. 自定义日志路径

    1
    2
    3
    # 在nginx.conf或站点配置中设置
    access_log /var/log/nginx/custom_access.log;
    error_log /var/log/nginx/custom_error.log;

进程管理

  1. 查看Nginx进程

    1
    ps aux | grep nginx           # 查看所有Nginx相关进程
  2. 强制终止Nginx

    1
    sudo pkill nginx              # 强制终止所有Nginx进程

虚拟主机(Server Block)配置

  1. 创建站点配置文件

    1
    2
    3
    # 通常配置在`/etc/nginx/sites-available/`,然后创建符号链接到`sites-enabled/`
    sudo nano /etc/nginx/sites-available/example.com.conf
    sudo ln -s /etc/nginx/sites-available/example.com.conf /etc/nginx/sites-enabled/
  2. 基础站点配置示例

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    server {
    listen 80;
    server_name example.com www.example.com;
    root /var/www/example.com/html;
    index index.html index.htm;

    location / {
    try_files $uri $uri/ =404;
    }

    # 反向代理到本地应用(如Node.js)
    location /api {
    proxy_pass http://localhost:3000;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    }
    }

SSL证书配置

  1. 使用Let’s Encrypt(Certbot)

    1
    2
    3
    # 安装Certbot并获取证书
    sudo apt install certbot python3-certbot-nginx # Ubuntu/Debian
    sudo certbot --nginx -d example.com -d www.example.com
  2. 手动配置SSL

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    server {
    listen 443 ssl;
    server_name example.com;
    ssl_certificate /etc/ssl/certs/example.com.crt;
    ssl_certificate_key /etc/ssl/private/example.com.key;

    # 其他SSL优化配置(如协议、加密套件)
    ssl_protocols TLSv1.2 TLSv1.3;
    ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256;
    }

静态文件服务优化

1
2
3
4
5
6
7
8
server {
location /static {
alias /var/www/static;
expires 30d; # 缓存30天
access_log off; # 关闭访问日志
add_header Cache-Control "public";
}
}

反向代理配置

1
2
3
4
5
6
7
8
# 将请求代理到后端服务(如Node.js、Python应用)
location / {
proxy_pass http://localhost:3000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}

常见问题排查

  1. 端口占用检查

    1
    sudo lsof -i :80              # 查看80端口占用情况
  2. 权限问题修复

    1
    2
    sudo chown -R www-data:www-data /var/www/  # 设置文件所有权(Ubuntu)
    sudo chmod -R 755 /var/www/ # 调整权限
  3. 配置错误回滚

    1
    2
    sudo nginx -t                # 先测试配置
    sudo systemctl reload nginx # 确认无误后重载

其他实用命令

  1. 查看已加载模块

    1
    nginx -V 2>&1 | grep --color=auto -o "with-http_[a-z_]*"
  2. 自定义配置文件路径启动

    1
    sudo nginx -c /path/to/custom-nginx.conf

通过以上命令和配置示例,您可以高效管理Nginx服务、优化Web性能,并快速解决常见问题。建议在修改配置前始终使用 nginx -t 验证语法。

CATALOG
  1. 1. Nginx 常用命令
    1. 1.0.1. 服务管理
    2. 1.0.2. 配置验证与调试
    3. 1.0.3. 日志管理
    4. 1.0.4. 进程管理
    5. 1.0.5. 虚拟主机(Server Block)配置
    6. 1.0.6. SSL证书配置
    7. 1.0.7. 静态文件服务优化
    8. 1.0.8. 反向代理配置
    9. 1.0.9. 常见问题排查
    10. 1.0.10. 其他实用命令