access.log 网站日志分析器

可选:nginx log_format 指令格式,为空使用 nginx 默认 combined 格式

网站日志字段说明

Nginx 的 log_format 指令用于定义 access.log 网站日志格式。以下是一些常用的变量说明:

基本变量

变量 说明 示例
$remote_addr 客户端 IP 地址 192.168.1.100
$remote_user 通过 HTTP 基本认证的用户名(如果存在) admin-
$time_local 本地时间(服务器时间) 25/Dec/2023:10:30:45 +0800
$time_iso8601 ISO 8601 格式的时间 2023-12-25T10:30:45+08:00
$request 完整的请求行(方法、URI、协议) GET /index.html HTTP/1.1
$request_method HTTP 请求方法 GET, POST, PUT, DELETE
$request_uri 完整的请求 URI(包含查询参数) /index.html?id=123
$uri 请求 URI(不包含查询参数,经过规范化处理) /index.html
$args 查询字符串(URL 参数) id=123&name=test
$status HTTP 响应状态码 200, 404, 500
$body_bytes_sent 发送给客户端的响应体大小(字节,不包括响应头) 1024
$bytes_sent 发送给客户端的总字节数(包括响应头和响应体) 2048

HTTP 请求头变量

变量 说明 示例
$http_user_agent 客户端 User-Agent(浏览器信息) Mozilla/5.0 (Windows NT 10.0; Win64; x64)...
$http_referer 来源页面(Referer) https://www.example.com/page-
$http_host 请求的 Host 头 www.example.com
$http_accept Accept 头 text/html,application/xhtml+xml
$http_accept_language Accept-Language 头 zh-CN,zh;q=0.9,en;q=0.8
$http_accept_encoding Accept-Encoding 头 gzip, deflate, br
$http_cookie Cookie 头 sessionid=abc123; user=test
$http_x_forwarded_for X-Forwarded-For 头(代理链中的客户端 IP) 192.168.1.100, 10.0.0.1
$http_x_real_ip X-Real-IP 头(真实客户端 IP,通常由反向代理设置) 192.168.1.100

服务器信息变量

变量 说明 示例
$scheme 请求协议(http 或 https) http, https
$host 请求的主机名(优先使用 Host 头,否则使用 server_name) www.example.com
$server_name 服务器名称(server_name 指令的值) www.example.com
$server_addr 服务器 IP 地址 192.168.1.1
$server_port 服务器端口 80, 443
$request_time 请求处理时间(秒,精确到毫秒) 0.123
$upstream_response_time 上游服务器响应时间(如果使用了反向代理) 0.045
$upstream_addr 上游服务器地址(如果使用了反向代理) 192.168.1.2:8080

常用格式示例

格式名称 log_format 定义 说明
combined(默认) $remote_addr - $remote_user [$time_local] "$request" $status $body_bytes_sent "$http_referer" "$http_user_agent" Nginx 默认的 combined 格式,包含基本信息
extended $remote_addr - $remote_user [$time_local] "$request" $status $body_bytes_sent "$http_referer" "$http_user_agent" "$http_x_forwarded_for" 包含 X-Forwarded-For 头,适用于反向代理场景
detailed $remote_addr - $remote_user [$time_local] "$request" $status $body_bytes_sent "$http_referer" "$http_user_agent" $request_time $upstream_response_time 包含请求处理时间和上游响应时间
custom $time_local - $status - $body_bytes_sent - $remote_addr - $scheme://$host$request_uri - $http_user_agent - $http_referer - $http_x_forwarded_for 自定义格式,使用分隔符 - 分隔字段

注意事项

  • 变量命名规则:HTTP 请求头变量使用 $http_ 前缀,例如 $http_user_agent$http_referer。注意 $http_referer 的拼写是 "referer"(HTTP 规范中的拼写错误,但已约定俗成)。
  • 空值表示:如果某个变量没有值,通常会显示为 -(在日志格式中显式指定时)或空字符串。
  • 引号使用:包含空格的变量值(如 $request$http_user_agent)建议用引号括起来,便于解析。
  • 时间格式$time_local 使用服务器本地时区,$time_iso8601 使用 ISO 8601 标准格式。
  • URI 与 URL$request_uri 包含查询参数,$uri 不包含查询参数且经过规范化处理(如去除重复斜杠)。$scheme://$host$request_uri 可以组成完整的 URL。
  • 代理场景:在使用反向代理时,$remote_addr 可能是代理服务器的 IP,真实客户端 IP 通常在 $http_x_forwarded_for$http_x_real_ip 中。
  • 性能变量$request_time$upstream_response_time 对于性能分析很有用,但会增加一定的性能开销。

更多信息

查阅 Nginx 官方文档 - log_format 获取完整的变量列表和详细说明。