The company leader said that nginx access logs should be stored in the database and analyzed using programs. However, the time format of nginx access logs is: [17/Jun/2013: 14: 42: 13 + 0400] This format cannot be saved to the database in the datetime format. It can only be saved to the database in the string format, but it is not good to store it in the database in the process query and analysis on a daily basis, therefore, you need to change the time format of nginx access logs. After searching for information on the Internet, you can find that you can modify the source code and then compile it to change the nginx access log format. However, all programmers in the company are PHP, no one understands the C language. I only want to change the source code and compile and install it myself, although I do not understand C ).
The format to be changed:
Original format: 17/Jun/2013: 14: 42: 13 + 0400
Change to: 22:39:02
Nginx version: nginx-1.4.1
After searching for information on the Internet, we found that the following two files need to be changed: src/http/modules/ngx_http_log_module.c and src/core/ngx_times.c. 1. Modify the src/core/ngx_times.c file. The file must be modified in three places: 1. the original code in lines 49 and 50 is 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. The original code on line 64 is:
ngx_cached_http_log_time.len = sizeof ("28 / Sep / 1970: 12: 00: 00 +0600")-1;
After modification:
ngx_cached_http_log_time.len = sizeof ("1970-09-28 12:00:00")-1;
3. The original code in lines 151 to 158 is a 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 is:
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: In fact, the P2 format is basically changed to the P1 format, and the log_time format can be directly changed to p1.
Second, modify the src / http / modules / ngx_http_log_module.c file, there is a place that needs to be modified
On line 220:
{ngx_string ("time_local"), sizeof ("28 / Sep / 1970: 12: 00: 00 +0600")-1
After modification:
{ngx_string ("time_local"), sizeof ("1970-09-28 12:00:00")-1
After the modification, recompile nginx. After the compilation and installation, kill nginx with killall -9 nginx to start, and then check the access log of nginx to see if the time format has been 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 "
Seeing that the time format above is the same as what I want, it means the modification is successful.
Some contents of this blog post refer to some articles on the Internet. If you have copyright issues, please contact me. For more blog posts, please move on: linux open source technology blog or ordinary days
This article is from the "ordinary days" blog, please be sure to keep this source http://wolfchen.blog.51cto.com/2211749/1223803