1、默认格式

log_format  main  '$remote_addr - $remote_user [$time_local] "$request" ' 
             '$status $body_bytes_sent  "$http_referer" ' 
             '"$http_user_agent"  $request_time';

# 记录形式
192.168.20.1 - - [07/Mar/2023:04:01:11 +0800] "GET /UserRecommend.php HTTP/1.1" 200870 "http://www.herlly.cn/herlly/index.html"
"Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1;Trident/4.0; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30)" 320

2、字段解析

说明

字段名

示例

主机头

$host

域名 www.helly.cn

服务器ip

$server_addr

192.168.20.1

端口

$server_port

80

客户ip

$remote_addr

192.168.5.1

客户

$remote_user

-

时间

$time_iso8601

2023-05-07T10:13:40+09:00

状态码

$status

204

发送主体大小

$body_bytes_sent

0

发送总大小

$bytes_sent

140

请求总大小

$request_length

578

请求主体大小

$request_body

-

请求时间

$request_time

0.001

请求方式

$request_method

GET

uri

$uri

/rest/quickreload/latest/18747370

变量

$args

since=1559180602998&_=1559181197999

协议

$server_protocol

HTTP/1.1

cookie

$cookie_nid

-

记录从哪个页面链接访问过来

$http_referer

http://www.herlly.cn/pages/viewpage.action?pageId=1874

客户端信息

$http_user_agent

Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.119 Safari/537.36

客户端真实ip(经过反向代理)

$http_x_forwarded_for

-

编码

$http_accept_encoding

gzip, deflate

当前通过一个连接获得的请求数量

$connection_requests

1

后端ip

$upstream_addr

192.168.20.10:8090

后端状态码

$upstream_status

204

后端响应时间

$upstream_status

0.001

后台缓存

$upstream_cache_status

-

后端接口状态

$upstream_http_x_status

-

3、模版

log_format main '$host\t$server_addr\t$server_port\t$remote_addr\t'
'$remote_user\t$time_iso8601\t$status\t'
'$body_bytes_sent\t$bytes_sent\t$request_length\t'
'$request_body\t$request_time\t$request_method\t'
'$uri\t$args\t$server_protocol\t$cookie_nid\t'
'$http_referer\t$http_user_agent\t$http_x_forwarded_for\t'
'$http_accept_encoding\t$connection_requests\t$upstream_addr\t'
'$upstream_status\t$upstream_response_time\t$upstream_cache_status\t$upstream_http_x_status';

4、json模版

log_format log_json '{"@timestamp": "$time_local", '
                        '"remote_addr": "$remote_addr", '
                        '"referer": "$http_referer", '
                        '"request": "$request", '
                        '"status": $status, '
                        '"bytes": $body_bytes_sent, '
                        '"agent": "$http_user_agent", '
                        '"x_forwarded": "$http_x_forwarded_for", '
                        '"up_addr": "$upstream_addr",'
                        '"up_host": "$upstream_http_host",'
                        '"up_resp_time": "$upstream_response_time",'
                        '"request_time": "$request_time"'
                  ' }';