How to modify the Nginx source code to change the time format of the access log

Source: Internet
Author: User
Tags string format

Because the company leader said that the Nginx access log should be stored in the database, and then analyzed using the program, but the Nginx access log Time format is: [17/jun/2013:14:42:13 +0400] This format can not be stored in datetime format in the database, Can only be stored in a string format into the database, but in the format of the string into the database is not good to process query analysis by day, so you need to change the Nginx access log time format, through the Internet to find information to change the source code can be further compiled can change the format of the Nginx access log, But the company's programmers are PHP, no one understand C language, think to go only I change the source code and then compile the installation (although I do not understand C).

I need to change the format:

The original format is: 17/jun/2013:14:42:13 +0400

Need to be modified into: 2013-06-17 22:39:02

The Nginx version is: nginx-1.4.1

After searching for information on the Internet, you find that you need to change two files: src/http/modules/ngx_http_log_module.c and src/core/ngx_times.c two files.

First, modify the src/core/ngx_times.c file, this file has 3 places need to be modified, respectively: 1, in 49 lines and 50 lines of the original code as follows:

Static U_char            cached_http_log_time[ngx_time_slots]
                              [sizeof ("28/sep/1970:12:00:00 +0600")];

After modification:

Static U_char            cached_http_log_time[ngx_time_slots]
                                    [sizeof ("1970-09-28 12:00:00")];

2, in the 64 line of the original code is:

Ngx_cached_http_log_time.len = sizeof ("28/sep/1970:12:00:00 +0600")-1;

After modification:

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

More Wonderful content: http://www.bianceng.cnhttp://www.bianceng.cn/Servers/web/

3, in line 151 to 158 of the original code is (very important modification):

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));

After the modification:

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));

Note: Here in fact, the P2 format is changed to the P1 format is basically the same, you can directly change the log_time format to P1.

Second, modify the src/http/modules/ngx_http_log_module.c file, there is a place to modify

In line 220:

1{ngx_string ("time_local"), sizeof ("28/sep/1970:12:00:00 +0600")-1

After modification:

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

After the modification, recompile nginx, compile the installation after the killall-9 Nginx to kill Nginx in the start, and then view the Nginx visit log, see the time format is not changed.

[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 "

See the time format above and I want to be the same, stating that the modification was successful.

This blog has some content to refer to some articles on the Internet, if there are copyright issues, please contact me. More Posts please go: Linux open source Technology blog or ordinary day

This article from the "Ordinary Days" blog, please be sure to keep this source http://wolfchen.blog.51cto.com/2211749/1223803

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.