1 概述:
1.1 环境
版本信息如下:
a、操作系统:centos 7.6
b、kubernetes版本:v1.15.0
c、ingress nginx版本:0.47.0
1.2 nginx的access log的时间精度
nginx的访问日志的时间字段的精度默认为秒,但提供了内置变量$msec,它是一个精度为毫秒的时间戳(http://nginx.org/en/docs/http/ngx_http_core_module.html#var_msec)。有了这个基础,可通过一些方式来生成我们想要的精度为毫秒的时间,例如2022-05-18T13:32:37.027+00:00。
2 修改ingress nginx的configmap
1)使用http-snippet字段来注入nginx配置指令,nginx配置指令会生成自定义变量 $time_iso8601_ms 。
注入的nginx配置指令,来源于文章 https://rotadev.com/can-nginx-log-time-in-iso-8601-format-but-include-milliseconds-server-fault/。
2)使用log-format-upstream字段来设置访问日志字段的格式,时间字段的值使用 $time_iso8601_ms 。
apiVersion: v1
data:
http-snippet: |
map "$time_iso8601 # $msec" $time_iso8601_ms { "~(^[^+]+)(\+[0-9:]+) # \d+\.(\d+)$" $1.$3$2; }
log-format-upstream: '{"time": "$time_iso8601_ms", "remote_addr": "$proxy_protocol_addr",
"x_forward_for": "$proxy_add_x_forwarded_for", "request_id": "$req_id", "remote_user":
"$remote_user", "bytes_sent": $bytes_sent, "request_time": $request_time, "status":
$status, "vhost": "$host", "request_proto": "$server_protocol", "path": "$uri",
"request_query": "$args", "request_length": $request_length, "duration": $request_time,"method":
"$request_method", "http_referrer": "$http_referer", "http_user_agent": "$http_user_agent"
}'
kind: ConfigMap
metadata:
annotations:
labels:
app.kubernetes.io/name: ingress-nginx
app.kubernetes.io/part-of: ingress-nginx
name: nginx-configuration
namespace: ingress-nginx
3 测试
nginx的日志如下图: