nginx 修改 time_local 时间格式

本文介绍如何将Nginx的日志时间格式从默认的time_local格式修改为更常用的datetime格式。通过修改Nginx源代码并重新编译安装,最终实现日志格式的变更。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

因为系统统计需要,需要将默认的time_local格式 23/Aug/2010:17:26:44 +0800 修改为datetime格式,参考网上的方法,发现也有不正确。自己稍作修改:

保险起见,使用time_iso8601变量

  1. cd到源码目录:

    vi src/http/modules/ngx_http_log_module.c 

        找到以下内容:

       { ngx_string("time_iso8601"), sizeof("1970-09-28T12:00:00+06:00") - 1,

        修改为:

       { ngx_string("time_iso8601"), sizeof("1970-09-28 12:00:00") - 1,

        

 2.    vi src/core/ngx_times.c

        找到:

        static u_char  cached_http_log_iso8601[NGX_TIME_SLOTS]  

                                [sizeof("1970-09-28T12:00:00+06:00")];

        修改为:

        static u_char  cached_http_log_iso8601[NGX_TIME_SLOTS]  

                                [sizeof("1970-09-28 12:00:00")];

        找到:

        ngx_cached_http_log_iso8601.len = sizeof("1970-09-28T12:00:00+06:00") - 1;  

        修改为:

        ngx_cached_http_log_iso8601.len = sizeof("1970-09-28 12:00:00") - 1; 

        找到:

        p3 = &cached_http_log_iso8601[slot][0];  

        (void) ngx_sprintf(p3, "%4d-%02d-%02dT%02d:%02d:%02d%c%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));

        修改为:

         

        (void) ngx_sprintf(p3, "%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);


3.     重新编译:

        ./configure --prefix=/usr/local/nginx  --user=web --group=web --with-http_stub_status_module --with-openssl=/usr/ --with-pcre=/opt/sourcecode/pcre-8.32 --add-module=./nginx-http-concat

        make

        不要make install

        然后停止nginx,将新编译好的nginx复制到原目录下即可

         cp /objs/nginx /usr/local/nginx/sbin/

4.    修改conf文件

        将

        log_format  main  '$remote_addr - $remote_user $time_local "$request" '  

                  '$status $body_bytes_sent "$http_referer" '  

                  '"$http_user_agent" "$http_x_forwarded_for"';  

        修改为:

        log_format  main  '$remote_addr - $remote_user $time_iso8601 "$request" '  

                  '$status $body_bytes_sent "$http_referer" '  

                  '"$http_user_agent" "$http_x_forwarded_for"';  


        /usr/local/nginx/sbin/nginx -t

        没有问题的话就可以启动了

看下log:192.168.0.143 - - [2014-11-12 10:36:26] "GET...

OK!


转载于:https://my.oschina.net/comguo/blog/343417

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值