7.8.9 常见部署问题与排错流程

在实际部署中,Nginx相关问题建议按“现象定位→配置核验→后端验证→日志与系统排查→回滚验证”的流程处理。以下给出标准化流程、命令、示例与练习。

原理流程草图(请求与排错路径):

文章图片

一、配置核验与生效检查(示例+命令)

# 1) 语法校验(预期:syntax is ok / test is successful)
sudo nginx -t

# 2) 查看完整生效配置(合并 include 后)
sudo nginx -T | less

# 3) 重载与重启(区别:reload无连接中断;restart会断开连接)
sudo systemctl reload nginx
sudo systemctl restart nginx

# 4) 检查端口占用(预期:80/443由nginx监听)
sudo ss -lntp | grep -E '80|443'

常见问题示例:server_name冲突导致命中默认虚拟主机

# /etc/nginx/conf.d/app.conf
server {
    listen 80;
    server_name app.example.com;
    location / {
        proxy_pass http://app_backend;
    }
}

# /etc/nginx/conf.d/default.conf
server {
    listen 80 default_server;
    server_name _;
    return 444;
}

排查要点:
- curl -I http://app.example.com 返回 444 → 命中 default_server
- 解决:确保 DNS 正确、server_name 准确且无重复;移除不必要的 default_server

二、后端验证与反向代理问题(示例+命令)

# 后端连通性
curl -I http://10.0.0.21:8080/health
nc -vz 10.0.0.21 8080

典型 upstream + 反代示例(含必要 Header):

# /etc/nginx/conf.d/app.conf
upstream app_backend {
    server 10.0.0.21:8080 max_fails=3 fail_timeout=10s;
    server 10.0.0.22:8080 max_fails=3 fail_timeout=10s;
    keepalive 32;
}

server {
    listen 80;
    server_name app.example.com;

    location / {
        proxy_pass http://app_backend;
        proxy_http_version 1.1;
        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;

        proxy_connect_timeout 3s;
        proxy_read_timeout 60s;
        proxy_send_timeout 60s;
    }
}

错误码快速定位:
- 502:后端崩溃/协议不匹配/连接被重置
- 504:后端响应慢或超时不足
- 403:权限/SELinux/访问控制/鉴权

排错命令:

# 后端进程与端口
ps -ef | grep -E 'app|java|gunicorn'
ss -lntp | grep 8080

# 后端日志(示例路径)
tail -f /var/log/app/app.log

三、静态资源与路径问题(示例+命令)
常见 404 原因:root/alias 混用、路径尾斜杠不一致

server {
    listen 80;
    server_name static.example.com;

    # 正确:root 会把 URI 追加到目录
    location /static/ {
        root /data/www;
        # 实际路径:/data/www/static/xxx
    }

    # 正确:alias 不追加 location 前缀
    location /assets/ {
        alias /data/assets/;
        # 实际路径:/data/assets/xxx
    }
}

验证:

curl -I http://static.example.com/static/logo.png
curl -I http://static.example.com/assets/logo.png

上传/下载异常排查:

client_max_body_size 50m;
proxy_request_buffering off;
df -h /var/lib/nginx /tmp

四、日志与系统层面联动(示例+命令)
访问日志定位慢请求与后端耗时:

log_format main '$remote_addr $status $request_time '
                '$upstream_response_time $request';
access_log /var/log/nginx/access.log main;
# 慢请求筛选(>1s)
awk '$3>1 {print}' /var/log/nginx/access.log | head

错误日志调试:

error_log /var/log/nginx/error.log info;
tail -f /var/log/nginx/error.log

系统资源检查:

top -p $(pgrep -d',' nginx)
iostat -x 1 5
vmstat 1 5
dmesg | tail

五、发布与回滚验证(示例+命令)
发布前配置一致性校验与对比:

# 比较配置差异
diff -u /etc/nginx/conf.d/app.conf.bak /etc/nginx/conf.d/app.conf

# 仅验证新配置语法
nginx -t -c /etc/nginx/nginx.conf

灰度与回滚切换示例:

# /etc/nginx/conf.d/app.conf
upstream app_backend {
    server 10.0.0.21:8080 weight=9;
    server 10.0.0.22:8080 weight=1; # 灰度
}
# 灰度验证
curl -H "Host: app.example.com" http://127.0.0.1/health
# 回滚:切回备份配置并reload
cp /etc/nginx/conf.d/app.conf.bak /etc/nginx/conf.d/app.conf
nginx -t && systemctl reload nginx

六、常见问题清单与命令速查(示例)

# 端口占用
ss -lntp | grep :80

# 权限不足
namei -l /data/www/static/logo.png

# SELinux(如启用)
getenforce
sudo setenforce 0  # 临时关闭用于验证

练习
1) 构造一个 502:停止后端服务,观察 Nginx 返回码并从 error.log 定位。
2) 构造一个 404:将 location 中 root 改为 alias,验证路径错误与修复。
3) 构造一个 504:在后端增加 sleep 120,设置 proxy_read_timeout 30s,验证超时并调整。
4) 开启访问日志格式,统计 request_time>1s 的请求并找出对应 upstream。