Featured image of post Nginx 常用命令大全 🐧

Nginx 常用命令大全 🐧

Nginx 常用命令大全 🐧 全面整理 Nginx Web 服务器的日常管理命令,涵盖安装、配置、调试和故

Nginx 常用命令大全 🐧

Nginx 架构

全面整理 Nginx Web 服务器的日常管理命令,涵盖安装、配置、调试和故障排除等操作


📖 目录导航


✨ Nginx 简介

Nginx (发音为"engine x") 是一款高性能的开源 Web 服务器和反向代理服务器,以其高并发处理能力、低内存占用和卓越的稳定性著称。它不仅可以用作 HTTP/HTTPS 服务器,还能作为负载均衡器、邮件代理服务器和通用 TCP/UDP 代理服务器使用。

主要特性

  • 🚀 高性能:采用事件驱动架构,能够处理数万个并发连接
  • 📦 低内存消耗:相比传统服务器,内存使用效率更高
  • 🔧 模块化设计:支持动态模块加载,灵活扩展功能
  • ⚖️ 负载均衡:提供多种负载均衡算法,支持健康检查
  • 🔒 安全性:支持 SSL/TLS 加密,各种安全头部设置
  • 🌐 反向代理:强大的反向代理功能,支持缓存和压缩

📋 一、版本与安装管理

1. 查看 Nginx 版本 🔍

1
nginx -v

2. 查看详细版本和编译信息 📦

1
nginx -V

3. 安装 Nginx (Ubuntu/Debian) ⬇️

1
2
sudo apt update
sudo apt install nginx -y

4. 安装 Nginx (CentOS/RHEL) ⬇️

1
2
sudo yum install epel-release -y
sudo yum install nginx -y

5. 升级 Nginx 版本 ⬆️

1
sudo apt upgrade nginx

6. 卸载 Nginx(保留配置文件)🗑️

1
sudo apt remove nginx nginx-common -y

7. 完全卸载 Nginx(删除配置文件)🔥

1
sudo apt remove --purge nginx nginx-common -y

8. 彻底清除 Nginx(删除所有相关文件)💥

1
sudo apt autoremove --purge nginx -y

🚀 二、服务控制命令

1. 启动 Nginx 服务 ▶️

1
sudo systemctl start nginx

2. 停止 Nginx 服务 ⏹️

1
sudo systemctl stop nginx

3. 重启 Nginx 服务 🔄

1
sudo systemctl restart nginx

4. 查看 Nginx 运行状态 📊

1
sudo systemctl status nginx

5. 设置开机自动启动 📈

1
sudo systemctl enable nginx

6. 禁用开机自动启动 📉

1
sudo systemctl disable nginx

7. 检查是否已启用开机启动 ✅

1
sudo systemctl is-enabled nginx

8. 重新加载 Bash 配置 🔧

1
source ~/.bashrc 

9. 查看所有运行的 Nginx 进程 👀

1
ps aux | grep nginx

⚙️ 三、配置测试与重载

1. 测试配置文件语法是否正确 🧪

1
nginx -t

2. 查看 Nginx 最终生效的配置 📝

1
nginx -T

3. 重新加载配置文件(不中断服务)🔄

1
nginx -s reload

4. 测试特定配置文件是否正确 🎯

1
nginx -t -c /etc/nginx/conf.d/xunlei.conf

5. 优雅停止 Nginx(处理完当前请求)🛑

1
nginx -s quit

6. 立即停止 Nginx ⚡

1
nginx -s stop

7. 重新打开日志文件 📂

1
nginx -s reopen

📊 四、日志与监控

1. 查看错误日志 📖

1
cat /var/log/nginx/error.log

2. 实时跟踪错误日志 🎥

1
sudo tail -f /var/log/nginx/error.log

3. 查看访问日志 📖

1
cat /var/log/nginx/access.log

4. 实时跟踪访问日志 🎥

1
sudo tail -f /var/log/nginx/access.log

5. 使用 journalctl 查看日志 🖥️

1
sudo journalctl -u nginx

6. 查看实时日志(带时间戳)⏰

1
sudo tail -f /var/log/nginx/access.log | awk '{print strftime("%Y-%m-%d %H:%M:%S"), $0}'

7. 查看 80 端口占用情况 🔍

1
netstat -tulnp | grep ":80"

8. 查看 443 端口占用情况 🔍

1
netstat -tulnp | grep ":443"

9. 监控 Nginx 连接数 📈

1
netstat -an | grep :80 | wc -l

🛠️ 五、高级调试命令

1. 查看 Nginx 进程信息 🔎

1
ps aux | grep nginx

2. 检查配置文件包含关系 🧩

1
nginx -T | grep include

3. 查看编译时模块信息 📦

1
nginx -V

4. 检查特定虚拟主机配置 🎯

1
nginx -T | grep -A 20 -B 5 "server_name example.com"

5. 查看当前连接数统计 📊

1
netstat -an | grep :80 | awk '{print $6}' | sort | uniq -c | sort -n

6. 检查 Nginx 配置中的监听端口 🎧

1
nginx -T | grep "listen"

7. 查看服务器名称配置 🌐

1
nginx -T | grep "server_name"

8. 调试特定 IP 的连接 🔍

1
tcpdump -i any -n host 192.168.1.100 and port 80

💡 实用技巧与提示

1. 配置检查流程 ✅

1
2
3
4
5
# 推荐的操作顺序
nginx -t                    # 先检查配置是否正确
sudo nginx -T > nginx.conf # 导出当前配置备份
sudo systemctl restart nginx  # 再重启服务
sudo systemctl status nginx   # 最后确认状态

2. 日志轮转 🔄

1
2
3
4
5
6
7
# 使用 logrotate 自动管理日志
sudo logrotate -f /etc/logrotate.d/nginx

# 手动切割日志
sudo mv /var/log/nginx/access.log /var/log/nginx/access.log.$(date +%Y%m%d)
sudo mv /var/log/nginx/error.log /var/log/nginx/error.log.$(date +%Y%m%d)
sudo nginx -s reopen

3. 性能监控 📈

1
2
3
4
5
6
7
8
# 监控 Nginx 连接数
watch -n 1 "netstat -an | grep :80 | wc -l"

# 查看前10个访问最多的IP
awk '{print $1}' /var/log/nginx/access.log | sort | uniq -c | sort -nr | head -10

# 查看最频繁访问的URL
awk '{print $7}' /var/log/nginx/access.log | sort | uniq -c | sort -nr | head -10

4. 安全检查 🔒

1
2
3
4
5
6
7
8
# 检查配置文件权限
find /etc/nginx -type f -exec ls -la {} \;

# 检查SSL配置
sudo nginx -T | grep ssl_

# 查看证书过期时间
echo | openssl s_client -connect example.com:443 2>/dev/null | openssl x509 -noout -dates

5. 备份配置 📂

1
2
3
4
5
# 备份当前配置
sudo tar -czf nginx-backup-$(date +%Y%m%d).tar.gz /etc/nginx

# 导出当前生效的配置
sudo nginx -T > nginx-config-$(date +%Y%m%d).conf

🚨 故障排除指南

常见问题及解决方案:

  1. 端口被占用

    1
    2
    3
    4
    
    # 查找占用80端口的进程
    sudo lsof -i :80
    # 或者
    sudo netstat -tulnp | grep ":80"
    
  2. 配置文件错误 🐛

    1
    2
    3
    4
    
    # 详细检查配置错误
    sudo nginx -t
    # 检查特定配置段
    sudo nginx -T | grep -A 10 -B 10 "error"
    
  3. 权限问题 🔐

    1
    2
    3
    4
    5
    
    # 检查Nginx用户权限
    sudo -u nginx stat /var/www/html
    # 修复文件权限
    sudo chown -R nginx:nginx /var/www/html
    sudo chmod -R 755 /var/www/html
    
  4. 模块缺失 📦

    1
    2
    3
    4
    
    # 查看已编译模块
    nginx -V 2>&1 | grep -- '--with-'
    # 查看动态加载的模块
    sudo nginx -T | grep "load_module"
    
  5. 性能问题 🐢

    1
    2
    3
    4
    
    # 检查工作进程数量
    ps aux | grep nginx | grep -v grep | wc -l
    # 监控服务器状态
    sudo nginx -T | grep -i "status"
    
  6. SSL证书问题 🔒

    1
    2
    3
    4
    
    # 检查证书链
    openssl s_client -connect example.com:443 -servername example.com
    # 验证证书
    openssl verify -CAfile /path/to/ca.crt /path/to/site.crt
    

💡 提示:修改配置后,务必使用 nginx -t 测试语法,然后再重载配置。对于生产环境,建议先在测试环境验证配置变更。

紧急恢复步骤 🆘

  1. 备份当前配置:sudo cp -r /etc/nginx /etc/nginx.backup
  2. 检查配置:sudo nginx -t
  3. 如果配置错误,恢复备份:sudo cp -r /etc/nginx.backup/* /etc/nginx/
  4. 重启服务:sudo systemctl restart nginx
  5. 查看日志:sudo tail -f /var/log/nginx/error.log

希望这份完整的 Nginx 命令指南能帮助您更好地管理您的 Web 服务器!如有其他问题,请参考 Nginx 官方文档 或社区资源。