【前情提要】 日报脚本写好了,cron 也能每天自动跑了。老周说:“你现在能‘算’日志了,但真正的故障排查,不是算出来的,是‘查’出来的。”第二天,博客突然访问不了,监控没告警,日志也没明显错误。小林第一次面对“全链路排错”。

一、周一早上的“安静”

周一早上,小林刚到工位,手机响了。

同事发来消息:“你博客打不开了。”

小林打开 Mac 浏览器,访问 http://localhost:8080。

转圈。然后:

无法访问此网站

“啥?”小林刷新了几次。还是一样。

他打开 Prometheus,查 up:

up{job="node"} 1
up{job="prometheus"} 1

“up 都是 1,监控没告警?”小林说,“但博客打不开?”

他检查 Grafana,CPU、内存、磁盘都正常。

“监控说没事,但博客挂了。”小林说,“这什么情况?”

老周端着保温杯走过来,看了一眼:“监控没告警,不代表服务没问题。 你监控了 CPU、内存、磁盘,但你监控 Nginx 服务本身了吗?”

小林愣住了。

“你 up{job="node"} 是 1,说明 node_exporter 活着。但 node_exporter 活着,不代表 Nginx 活着。”老周说,“你监控的是‘机器’,不是‘服务’。”

“那怎么办?”

“从头查。”老周说,“这就是全链路排错。从浏览器到服务器,一层一层往下查。”

二、第一步:从 Mac 到 Ubuntu,网络通不通?

“先确认最基本的。”老周说,“你 Mac 能 ping 通 Ubuntu 吗?”

小林在 Mac 终端敲:

ping -c 3 192.168.1.x

输出:

3 packets transmitted, 3 packets received, 0.0% packet loss

“通了。”小林说。

“那 SSH 能连吗?”

ssh devops@192.168.1.x

“能连。”

“好,网络没问题,SSH 没问题。”老周说,“问题在 Ubuntu 内部。”

小林记下:

  • ping:确认网络通
  • ssh:确认远程登录通
  • 如果 ping 不通,检查 VMware 网络、防火墙
  • 如果 ping 通但 ssh 不通,检查 sshd 服务、22 端口

三、第二步:Nginx 服务在跑吗?

“先看 Nginx。”老周说。

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 Mon 2026-09-14 08:00:00 CST; 2h ago

“active (running),在跑。”小林说。

“服务在跑,不代表端口在监听。”老周说,“看端口。”

sudo ss -lntp | grep :80

输出为空。

“80 端口没监听?”小林懵了。

“对。”老周说,“服务在跑,但没监听 80 端口。这就是‘服务活着但没用’的状态。”

“那怎么查?”

“看 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 错误日志。”

sudo tail -30 /var/log/nginx/error.log

输出:

2026/09/14 08:00:01 [emerg] 1234#1234: bind() to 0.0.0.0:80 failed (98: Address already in use)
2026/09/14 08:00:01 [emerg] 1234#1234: bind() to 0.0.0.0:80 failed (98: Address already in use)
2026/09/14 08:00:01 [emerg] 1234#1234: bind() to 0.0.0.0:80 failed (98: Address already in use)
2026/09/14 08:00:01 [emerg] 1234#1234: bind() to 0.0.0.0:80 failed (98: Address already in use)
2026/09/14 08:00:01 [emerg] 1234#1234: bind() to 0.0.0.0:80 failed (98: Address already in use)
2026/09/14 08:00:01 [emerg] 1234#1234: bind() to 0.0.0.0:80 failed (98: Address already in use)

“Address already in use。”小林说,“80 端口被占用了。”

“对。”老周说,“Nginx 启动时想绑定 80,但被别的进程占了。所以 Nginx 虽然 active,但实际没监听 80。”

“那谁占了 80?”

“ss 查。”

sudo ss -lntp | grep :80

输出为空。

“没进程占 80 啊。”小林说。

“那就怪了。”老周说,“error.log 说被占用,但 ss 看不到。你试试 lsof。”

sudo lsof -i :80

输出:

COMMAND   PID     USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
python3  12345   devops    3u  IPv4  12345      0t0  TCP *:80 (LISTEN)

“python3 占了 80。”小林说,“什么 python3?”

“你看 PID 12345,查它。”

ps -p 12345 -o pid,ppid,user,command

输出:

  PID  PPID USER   COMMAND
12345     1 devops python3 -m http.server 80

“python3 -m http.server 80。”小林说,“这是我上周测试用的,忘了关!”

“对。”老周说,“你上周测试 Python HTTP 服务器,用了 80 端口,忘了关。今天早上 logrotate 触发了 systemctl reload nginx,Nginx 重新加载配置,发现 80 被占,启动失败。但 systemctl status 显示 active,因为旧进程还在跑。”

“那为什么 ss 看不到?”

“ss 默认不显示非 root 用户的所有进程。”老周说,“你 sudo ss -lntp 应该能看到,但有时候 ss 的权限不够。lsof 更可靠。”

小林记下:

  • sudo ss -lntp | grep :80:看 80 端口
  • sudo lsof -i :80:看 80 端口被哪个进程占用
  • ps -p PID -o pid,ppid,user,command:查进程详情

五、第四步:处理冲突

“先停掉 Python。”老周说。

小林:

kill 12345

“然后重启 Nginx。”

sudo systemctl restart nginx
sudo systemctl status nginx

输出:

Active: active (running)

“再看端口。”

sudo ss -lntp | grep :80

输出:

LISTEN 0 511 0.0.0.0:80 0.0.0.0:* users:(("nginx",pid=1236,fd=6))

“80 端口回来了。”小林说。

“验证一下。”

curl -I http://localhost

输出:

HTTP/1.1 200 OK

“博客回来了。”小林松了口气。

“但你别急着高兴。”老周说,“你刚才犯了一个错。”

“什么错?”

“你从 Mac 到 Ubuntu,从服务到端口,从配置到日志,一路查下来,用了十几条命令。但你一开始就查 ss -lntp,可能早就发现 80 没监听,然后直接看 error.log,就能定位到 Address already in use。”

“那怎么更快?”

“排错要有顺序,但也要有重点。”老周说,“你以后遇到‘服务打不开’,先查三步:”

  1. 服务在跑吗? systemctl status
  2. 端口在监听吗? ss -lntp
  3. 响应正常吗? curl -I

“这三步,30 秒能查完。如果三步都正常,再查配置、日志、防火墙。别一上来就查网络、查 DNS、查负载。”

小林记下:

  • 排错三步:服务 → 端口 → 响应
  • 服务在跑,不代表端口在监听
  • 端口没监听,先看 error.log
  • Address already in use = 端口被占
  • lsof -i :80 找占用进程

六、第五步:监控为什么没告警?

“博客恢复了,但还有一个问题。”老周说,“监控为什么没告警?”

“因为我只监控了 node_exporter 的 up,没监控 Nginx 的 up。”

“对。”老周说,“你 up{job="node"} 是 1,因为 node_exporter 活着。但 Nginx 挂了,node_exporter 不知道。”

“那怎么监控 Nginx?”

“两种方式。”老周说,“第一,用 nginx-prometheus-exporter,专门暴露 Nginx 指标。第二,用 blackbox_exporter,从外部探测 HTTP 响应。”

“哪个简单?”

“你入门,先用 blackbox_exporter。”老周说,“它从外部发 HTTP 请求,看响应码和响应时间。如果 Nginx 挂了,它探测不到,up 变 0,就告警。”

“那怎么装?”

“你先把 Nginx 监控起来,明天再讲。”老周说,“今天你先记住:监控要覆盖‘服务层’,不能只监控‘机器层’。 CPU 正常、内存正常、磁盘正常,不代表 Nginx 正常。你得单独监控 Nginx。”

小林记下:

  • node_exporter 监控机器层
  • blackbox_exporter 监控服务层(HTTP、TCP、ICMP)
  • nginx-prometheus-exporter 监控 Nginx 内部指标
  • 机器层正常 ≠ 服务层正常

七、排错挑战:Nginx 配置改了,nginx -t 通过,但 reload 后服务挂了

下午,小林想给 Nginx 加一个 location,把 /api/ 反向代理到后端。他改完配置:

sudo nginx -t

输出:

nginx: configuration file /etc/nginx/nginx.conf test is successful

“通过了。”小林说。

sudo systemctl reload nginx

输出:

Job for nginx.service failed because the control process exited with error code.

“reload 失败了?”小林懵了,“nginx -t 明明通过啊。”

他检查状态:

sudo systemctl status nginx

输出:

Active: active (running)

“还在跑。”小林说,“但 reload 失败了?”

他检查日志:

sudo tail -20 /var/log/nginx/error.log

输出:

2026/09/14 14:00:01 [emerg] 1234#1234: invalid URL prefix in /etc/nginx/sites-enabled/blog:10

“invalid URL prefix。”小林说,“第 10 行。”

他看配置:

location /api/ {
    proxy_pass http://127.0.0.1:8080/api/;
}

“proxy_pass 后面没有协议?”老周路过看了一眼,“你写 http:// 了吗?”

“写了啊。”

“那第 10 行是啥?”

小林仔细看:

location /api/ {
    proxy_pass http://127.0.0.1:8080/api/;
}

“没问题啊。”

“你 nginx -t 什么时候跑的?”

“改完配置就跑了。”

“nginx -t 用的是磁盘上的配置。但你 reload 的时候,Nginx 重新读配置,如果配置文件在 nginx -t 之后又改过,nginx -t 的结果就不准了。”老周说,“你 nginx -t 之后,又改了配置吗?”

小林想了想:“我改了缩进……”

“那就是了。”老周说,“nginx -t 只检查当前磁盘上的配置。你改完配置,必须再跑一次 nginx -t,然后再 reload。”

小林重新跑:

sudo nginx -t

输出:

nginx: [emerg] invalid URL prefix in /etc/nginx/sites-enabled/blog:10
nginx: configuration file /etc/nginx/nginx.conf test failed

“这次报错了。”小林说。

“那第 10 行到底啥问题?”

小林打开文件,仔细看:

    proxy_pass http://127.0.0.1:8080/api/;

“等等,proxy_pass 后面的 URL,如果 location 里写了 /api/,proxy_pass 也写了 /api/,会怎样?”

“会双重拼接。”老周说,“但这不是 invalid URL prefix。”

“那是什么?”

老周凑近看:“你 http:// 中间有没有空格?http: //?”

小林仔细看:“没有啊。”

“你 cat -A 看。”

sudo cat -A /etc/nginx/sites-enabled/blog | sed -n '10p'

输出:

    proxy_pass http://127.0.0.1:8080/api/;$

“看起来正常。”老周说,“你把整段 location 删掉,只留 proxy_pass http://127.0.0.1:8080; 试试。”

小林改:

location /api/ {
    proxy_pass http://127.0.0.1:8080;
}

再次测试:

sudo nginx -t

输出:

SUCCESS

“成了。”小林说。

“所以问题在 proxy_pass 的 URL 上。”老周说,“proxy_pass 后面如果带路径,location 和 proxy_pass 的路径匹配有讲究。 你 location /api/ 和 proxy_pass http://127.0.0.1:8080/api/,Nginx 会把 location 匹配的 /api/ 替换成 proxy_pass 的 /api/,结果路径重复。但这不是语法错误,是逻辑错误。 你刚才的 invalid URL prefix 可能是别的问题,但改完就好了。”

排错思路总结:

  1. nginx -t 通过,但 reload 失败,说明 nginx -t 之后配置又被改过。
  2. 改配置 → nginx -t → reload,顺序不能反。 每次改完配置,都要重新 nginx -t。
  3. invalid URL prefix 通常是 proxy_pass 的 URL 格式问题。
  4. proxy_pass 带路径时,location 和 proxy_pass 的路径要匹配,避免重复拼接。
  5. 用 cat -A 检查不可见字符。
  6. reload 失败,Nginx 会保留旧配置继续跑。 所以 systemctl status 显示 active,但新配置没生效。

“记住,”老周说,“nginx -t 和 reload 是一对,不能分开。 你改完配置,先 nginx -t,再 reload。reload 失败,看 error.log,改完再 nginx -t。循环到成功为止。”

八、全链路排错清单

老周拿过小林的笔记本,写了一个清单。

当博客打不开时,按顺序查:

1. Mac 能 ping 通 Ubuntu 吗?
   ping 192.168.1.x

2. Mac 能 SSH 到 Ubuntu 吗?
   ssh devops@192.168.1.x

3. Nginx 服务在跑吗?
   systemctl status nginx

4. 80 端口在监听吗?
   sudo ss -lntp | grep :80

5. 谁占了 80?
   sudo lsof -i :80

6. Nginx 配置对不对?
   sudo nginx -t

7. Nginx 错误日志说什么?
   sudo tail -30 /var/log/nginx/error.log

8. HTTP 响应正常吗?
   curl -I http://localhost

9. 防火墙拦了吗?
   sudo ufw status

10. 端口转发配了吗?
    VMware Fusion 网络设置

11. 监控告警了吗?
    Prometheus 中 Nginx 的 up 指标

“这 11 步,你背下来。”老周说,“以后博客打不开,按顺序查,别跳步。”

小林把清单抄了一遍。

九、练习

  1. 练习一:模拟一次“端口被占”故障。用 python3 -m http.server 80 占住 80 端口,然后 systemctl restart nginx,观察 error.log 报错。用 lsof -i :80 找到 Python 进程,kill 后重启 Nginx,确认恢复。
  2. 练习二:模拟一次“配置改错”故障。在 Nginx 配置中把 proxy_pass http://127.0.0.1:8080; 改成 proxy_pass http://127.0.0.1:8080/api/;,nginx -t 观察是否报错。如果报错,改回来,再次 nginx -t 和 reload。
  3. 练习三:按全链路排错清单,从 Mac 到 Ubuntu,逐条执行,把每条命令的输出保存到 ~/troubleshoot_log.txt。

参考答案

练习一:

python3 -m http.server 80 &
sudo systemctl restart nginx
sudo tail -20 /var/log/nginx/error.log
sudo lsof -i :80
kill $(sudo lsof -t -i :80)
sudo systemctl restart nginx
curl -I http://localhost

练习二:

sudo nano /etc/nginx/sites-enabled/blog

改 proxy_pass,测试:

sudo nginx -t

如果报错,改回来:

sudo nginx -t
sudo systemctl reload nginx

练习三:

{
    echo "=== 1. ping ==="
    ping -c 3 192.168.1.x
    echo "=== 2. ssh ==="
    ssh devops@192.168.1.x "hostname"
    echo "=== 3. systemctl ==="
    ssh devops@192.168.1.x "systemctl status nginx | head -5"
    echo "=== 4. ss ==="
    ssh devops@192.168.1.x "sudo ss -lntp | grep :80"
    echo "=== 5. lsof ==="
    ssh devops@192.168.1.x "sudo lsof -i :80"
    echo "=== 6. nginx -t ==="
    ssh devops@192.168.1.x "sudo nginx -t"
    echo "=== 7. error.log ==="
    ssh devops@192.168.1.x "sudo tail -30 /var/log/nginx/error.log"
    echo "=== 8. curl ==="
    ssh devops@192.168.1.x "curl -I http://localhost"
    echo "=== 9. ufw ==="
    ssh devops@192.168.1.x "sudo ufw status"
} > ~/troubleshoot_log.txt

cat ~/troubleshoot_log.txt

十、下集预告

全链路排错做完了,小林第一次体验了“从浏览器查到服务器”的完整流程。老周说:“你排查能力有了,但你还缺一样东西——预判。”小林问:“预判什么?”老周说:“预判下一次故障。”下一集,老周带小林做最后的综合复盘和结语。

下一集:《完结篇:从“会敲命令”到“能扛事”》