当前位置: 代码网 > it编程>数据库>Access > Nginx access.log 与 error.log的区别小结

Nginx access.log 与 error.log的区别小结

2026年07月30日 Access 我要评论
详细讲解 nginx 中的 access.log 和 error.log 这两个核心日志文件。它们是理解服务器运行状况、排查问题、分析流量和性能调优的关键。核心摘要特性access.log (访问日志

详细讲解 nginx 中的 access.logerror.log 这两个核心日志文件。

它们是理解服务器运行状况、排查问题、分析流量和性能调优的关键。

核心摘要

特性access.log (访问日志)error.log (错误日志)
​​记录内容​​所有客户端访问请求的记录nginx 服务运行时的​​事件​​和​​错误​​信息
​​日志级别​​不适用(记录所有成功和失败的请求)debug, info, notice, ​​warn​​, ​​error​​, crit, alert, emerg
​​主要用途​​流量分析、行为分析、安全审计、性能监控​​故障排查​​、调试配置、监控服务器健康状态
​​默认路径​​/var/log/nginx/access.log/var/log/nginx/error.log
​​格式​​可高度自定义(使用 log_format 指令)相对固定,包含时间、级别、消息内容等

1. access.log (访问日志)

访问日志记录了每一个向 nginx 服务器发起的客户端请求。通过分析它,你可以知道谁在什么时候访问了什么内容,以及结果如何。

默认日志格式

nginx 的默认访问日志格式通常被称为 ​​“组合日志格式” (combined log format)​​。一条典型的记录看起来像这样:

192.168.1.100 - - [12/sep/2023:10:25:30 +0800] "get /images/logo.png http/1.1" 200 1845 "https://example.com/" "mozilla/5.0 (windows nt 10.0; win64; x64) applewebkit/537.36 (khtml, like gecko) chrome/115.0.0.0 safari/537.36"

字段解析:

  1. ​​192.168.1.100​​:客户端的 ip 地址。
  2. ​​-​​:由 identd 协议确定的用户身份(通常为破折号 -,因为此协议已不再使用)。
  3. ​​-​​:http 认证的用户名(如果请求需要认证,否则为 -)。
  4. ​​[12/sep/2023:10:25:30 +0800]​​:请求的时间戳,包括时区。
  5. ​​"get /images/logo.png http/1.1"​​:请求行,包括​​请求方法​​ (get)、​​请求的uri​​ (/images/logo.png) 和​​协议版本​​ (http/1.1)。
  6. ​​200​​:http ​​状态码​​。这是最重要的字段之一。
    • 2xx:成功(如 200 ok)
    • 3xx:重定向(如 301 moved permanently, 304 not modified)
    • 4xx:客户端错误(如 ​​404 not found​​, 403 forbidden)
    • 5xx:服务器错误(如 ​​502 bad gateway​​, 504 gateway timeout, 500 internal server error)
  7. ​​1845​​:服务器返回给客户端的​​响应体大小​​(字节数),不包括响应头。
  8. ​​"https://example.com/"​​:​​referer​​ 请求头,表示用户是从哪个页面链接过来的。
  9. ​​"mozilla/5.0 ..."​​:​​user-agent​​ 请求头,表示客户使用的浏览器、操作系统和设备信息。

自定义日志格式

你可以在 nginx 配置文件中使用 log_format 指令定义自己的格式。例如,添加一个记录请求处理时间的格式非常有用:

# 在 http {} 块中定义一种名为 'main' 的日志格式
http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for" '
                      '$request_time $upstream_response_time';
    # 在 server {} 或 location {} 块中使用这种格式
    server {
        access_log  /var/log/nginx/access.log  main;
        ...
    }
}
  • ​​$request_time​​:处理请求所花费的总时间(秒),从读取客户端第一个字节到发送完响应最后一个字节。
  • ​​$upstream_response_time​​:nginx 向后端服务器(如 php-fpm, tomcat)发起请求到接收完响应所花费的时间。对于诊断后端性能问题至关重要。
  • ​​$http_x_forwarded_for​​:当服务器位于负载均衡器或cdn之后时,这个头通常包含客户端的真实ip。

2. error.log (错误日志)

错误日志记录了 nginx 服务运行时遇到的各种事件和问题,从调试信息到严重错误。它的详细程度由设置的日志级别决定。

日志级别 (从低到高)

  • debug:最详细的调试信息,通常只在开发或深度排查时使用。
  • info:一般信息性消息。
  • notice:正常但值得注意的事件。
  • warn:警告信息,可能的问题,但不会影响服务。
  • error:处理请求时遇到的错误(如连接上游服务器失败)。
  • crit:临界错误,需要立即关注。
  • alert:需要立即采取行动的严重错误。
  • emerg:系统不可用的紧急错误。

常见错误信息示例

​权限被拒绝 (permission denied)​

2023/09/12 10:25:30 [error] 12345#0: *100 open() "/var/www/html/secret.txt" failed (13: permission denied), client: 192.168.1.100, server: example.com, request: "get /secret.txt http/1.1", host: "example.com"

​原因​​:nginx worker 进程(通常是 www-datanginx 用户)没有读取该文件的权限。

​文件不存在 (no such file or directory)​

2023/09/12 10:26:15 [error] 12345#0: *101 open() "/var/www/html/missing.jpg" failed (2: no such file or directory), client: 192.168.1.100, server: example.com, request: "get /missing.jpg http/1.1", host: "example.com"

​原因​​:请求的文件在服务器上不存在。这通常会在 access.log 中产生一个 404 状态码。

​无法连接到上游服务 (connection refused to upstream)​

2023/09/12 10:27:00 [error] 12345#0: *102 connect() failed (111: connection refused) while connecting to upstream, client: 192.168.1.100, server: example.com, request: "get /api/ http/1.1", upstream: "http://127.0.0.1:8080/api/", host: "example.com"

​原因​​:nginx 无法连接到配置的后端服务器(如 tomcat, node.js, php-fpm)。可能原因是后端服务没有启动或监听端口错误。这通常会在 access.log 中产生一个 502504 状态码。

​上游响应超时 (upstream timed out)​

2023/09/12 10:28:30 [error] 12345#0: *103 upstream timed out (110: connection timed out) while reading response header from upstream, client: 192.168.1.100, server: example.com, request: "get /slow-query/ http/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "example.com"

​原因​​:后端服务器处理请求时间过长,超过了 nginx 配置的 proxy_read_timeoutfastcgi_read_timeout 值。

​配置错误​

2023/09/12 09:00:00 [emerg] 12345#0: unknown directive "stubstaus" in /etc/nginx/conf.d/status.conf:1

  • ​原因​​:nginx 配置文件中有语法错误或使用了未知指令。通常在重载或重启 nginx 时出现,会导致 nginx 启动失败。

管理与最佳实践

  1. ​日志轮转 (log rotation)​
    日志文件会不断增长,需要定期切割和归档。linux 系统通常使用 logrotate 工具来管理,nginx 安装后通常会自带一个配置文件 (/etc/logrotate.d/nginx)。它会自动按天或按周切割日志,并压缩旧文件。

  2. ​调试技巧​

    • ​实时查看日志​​:使用 tail 命令。
      # 实时查看访问日志
      tail -f /var/log/nginx/access.log
      # 实时查看错误日志
      tail -f /var/log/nginx/error.log
      # 实时查看错误日志,并过滤出 ‘error' 级别的记录
      tail -f /var/log/nginx/error.log | grep -i error
    • ​关联分析​​:当在 access.log 中发现一个 502 错误时,立即去查看同一时间点的 error.log,通常能找到对应的详细错误信息。
  3. ​性能考虑​
    在高流量网站下,记录过多信息(如完整的 user-agent)或使用 debug 级别可能会影响磁盘 i/o 和性能。应根据实际需求定制日志格式和级别。

总结来说,access.log 是你了解​​谁来了​​和​​干了什么​​的窗口,而 error.log 则是你了解​​服务器哪里不舒服​​的诊断报告。两者结合使用,是管理和维护一个健康 nginx 服务器的必备技能。

到此这篇关于nginx access.log 与 error.log的区别小结的文章就介绍到这了,更多相关nginx access.log 与 error.log内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2026  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com