修改nginx源代码改变访问日志的时间格式

简介:

 由于公司领导说要把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

经过网上查找资料,发现需要更改两个文件:src/http/modules/ngx_http_log_module.c 和src/core/ngx_times.c 这两个文件。
一、修改src/core/ngx_times.c 文件,这个文件有3个地方需要修改,分别是:
1、在49行和50行原代码如下:
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" )];
2、在64行的原代码为:
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;
3、在151行到158行的原代码是(很重要的修改):
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));
注:这里其实是把P2格式改成P1格式基本相同了,完全可以直接将log_time的格式改成p1。

二、修改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,如需转载请自行联系原作者

相关实践学习
日志服务之使用Nginx模式采集日志
本文介绍如何通过日志服务控制台创建Nginx模式的Logtail配置快速采集Nginx日志并进行多维度分析。
相关文章
|
30天前
|
存储 JSON 应用服务中间件
Higress的日志收集中,底层用的是Envoy,可不可以实现类似NGINX的rsyslog发送?
【2月更文挑战第30天】Higress的日志收集中,底层用的是Envoy,可不可以实现类似NGINX的rsyslog发送?
16 2
|
11天前
|
网络协议 应用服务中间件 Linux
centos7 Nginx Log日志统计分析 常用命令
centos7 Nginx Log日志统计分析 常用命令
23 2
|
12天前
|
运维 监控 应用服务中间件
LNMP详解(十四)——Nginx日志详解
LNMP详解(十四)——Nginx日志详解
16 2
|
13天前
|
应用服务中间件 网络安全 nginx
nginx配置https访问
nginx配置https访问
25 0
|
19天前
|
存储 监控 数据可视化
Nginx+Promtail+Loki+Grafana Nginx日志展示
通过这些步骤,你可以将Nginx的日志收集、存储、查询和可视化整合在一起。这样,你就可以在Grafana中轻松地创建和展示Nginx日志的图表和面板。
27 3
|
22天前
|
应用服务中间件 nginx
nginx配置访问qicaitun.com强制跳转www.qicaitun.com
nginx配置访问qicaitun.com强制跳转www.qicaitun.com
9 0
|
23天前
|
应用服务中间件 nginx
nginx配置https和直接访问静态文件的方式
nginx配置https和直接访问静态文件的方式
27 3
|
30天前
|
前端开发 应用服务中间件 nginx
nginx中配置不输入端口(指定地址)访问项目的方法
nginx中配置不输入端口(指定地址)访问项目的方法
23 0
|
18天前
|
Java
使用Java代码打印log日志
使用Java代码打印log日志
73 1
|
19天前
|
Linux Shell
Linux手动清理Linux脚本日志定时清理日志和log文件执行表达式
Linux手动清理Linux脚本日志定时清理日志和log文件执行表达式
72 1