怎么修改nginx的access.log的时间格式

    xiaoxiao2026-04-20  7

    说明

    nginx访问日志的时间格式,原格式为:

    03/Jun/2017:02:06:53 +0800

    这种日志格式,对于我们来说,不是我们习惯的日志格式。使用起来不方便。

    我们打算修改为

    2017-06-03 07:06:53

    我们有两种方法可以达到要求

    修改nginx源代码 参考:http://wolfchen.blog.51cto.com/2211749/1223803 利用lua的方式

    我们的原则是不修改nginx源代码的,以便平滑升级nginx,所以我们选择第二种方法,具体如下:

    lua 方法

    http { include /etc/nginx/mime.types; default_type application/octet-stream; log_format main '"$remote_addr" "$fmt_localtime" "$request_id" "$requ '"$status" "$body_bytes_sent" "$request_length" "$htt '"$http_user_agent" "$http_x_forwarded_for" "$http_ho #access_log /data/log/nginx/access.log main; map $host $fmt_localtime { default ''; } log_by_lua_block { ngx.var.fmt_localtime = ngx.localtime(); } ... }

    1) 首先我们自定了一个nginx 变量 $fmt_localtime,因为在http context不能够使用 set $variable。

    所以我们采用map的方式如下

    map $host $fmt_localtime { default ''; }

    2) 然后我们用 log_by_lua_block 设置 ngx.fmt_localtime的时间

    3) 设置日志格式 log_format

    最新回复(0)