Nginx 错误日志的作用:
- 问题诊断:记录详细的错误信息,帮助系统管理员快速定位问题源头,例如配置错误、资源访问异常等。
- 配置审核:通过错误日志可以发现配置文件中的潜在问题,确保服务器配置的正确性。
- 资源管理:错误日志可以揭示资源使用中的问题,如文件权限不足、磁盘空间不足等。
- 安全监控:记录的安全相关错误可以揭示潜在的攻击尝试,如非法访问、注入攻击等。
触发条件:
- 错误发生:任何时候服务器遇到错误,比如处理请求失败、配置文件语法错误等,错误信息都会被记录。
- 警告和通知:某些情况下,Nginx 可能记录警告信息或重要通知,这些通常不是错误,但对系统管理员了解服务器状态很重要。
1.配置请求日志
1.1 日志来源
- 默认日志路径: /var/log/nginx/error.log
- 默认配置文件路径: /etc/nginx/nginx.conf
- 修改日志路径和格式: 提供示例配置(通过变更配置文件内容修改日志路径,请求日志格式)
# 主配置文件 /etc/nginx/nginx.conf
http {
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
# 错误日志配置,定义日志路径和日志级别(error, warn, notice, info, debug)
error_log /var/log/nginx/error.log warn;
# Gzip 压缩设置
gzip on;
gzip_disable "msie6";
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
# 虚拟主机配置文件示例 /etc/nginx/sites-enabled/example.com.conf
server {
listen 80;
server_name example.com www.example.com;
# 定义虚拟主机的错误日志路径和日志级别
error_log /var/log/nginx/example_error.log error;
access_log /var/log/nginx/example_access.log custom;
location / {
root /var/www/html;
index index.html index.htm;
}
}
1.2 配置测试和重载
- 测试配置: sudo nginx -t
- 重载配置: sudo systemctl reload nginx
- 重新启动: sudo systemctl restart nginx
1.3 字段参考(不支持自定义) Module ngx_http_log_module
字段名 | 字段用例 | 正则表达式 | 字段解释 | 是否必填 | |
1 | client | 192.168.1.1 | \S* | 发起请求的客户端的 IP 地址。 | Y |
2 | message | GET /index.html HTTP/1.1 | (GET|POST|PUT|DELETE|PATCH|HEAD) \S+ \S+/\d\.\d | 消息内容,描述了日志条目的具体信息。 | Y |
3 | host | 192.168.1.1 | \d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3} | 客户端尝试访问的域名或 IP 地址。 | N |
4 | cid | 02/Jul/2024:06:30:20 +0000 | \d{2}/\w{3}/\d{4}:\d{2}:\d{2}:\d{2} [+\-]\d{4} | 连接ID,是 Nginx 用来标识特定连接的唯一标识符。 | N |
5 | server | https://example.com | \S+ | 表示处理请求的服务器的名称或地址。 | Y |
6 | severity | https://example.com/previous-page | (?:https?://)?(?:[-\w]+\.)+[-\w]+(?:/\S*)? | 日志的严重性级别。 | N |
7 | tid | Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36 | .+ | 线程ID,表示处理请求的线程的唯一标识符。 | N |
8 | upstream | 80 /"-" | /d+\S | 上游服务器的地址。 | N |
9 | method | 1234 | \d+ | 请求方法。 | N |
10 | path | 1 | /d+ | 请求路径URL。 | N |
11 | http_version | 1 | /d+ | 请求路径URL。 | N |
- 日志采集概述
2.1 error日志格式样例
2024/05/14 23:13:59 [error] 490829#0: *6 open() "/usr/local/nginx/html/group/conf" failed (2: No such file or directory), client: 111.42.240.165, server: localhost, request: "GET /group/conf HTTP/1.1", host: "106.54.46.2"
日志字段解释
- 时间戳:2024/05/14 23:13:59,错误发生的日期和时间。
- 日志级别:[error],表明这是一个错误级别的日志条目。
- 进程信息:490829#0,表示日志消息是由 Nginx 进程 ID 490829 处理的。
- 连接序号:*6,Nginx 工作进程中的连接序列号。
- 错误详情:open() "/usr/local/nginx/html/group/conf" failed (2: No such file or directory),尝试打开文件时失败,错误码为 2,表示 "没有找到文件或目录"。
- 客户端信息:client: 111.42.240.165,发起请求的客户端 IP 地址。
- 服务器信息:server: localhost,处理请求的服务器名称。
- 请求详情:request: "GET /group/conf HTTP/1.1",客户端发起的 HTTP 请求内容。
- 请求的主机:host: "106.54.46.2",客户端尝试访问的域名或 IP 地址。
日志条目解释
这条 Nginx 错误日志记录了在 2024 年 5 月 14 日 23:13:59,Nginx 进程 ID 490829 的连接序号 6 在尝试处理来自 IP 地址 111.42.240.165 的客户端对 "/group/conf" 路径的 GET 请求时发生了错误。错误是由于服务器尝试打开 "/usr/local/nginx/html/group/conf" 文件失败,因为该文件或目录不存在。请求是通过 localhost 服务器,使用 HTTP/1.1 协议,目标主机为 "106.54.46.2" 发起的。
2.2 error日志采集方式
Vector配置
{
"agent": "Mozilla/5.0",
"agent_version": "1.1",
"category": "网关",
"client": "111.42.240.186",
"file": "access.log",
"host": "VM-4-7-ubuntu",
"id": "测试id",
"log_at": "2024-05-19 16:55:46",
"log_level": "INFO",
"message": "111.42.240.186 - - [20/May/2024:00:55:46 +0800] \"POST /myfiles?cmd=count HTTP/1.1\" 200 42 \"-\" \"Mozilla/5.0\"",
"numbers": {
"response_size": 42,
"status_code": 200
},
"parsed_request": {
"method": "POST",
"path": "/myfiles?cmd=count",
"protocol": "HTTP",
"protocol_version": "1.1"
},
"raw_data": "111.42.240.186 - - [20/May/2024:00:55:46 +0800] \"POST /myfiles?cmd=count HTTP/1.1\" 200 42 \"-\" \"Mozilla/5.0\"",
"referer": "-",
"request": "POST /myfiles?cmd=count HTTP/1.1",
"size": 42,
"source": {
"host": "VM-4-7-ubuntu",
"ip": "111.42.240.186",
"product": "Nginx",
"program": "nginx",
"region": "China"
},
"status": 200,
"strings": {
"http_referer": "-",
"method": "POST",
"path": "/myfiles?cmd=count",
"protocol": "HTTP",
"protocol_version": "1.1",
"request": "POST /myfiles?cmd=count HTTP/1.1",
"server": null,
"upstream": null
},
"timestamp": "2024-05-19T16:55:46Z",
"topic": "nginx_access",
"type": "accesslog"
}
2.3 error日志格式样例解析结果
{
"agent_version": "1.1",
"category": "网关",
"client": "111.42.240.165",
"file": "error.log",
"host": "106.54.46.2",
"id": "测试id",
"log_at": "2024-05-14 15:13:59",
"log_level": "error",
"message": "open() \"/usr/local/nginx/html/group/conf\" failed (2: No such file or directory)",
"numbers": {
"cid": 6,
"pid": 490829,
"tid": 0
},
"parsed_request": {
"method": "GET",
"path": "/group/conf",
"protocol": "HTTP",
"protocol_version": "1.1"
},
"raw_data": "2024/05/14 23:13:59 [error] 490829#0: *6 open() \"/usr/local/nginx/html/group/conf\" failed (2: No such file or directory), client: 111.42.240.165, server: localhost, request: \"GET /group/conf HTTP/1.1\", host: \"106.54.46.2\"",
"request": "GET /group/conf HTTP/1.1",
"severity": "error",
"source": {
"host": "106.54.46.2",
"ip": "111.42.240.165",
"product": "Nginx",
"program": "nginx",
"region": "China"
},
"strings": {
"http_referer": null,
"method": "GET",
"path": "/group/conf",
"protocol": "HTTP",
"protocol_version": "1.1",
"request": "GET /group/conf HTTP/1.1",
"server": "localhost",
"upstream": null
},
"timestamp": "2024-05-14T15:13:59Z",
"topic": "nginx_error",
"type": "errorlog"
}
- 注意点
3.1 nginx 默认位置没有日志文件处理方法
查找Nginx配置文件,使用 find 命令在整个系统中搜索 nginx.conf 文件:
bash:
sudo find / -name "nginx.conf"
找到配置文件后,使用文本编辑器(如 vim)打开它进行编辑:
bash:
sudo vim /etc/nginx/nginx.conf
在配置文件中,查找 error_log 的设置。确保它们指向有效的路径,并且这些路径存在:
nginx:
error_log /var/log/nginx/error.log;
确认日志目录存在:
检查日志文件指定的目录是否存在。如果不存在,需要创建它们:
bash
sudo mkdir -p /var/log/nginx
设置正确的权限:
确保 Nginx 进程(通常是由 www-data 用户运行)有权限写入日志文件的目录。
bash
sudo chown -R www-data:www-data /var/log/nginx
sudo chmod -R 755 /var/log/nginx
测试配置文件:
在修改配置文件后,使用以下命令测试配置文件是否有语法错误。
bash
sudo nginx -t
重载或重启 Nginx:
如果配置测试通过,重载或重启 Nginx 以应用更改。
bash
sudo systemctl restart nginx
3.2 一条日志既不符合access log格式 也不符合error log 提取
只保留原数据massage字段其他字段为空存入数据库
3.3 docker容器部署的nginx处理:
3.3.1. 读取docker nginx logs输出 :参考Docker logs | Vector documentation
sources:
docker_logs:
type: "docker_logs"
include_containers: ["nginx"]
3.3.2. 读取docker容器文件 :
sources:
container_logs:
type: "file"
# 包含要监控的日志文件的路径,可以使用通配符
include: ["/path/to/container/logs/*.log"]
# 从文件末尾开始读取,只捕获新日志
read_from: "end"
4. 日志用途与字段分析
4.1 日志用途
Nginx 错误日志对于网络安全和系统运维至关重要,主要用途包括:
- 问题诊断:识别服务器内部错误,如配置错误或资源访问问题。
- 性能监测:分析错误发生频率和类型,评估系统性能和稳定性。
- 安全审计:检测潜在的恶意访问和攻击行为,加强安全防护。
- 访问监控:了解访问模式,预防未授权访问和数据泄露。
4.2 数据字段分析
client
- 用途:追踪请求发起者的IP地址。
- 分析:如果client字段显示某个IP地址频繁出现错误请求,如“111.222.333.44”,可能表明该IP正在进行恶意扫描或攻击。
message
- 用途:提供具体的错误描述。
- 分析:例如,如果message包含“open() “/usr/local/nginx/html/index.html” failed (2: No such file or directory)”,这表明请求的文件不存在,需要检查文件路径或访问权限。
host
- 用途:记录请求的目标域名。
- 分析:如果host字段显示为“example.com”,但请求的资源不存在,可能需要更新DNS设置或服务器配置。
cid
- 用途:标识特定的连接。
- 分析:通过cid可以追踪到具体的连接过程中发生的错误,例如“[error] 490829#0: *6”,这里的“490829#0”表示进程ID和连接序列。
server
- 用途:标识处理请求的服务器。
- 分析:server字段可以帮助确定是哪个虚拟主机或服务器发生了错误,例如“localhost”。
severity
- 用途:表示错误的严重性级别。
- 分析:例如,如果severity是“error”,则表示发生了一个需要立即关注的严重问题。
tid
- 用途:标识处理请求的线程。
- 分析:在多线程环境中,tid可以帮助识别和分析线程相关的问题,如“*6”表示第6个线程。
upstream
- 用途:标识上游服务器。
- 分析:如果日志中upstream字段出现问题,如“upstream connect timeout”,可能表明上游服务响应超时,需要检查上游服务的可用性。
method
- 用途:记录使用的HTTP方法。
- 分析:如果method显示为不常用的方法,如“DELETE”或“PATCH”,需要进一步检查请求的合法性。
path
- 用途:记录请求的资源路径。
- 分析:如果path指向敏感资源,如“/api/delete_user”,但请求没有合法的认证,可能表明有未授权访问的尝试。
http_version
- 用途:记录使用的HTTP协议版本。
- 分析:如果http_version显示为较旧的版本,如“HTTP/1.0”,可能需要提示用户升级或检查潜在的降级攻击。
5. 参考
- Nginx 官方文档: 用于理解 Nginx 配置和日志处理。