纯粹笔记整理,非原创


nginx有两个日志格式,分别是$time_local和$time_iso8601,均未能满足"1970-09-28 12:00:00"的格式,通过修改源码重新编译满足需求:


1、vi /root/pkg/nginx-1.10.2/src/http/modules/ngx_http_log_module.c

static ngx_http_log_var_t  ngx_http_log_vars[] = {
    ...
    { ngx_string("time_iso8601"), sizeof("1970-09-28T12:00:00+06:00") - 1,
                          ngx_http_log_iso8601 },
    ...
};

改为:

static ngx_http_log_var_t  ngx_http_log_vars[] = {
    ...
    { ngx_string("time_iso8601"), sizeof("1970-09-28 12:00:00") - 1,
                          ngx_http_log_iso8601 },
    ...
};


2、vi /root/pkg/nginx-1.10.2/src/core/

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%02i:%02i",
                       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));

改为:

    p3 = &cached_http_log_iso8601[slot][0];
    (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,
                       tp->gmtoff < 0 ? '-' : '+',
                       ngx_abs(tp->gmtoff / 60), ngx_abs(tp->gmtoff % 60));


3、修改完后重新编译

./configure --user=nginx --group=nginx --prefix=/home/nginx/nginx --with-http_ssl_module --with-http_realip_module --with-http_sub_module --with-http_gzip_static_module --with-http_stub_status_module

这里只需要make,不用make install

mv /home/nginx/nginx/sbin/nginx /home/nginx/nginx/sbin/nginx.bak
cp objs/nginx /home/nginx/nginx/sbin/nginx
chown -R nginx: /home/nginx/nginx/sbin/
[nginx@web-nginx1 sbin]$ cd /home/nginx/nginx/sbin/ && ./nginx -t
nginx: the configuration file /home/nginx/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /home/nginx/nginx/conf/nginx.conf test is successful
kill -USR2 `cat nginx.pid` 
kill -WINCH `cat nginx.pid.oldbin`
timestamp=2017-01-04T04:04:35+08:00`remote_addr=192.168.56.1`remote_user=-`request=GET /favicon.ico HTTP/1.1`request_method=GET`status=404`upstream_addr=-`upstream_response_time=-`request_time=0.000`body_bytes_sent=571`http_referrer=http://192.168.56.102:8080/`http_user_agent=Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36`http_x_forwarded_for=-
timestamp=2017-01-04 04:05:15`remote_addr=192.168.56.1`remote_user=-`request=GET / HTTP/1.1`request_method=GET`status=403`upstream_addr=-`upstream_response_time=-`request_time=0.000`body_bytes_sent=571`http_referrer=-`http_user_agent=Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36`http_x_forwarded_for=-