温馨提示×

debian nginx日志级别怎设

小樊
33
2026-10-21 20:33:21
栏目: 智能运维

在 Debian 下,Nginx 的日志级别是通过 nginx 配置文件 来设置的,不是系统服务参数。


一、Nginx 日志级别说明

Nginx 的 error_log 支持以下级别(从低到高):

debug
info
notice
warn
error
crit
alert
emerg

默认级别一般是:error

级别越低,日志越详细;debug 最详细,但性能影响较大。


二、设置方法(Debian 通用)

1️⃣ 修改主配置文件

编辑 Nginx 主配置:

sudo nano /etc/nginx/nginx.conf

在 events 或 http 块中设置:

error_log /var/log/nginx/error.log warn;

示例(http 块内全局生效):

http {
    error_log /var/log/nginx/error.log warn;
    ...
}

2️⃣ 针对某个虚拟主机设置

编辑站点配置(如 /etc/nginx/sites-enabled/default):

server {
    error_log /var/log/nginx/example-error.log debug;
    ...
}

3️⃣ access_log 的“级别”(注意)

access_log 没有日志级别概念,只有是否记录:

access_log off;

或:

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

三、开启 debug 日志(重要)

如果你想用 debug 级别:

✅ 必须满足两个条件

  1. 编译时支持 debug
  2. 配置中指定 debug

检查是否支持:

nginx -V 2>&1 | grep debug

如果 没有 --with-debug,Debian 默认包通常 不支持 debug 日志。


安装带 debug 的 Nginx(Debian)

sudo apt install nginx-extras

或源码编译时加:

--with-debug

四、修改后重新加载

sudo nginx -t
sudo systemctl reload nginx

五、示例汇总

# 全局
error_log /var/log/nginx/error.log error;

# 某 server
server {
    error_log /var/log/nginx/site.log info;
}

如果你愿意,可以告诉我:

  • 你是 想查问题 / 降日志量 / 开 debug
  • 用的 Debian 版本 + nginx 版本

我可以给你更精确的建议。

0 踩