1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
|
## 首先定义请求头, 请求体
server
{
...
set $resp_body "";
set $req_body "";
set $req_headers "";
rewrite_by_lua_block {
local req_headers = "Headers: ";
ngx.var.req_body = ngx.req.get_body_data();
local h, err = ngx.req.get_headers()
for k, v in pairs(h) do
req_headers = req_headers .. k .. ": " .. v .. "\n";
end
ngx.var.req_headers = req_headers;
}
body_filter_by_lua '
local resp_body = string.sub(ngx.arg[1], 1, 1000)
ngx.ctx.buffered = (ngx.ctx.buffered or "") .. resp_body
if ngx.arg[2] then
ngx.var.resp_body = ngx.ctx.buffered
end
';
# 记录access
access_log /var/log/com.log json_combined;
}
## 再在server外定义nginx json
log_format json_combined escape=json '{"@timestamp":"$time_iso8601",'
'"@source":"$server_addr",'
'"@nginx_fields":{'
'"remote_addr":"$remote_addr",'
'"remote_user":"$remote_user",'
'"body_bytes_sent":"$body_bytes_sent",'
'"request_time":"$request_time",'
'"status":"$status",'
'"host":"$host",'
'"uri":"$uri",'
'"server":"$server_name",'
'"port":"$server_port",'
'"protocol":"$server_protocol",'
'"request_uri":"$request_uri",'
'"request_body":"$request_body",'
'"request_method":"$request_method",'
'"http_referrer":"$http_referer",'
'"body_bytes_sent":"$body_bytes_sent",'
'"http_x_forwarded_for":"$http_x_forwarded_for",'
'"http_user_agent":"$http_user_agent",'
'"http_header_authorization":"$http_authorization",'
'"upstream_response_time":"$upstream_response_time",'
\n
'"req_headers":"$req_headers",'
\n
'"resp_body":"$resp_body",'
\n
'"req_body":"$req_body",'
\n
'"upstream_addr":"$upstream_addr"}}';
|