栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 系统运维 > 运维 > Linux

ingress nginx日志时间精确到毫秒

Linux 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

ingress nginx日志时间精确到毫秒

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的日志如下图:

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/913690.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号