更新时间:2022-09-16 15:26:19
由于公司领导说要把nginx访问日志存进数据库,然后利用程序进行分析,但是nginx访问日志的时间格式是:[17/Jun/2013:14:42:13 +0400] 这种格式不能用datetime格式存进数据库,只能以字符串的格式存进数据库,但是以字符串的格式存进数据库不好按天来进程查询分析,所以需要更改nginx访问日志的时间格式,经过网上查找资料得知更改源代码可以再进行编译可以更改nginx访问日志的格式,但是公司里的程序员都是PHP的,没有人懂C语言,想来想去只有我自己改源码然后编译安装(虽然我也不懂C)。
我需要更改的格式:
原格式为: 17/Jun/2013:14:42:13 +0400
需要修改成:2013-06-17 22:39:02
nginx版本是:nginx-1.4.1
1
2
|
static u_char cached_http_log_time[NGX_TIME_SLOTS]
[ sizeof ( "28/Sep/1970:12:00:00 +0600" )];
|
1
2
|
static u_char cached_http_log_time[NGX_TIME_SLOTS]
[ sizeof ( "1970-09-28 12:00:00" )];
|
1
|
ngx_cached_http_log_time.len = sizeof ( "28/Sep/1970:12:00:00 +0600" ) - 1;
|
1
|
ngx_cached_http_log_time.len = sizeof ( "1970-09-28 12:00:00" ) - 1;
|
1
2
3
4
5
6
7
|
p2 = &cached_http_log_time[slot][0]; ( void ) ngx_sprintf(p2, "%02d/%s/%d:%02d:%02d:%02d %c%02d%02d" ,
tm .ngx_tm_mday, months[ tm .ngx_tm_mon - 1],
tm .ngx_tm_year, tm .ngx_tm_hour,
tm .ngx_tm_min, tm .ngx_tm_sec,
tp->gmtoff < 0 ? '-' : '+' ,
ngx_abs(tp->gmtoff / 60), ngx_abs(tp->gmtoff % 60));
|
1
2
3
4
5
6
7
|
p2 = &cached_http_log_time[slot][0]; ( void ) ngx_sprintf(p2, "%4d-%02d-%02d %02d:%02d:%02d" ,
tm .ngx_tm_year, tm .ngx_tm_mon,
tm .ngx_tm_mday, tm .ngx_tm_hour,
tm .ngx_tm_min, tm .ngx_tm_sec,
tp->gmtoff < 0 ? '-' : '+' ,
ngx_abs(tp->gmtoff / 60), ngx_abs(tp->gmtoff % 60));
|
二、修改src/http/modules/ngx_http_log_module.c 文件,有一个地方需要修改
在220行:
1
|
{ ngx_string( "time_local" ), sizeof ( "28/Sep/1970:12:00:00 +0600" ) - 1
|
修改后:
1
|
{ ngx_string( "time_local" ), sizeof ( "1970-09-28 12:00:00" ) - 1
|
修改好后,重新编译nginx,编译安装完后用killall -9 nginx 干掉nginx在启动,然后查看nginx的访问日志,看看时间格式是不是改好了。
1
2
3
|
[root@localhost objs]# tail -n 0 -f /usr/local/nginx/logs/access.log 10.10.8.120 - - [2013-06-18 03:24:24] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.43 Safari/537.31" 10.10.8.120 - - [2013-06-18 03:24:24] "GET /favicon.ico HTTP/1.1" 404 570 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.43 Safari/537.31" |
看到上面的时间格式和我想要的是一样的,说明修改成功。
本文转自1594cqb 51CTO博客,原文链接:http://blog.51cto.com/wolfchen/1223803,如需转载请自行联系原作者