熱門服務
眾多企業的選擇

Nginx log日志是我們大數據分析的重要文件,其中有個時間戳字段,Nginx本身再帶了兩種類型:
展示樣式基本為:"04/Sep/2020:18:22:03 +0800" 這種類型,打開日志查看記錄時很不方便,所以更好格式化為YYYY-MM-DD hh:ss:mm 這樣看的比較舒服
# 自定義時間變量
if ($time_iso8601 ~ "^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})") {
set $year $1;
set $month $2;
set $day $3;
set $hour $4;
set $minutes $5;
set $seconds $6;
}
log_format mylog '{"timestamp": "$year$month$day $hour:$minutes:$seconds",'
'"host": "$remote_addr",'
'"client_id": "$remote_user",'
'"request": "$request",'
'"agent": "$http_user_agent"}';
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的時間【版權聲明】:本站內容來自于與互聯網(注明原創稿件除外),供訪客免費學習需要。如文章或圖像侵犯到您的權益,請及時告知,我們第一時間刪除處理!謝謝!