温馨提示×

debian nginx日志为何丢失

小樊
45
2026-09-22 13:51:43
栏目: 智能运维

Debian 上 Nginx 日志“丢失”通常不是真的被删了,而是配置、轮转或权限等问题导致你“看不到”或“没写进去”。下面按最常见原因逐条排查。


一、最常见原因

1. Nginx 根本没配置该日志

检查你访问的 serverlocation

grep -R access_log /etc/nginx/
grep -R error_log /etc/nginx/

常见情况:

  • 只配置了 error_log,没配置 access_log
  • location 里写了:
    access_log off;
    

✅ 解决:在 serverhttp 块加上:

access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;

2. 日志被 logrotate 轮转后没 reopen

Debian 默认用 logrotate

cat /etc/logrotate.d/nginx

如果看到:

postrotate
  invoke-rc.d nginx rotate >/dev/null 2>&1
endscript

Nginx 没收到信号,就会继续写旧文件(已改名)。

✅ 检查:

ls -l /var/log/nginx/

如果你看到:

  • access.log.1
  • 但 nginx 还在写 access.log.1

说明 reopen 失败

✅ 解决:

nginx -s reopen

systemctl reload nginx

3. 日志目录或文件权限问题

Nginx worker 通常是 www-data

ps aux | grep nginx

检查:

ls -ld /var/log/nginx
ls -l /var/log/nginx/access.log

如果不是:

www-data www-data

✅ 修复:

chown -R www-data:adm /var/log/nginx
chmod 0755 /var/log/nginx

4. 使用了 systemd 的 Stdout,而不是文件

如果你看到:

error_log /dev/stderr;

access_log syslog:server=...;

那日志不在文件里

✅ 查看:

journalctl -u nginx

5. 磁盘满了 / inode 用完

df -h
df -i

✅ 如果 100%:

  • 清理日志
  • 或调整 logrotate

6. Nginx 没真正重启

改了配置但没生效:

nginx -t
systemctl restart nginx

二、快速自查清单(5 分钟)

nginx -t
ps aux | grep nginx
ls -l /var/log/nginx
grep access_log /etc/nginx/sites-enabled/*
journalctl -u nginx --no-pager | tail

三、如果你愿意

你可以贴出:

  • nginx -T 输出(脱敏)
  • ls -l /var/log/nginx
  • 你说“丢失”的时间点

我可以直接帮你定位是哪一类问题。

0