Date variable

Anders Melchiorsen mail at spoon.kalibalik.dk
Tue Nov 17 00:18:54 MSK 2009


Anders Melchiorsen <mail at spoon.kalibalik.dk> writes:

> Rather than rotating logs, I name my access log files like
> access-YEAR-MONTH.log.
>
> Is this possible with nginx? I was looking a variable containing the
> current date, but did not find any.

Hello again.

I made the below quick patch and configuration like this:

        if ($timestamp ~ "^(\d\d\d\d)/(\d\d)/(\d\d) (\d\d):(\d\d):(\d\d)") {
            set $year $1;
            set $month $2;
            set $day $3;
            set $H $4;
            set $M $5;
            set $S $6;
        }

        access_log /var/log/http/$host/access-$year-$month.log;

I just glued together a few pieces that seemed about right, so I am
sure that there are all kinds of things wrong with the patch. But it
seems to mostly work, at least as an example of what I am looking for.

Is something like this possible with the stock nginx?


Cheers,
Anders.




diff -ur nginx-0.7.63-orig/src/http/ngx_http_variables.c nginx-0.7.63/src/http/ngx_http_variables.c
--- nginx-0.7.63-orig/src/http/ngx_http_variables.c	2009-05-22 13:32:17.000000000 +0200
+++ nginx-0.7.63/src/http/ngx_http_variables.c	2009-11-16 18:05:21.000000000 +0100
@@ -56,6 +56,8 @@
     ngx_http_variable_value_t *v, uintptr_t data);
 static ngx_int_t ngx_http_variable_request_method(ngx_http_request_t *r,
     ngx_http_variable_value_t *v, uintptr_t data);
+static ngx_int_t ngx_http_variable_timestamp(ngx_http_request_t *r,
+    ngx_http_variable_value_t *v, uintptr_t data);
 static ngx_int_t ngx_http_variable_remote_user(ngx_http_request_t *r,
     ngx_http_variable_value_t *v, uintptr_t data);
 static ngx_int_t ngx_http_variable_body_bytes_sent(ngx_http_request_t *r,
@@ -195,6 +197,9 @@
     { ngx_string("request_method"), NULL,
       ngx_http_variable_request_method, 0, 0, 0 },
 
+    { ngx_string("timestamp"), NULL,
+      ngx_http_variable_timestamp, 0, 0, 0 },
+
     { ngx_string("remote_user"), NULL, ngx_http_variable_remote_user, 0, 0, 0 },
 
     { ngx_string("body_bytes_sent"), NULL, ngx_http_variable_body_bytes_sent,
@@ -1208,6 +1213,26 @@
 
 
 static ngx_int_t
+ngx_http_variable_timestamp(ngx_http_request_t *r,
+    ngx_http_variable_value_t *v, uintptr_t data)
+{
+    if (ngx_cached_err_log_time.len) {
+        v->len = ngx_cached_err_log_time.len;
+
+        v->valid = 1;
+        v->no_cacheable = 0;
+        v->not_found = 0;
+        v->data = ngx_cached_err_log_time.data;
+
+    } else {
+        v->not_found = 1;
+    }
+
+    return NGX_OK;
+}
+
+
+static ngx_int_t
 ngx_http_variable_remote_user(ngx_http_request_t *r,
     ngx_http_variable_value_t *v, uintptr_t data)
 {





More information about the nginx mailing list