118
|
1
|
|
2 /*
|
|
3 * Copyright (C) Igor Sysoev
|
|
4 */
|
|
5
|
|
6
|
|
7 #include <ngx_config.h>
|
|
8 #include <ngx_core.h>
|
|
9 #include <ngx_http.h>
|
|
10 #include <nginx.h>
|
|
11
|
|
12
|
260
|
13 typedef struct ngx_http_log_op_s ngx_http_log_op_t;
|
|
14
|
|
15 typedef u_char *(*ngx_http_log_op_run_pt) (ngx_http_request_t *r, u_char *buf,
|
|
16 ngx_http_log_op_t *op);
|
|
17
|
|
18 typedef size_t (*ngx_http_log_op_getlen_pt) (ngx_http_request_t *r,
|
|
19 uintptr_t data);
|
|
20
|
|
21
|
|
22 struct ngx_http_log_op_s {
|
|
23 size_t len;
|
|
24 ngx_http_log_op_getlen_pt getlen;
|
|
25 ngx_http_log_op_run_pt run;
|
|
26 uintptr_t data;
|
|
27 };
|
|
28
|
122
|
29
|
|
30 typedef struct {
|
|
31 ngx_str_t name;
|
|
32 ngx_array_t *ops; /* array of ngx_http_log_op_t */
|
|
33 } ngx_http_log_fmt_t;
|
|
34
|
206
|
35
|
122
|
36 typedef struct {
|
|
37 ngx_array_t formats; /* array of ngx_http_log_fmt_t */
|
|
38 ngx_uint_t combined_used; /* unsigned combined_used:1 */
|
|
39 } ngx_http_log_main_conf_t;
|
|
40
|
206
|
41
|
122
|
42 typedef struct {
|
384
|
43 ngx_array_t *lengths;
|
|
44 ngx_array_t *values;
|
|
45 } ngx_http_log_script_t;
|
|
46
|
|
47
|
|
48 typedef struct {
|
122
|
49 ngx_open_file_t *file;
|
384
|
50 ngx_http_log_script_t *script;
|
182
|
51 time_t disk_full_time;
|
278
|
52 time_t error_log_time;
|
122
|
53 ngx_array_t *ops; /* array of ngx_http_log_op_t */
|
|
54 } ngx_http_log_t;
|
|
55
|
206
|
56
|
122
|
57 typedef struct {
|
|
58 ngx_array_t *logs; /* array of ngx_http_log_t */
|
384
|
59
|
|
60 ngx_open_file_cache_t *open_file_cache;
|
|
61 time_t open_file_cache_valid;
|
|
62 ngx_uint_t open_file_cache_min_uses;
|
|
63
|
122
|
64 ngx_uint_t off; /* unsigned off:1 */
|
|
65 } ngx_http_log_loc_conf_t;
|
|
66
|
|
67
|
|
68 typedef struct {
|
|
69 ngx_str_t name;
|
|
70 size_t len;
|
|
71 ngx_http_log_op_run_pt run;
|
|
72 } ngx_http_log_var_t;
|
|
73
|
|
74
|
278
|
75 static void ngx_http_log_write(ngx_http_request_t *r, ngx_http_log_t *log,
|
|
76 u_char *buf, size_t len);
|
384
|
77 static ssize_t ngx_http_log_script_write(ngx_http_request_t *r,
|
|
78 ngx_http_log_script_t *script, u_char **name, u_char *buf, size_t len);
|
278
|
79
|
118
|
80 static u_char *ngx_http_log_connection(ngx_http_request_t *r, u_char *buf,
|
|
81 ngx_http_log_op_t *op);
|
|
82 static u_char *ngx_http_log_pipe(ngx_http_request_t *r, u_char *buf,
|
|
83 ngx_http_log_op_t *op);
|
|
84 static u_char *ngx_http_log_time(ngx_http_request_t *r, u_char *buf,
|
|
85 ngx_http_log_op_t *op);
|
|
86 static u_char *ngx_http_log_msec(ngx_http_request_t *r, u_char *buf,
|
|
87 ngx_http_log_op_t *op);
|
|
88 static u_char *ngx_http_log_request_time(ngx_http_request_t *r, u_char *buf,
|
|
89 ngx_http_log_op_t *op);
|
|
90 static u_char *ngx_http_log_status(ngx_http_request_t *r, u_char *buf,
|
|
91 ngx_http_log_op_t *op);
|
122
|
92 static u_char *ngx_http_log_bytes_sent(ngx_http_request_t *r, u_char *buf,
|
118
|
93 ngx_http_log_op_t *op);
|
126
|
94 static u_char *ngx_http_log_body_bytes_sent(ngx_http_request_t *r,
|
122
|
95 u_char *buf, ngx_http_log_op_t *op);
|
118
|
96 static u_char *ngx_http_log_request_length(ngx_http_request_t *r, u_char *buf,
|
|
97 ngx_http_log_op_t *op);
|
|
98
|
|
99 static ngx_int_t ngx_http_log_variable_compile(ngx_conf_t *cf,
|
|
100 ngx_http_log_op_t *op, ngx_str_t *value);
|
|
101 static size_t ngx_http_log_variable_getlen(ngx_http_request_t *r,
|
|
102 uintptr_t data);
|
|
103 static u_char *ngx_http_log_variable(ngx_http_request_t *r, u_char *buf,
|
|
104 ngx_http_log_op_t *op);
|
376
|
105 static uintptr_t ngx_http_log_escape(u_char *dst, u_char *src, size_t size);
|
118
|
106
|
|
107
|
|
108 static void *ngx_http_log_create_main_conf(ngx_conf_t *cf);
|
|
109 static void *ngx_http_log_create_loc_conf(ngx_conf_t *cf);
|
|
110 static char *ngx_http_log_merge_loc_conf(ngx_conf_t *cf, void *parent,
|
|
111 void *child);
|
|
112 static char *ngx_http_log_set_log(ngx_conf_t *cf, ngx_command_t *cmd,
|
|
113 void *conf);
|
|
114 static char *ngx_http_log_set_format(ngx_conf_t *cf, ngx_command_t *cmd,
|
|
115 void *conf);
|
122
|
116 static char *ngx_http_log_compile_format(ngx_conf_t *cf,
|
|
117 ngx_array_t *ops, ngx_array_t *args, ngx_uint_t s);
|
384
|
118 static char *ngx_http_log_open_file_cache(ngx_conf_t *cf, ngx_command_t *cmd,
|
|
119 void *conf);
|
122
|
120 static ngx_int_t ngx_http_log_init(ngx_conf_t *cf);
|
118
|
121
|
|
122
|
|
123 static ngx_command_t ngx_http_log_commands[] = {
|
|
124
|
|
125 { ngx_string("log_format"),
|
|
126 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_2MORE,
|
|
127 ngx_http_log_set_format,
|
|
128 NGX_HTTP_MAIN_CONF_OFFSET,
|
|
129 0,
|
|
130 NULL },
|
|
131
|
|
132 { ngx_string("access_log"),
|
194
|
133 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF
|
340
|
134 |NGX_HTTP_LMT_CONF|NGX_CONF_TAKE123,
|
118
|
135 ngx_http_log_set_log,
|
|
136 NGX_HTTP_LOC_CONF_OFFSET,
|
|
137 0,
|
|
138 NULL },
|
|
139
|
384
|
140 { ngx_string("open_log_file_cache"),
|
|
141 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1234,
|
|
142 ngx_http_log_open_file_cache,
|
|
143 NGX_HTTP_LOC_CONF_OFFSET,
|
|
144 0,
|
|
145 NULL },
|
|
146
|
118
|
147 ngx_null_command
|
|
148 };
|
|
149
|
|
150
|
216
|
151 static ngx_http_module_t ngx_http_log_module_ctx = {
|
260
|
152 NULL, /* preconfiguration */
|
122
|
153 ngx_http_log_init, /* postconfiguration */
|
118
|
154
|
|
155 ngx_http_log_create_main_conf, /* create main configuration */
|
|
156 NULL, /* init main configuration */
|
|
157
|
|
158 NULL, /* create server configuration */
|
|
159 NULL, /* merge server configuration */
|
|
160
|
|
161 ngx_http_log_create_loc_conf, /* create location configration */
|
|
162 ngx_http_log_merge_loc_conf /* merge location configration */
|
|
163 };
|
|
164
|
|
165
|
|
166 ngx_module_t ngx_http_log_module = {
|
|
167 NGX_MODULE_V1,
|
|
168 &ngx_http_log_module_ctx, /* module context */
|
|
169 ngx_http_log_commands, /* module directives */
|
|
170 NGX_HTTP_MODULE, /* module type */
|
|
171 NULL, /* init master */
|
122
|
172 NULL, /* init module */
|
118
|
173 NULL, /* init process */
|
|
174 NULL, /* init thread */
|
|
175 NULL, /* exit thread */
|
|
176 NULL, /* exit process */
|
|
177 NULL, /* exit master */
|
|
178 NGX_MODULE_V1_PADDING
|
|
179 };
|
|
180
|
|
181
|
278
|
182 static ngx_str_t ngx_http_access_log = ngx_string(NGX_HTTP_LOG_PATH);
|
122
|
183
|
|
184
|
|
185 static ngx_str_t ngx_http_combined_fmt =
|
130
|
186 ngx_string("$remote_addr - $remote_user [$time_local] "
|
126
|
187 "\"$request\" $status $body_bytes_sent "
|
122
|
188 "\"$http_referer\" \"$http_user_agent\"");
|
118
|
189
|
|
190
|
122
|
191 static ngx_http_log_var_t ngx_http_log_vars[] = {
|
|
192 { ngx_string("connection"), NGX_ATOMIC_T_LEN, ngx_http_log_connection },
|
|
193 { ngx_string("pipe"), 1, ngx_http_log_pipe },
|
130
|
194 { ngx_string("time_local"), sizeof("28/Sep/1970:12:00:00 +0600") - 1,
|
122
|
195 ngx_http_log_time },
|
|
196 { ngx_string("msec"), NGX_TIME_T_LEN + 4, ngx_http_log_msec },
|
298
|
197 { ngx_string("request_time"), NGX_TIME_T_LEN + 4,
|
|
198 ngx_http_log_request_time },
|
122
|
199 { ngx_string("status"), 3, ngx_http_log_status },
|
|
200 { ngx_string("bytes_sent"), NGX_OFF_T_LEN, ngx_http_log_bytes_sent },
|
126
|
201 { ngx_string("body_bytes_sent"), NGX_OFF_T_LEN,
|
|
202 ngx_http_log_body_bytes_sent },
|
122
|
203 { ngx_string("apache_bytes_sent"), NGX_OFF_T_LEN,
|
126
|
204 ngx_http_log_body_bytes_sent },
|
122
|
205 { ngx_string("request_length"), NGX_SIZE_T_LEN,
|
|
206 ngx_http_log_request_length },
|
|
207
|
|
208 { ngx_null_string, 0, NULL }
|
|
209 };
|
118
|
210
|
|
211
|
|
212 ngx_int_t
|
|
213 ngx_http_log_handler(ngx_http_request_t *r)
|
|
214 {
|
|
215 u_char *line, *p;
|
|
216 size_t len;
|
278
|
217 ngx_uint_t i, l;
|
118
|
218 ngx_http_log_t *log;
|
120
|
219 ngx_open_file_t *file;
|
118
|
220 ngx_http_log_op_t *op;
|
|
221 ngx_http_log_loc_conf_t *lcf;
|
|
222
|
|
223 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
224 "http log handler");
|
|
225
|
|
226 lcf = ngx_http_get_module_loc_conf(r, ngx_http_log_module);
|
|
227
|
|
228 if (lcf->off) {
|
|
229 return NGX_OK;
|
|
230 }
|
|
231
|
|
232 log = lcf->logs->elts;
|
|
233 for (l = 0; l < lcf->logs->nelts; l++) {
|
|
234
|
182
|
235 if (ngx_time() == log[l].disk_full_time) {
|
|
236
|
|
237 /*
|
278
|
238 * on FreeBSD writing to a full filesystem with enabled softupdates
|
182
|
239 * may block process for much longer time than writing to non-full
|
278
|
240 * filesystem, so we skip writing to a log for one second
|
182
|
241 */
|
|
242
|
|
243 continue;
|
|
244 }
|
|
245
|
118
|
246 len = 0;
|
|
247 op = log[l].ops->elts;
|
|
248 for (i = 0; i < log[l].ops->nelts; i++) {
|
|
249 if (op[i].len == 0) {
|
|
250 len += op[i].getlen(r, op[i].data);
|
|
251
|
|
252 } else {
|
|
253 len += op[i].len;
|
|
254 }
|
|
255 }
|
|
256
|
120
|
257 len += NGX_LINEFEED_SIZE;
|
|
258
|
|
259 file = log[l].file;
|
|
260
|
384
|
261 if (file && file->buffer) {
|
120
|
262
|
|
263 if (len > (size_t) (file->last - file->pos)) {
|
|
264
|
278
|
265 ngx_http_log_write(r, &log[l], file->buffer,
|
|
266 file->pos - file->buffer);
|
120
|
267
|
|
268 file->pos = file->buffer;
|
|
269 }
|
|
270
|
|
271 if (len <= (size_t) (file->last - file->pos)) {
|
|
272
|
|
273 p = file->pos;
|
|
274
|
|
275 for (i = 0; i < log[l].ops->nelts; i++) {
|
|
276 p = op[i].run(r, p, &op[i]);
|
|
277 }
|
|
278
|
|
279 ngx_linefeed(p);
|
|
280
|
|
281 file->pos = p;
|
|
282
|
|
283 continue;
|
|
284 }
|
|
285 }
|
118
|
286
|
382
|
287 line = ngx_pnalloc(r->pool, len);
|
118
|
288 if (line == NULL) {
|
|
289 return NGX_ERROR;
|
|
290 }
|
|
291
|
|
292 p = line;
|
|
293
|
|
294 for (i = 0; i < log[l].ops->nelts; i++) {
|
|
295 p = op[i].run(r, p, &op[i]);
|
|
296 }
|
|
297
|
120
|
298 ngx_linefeed(p);
|
|
299
|
278
|
300 ngx_http_log_write(r, &log[l], line, p - line);
|
118
|
301 }
|
|
302
|
|
303 return NGX_OK;
|
|
304 }
|
|
305
|
|
306
|
278
|
307 static void
|
|
308 ngx_http_log_write(ngx_http_request_t *r, ngx_http_log_t *log, u_char *buf,
|
|
309 size_t len)
|
|
310 {
|
384
|
311 u_char *name;
|
|
312 time_t now;
|
|
313 ssize_t n;
|
|
314 ngx_err_t err;
|
278
|
315
|
384
|
316 if (log->script == NULL) {
|
|
317 name = log->file->name.data;
|
|
318 n = ngx_write_fd(log->file->fd, buf, len);
|
|
319
|
|
320 } else {
|
|
321 name = NULL;
|
|
322 n = ngx_http_log_script_write(r, log->script, &name, buf, len);
|
|
323 }
|
278
|
324
|
|
325 if (n == (ssize_t) len) {
|
|
326 return;
|
|
327 }
|
|
328
|
|
329 now = ngx_time();
|
|
330
|
|
331 if (n == -1) {
|
|
332 err = ngx_errno;
|
|
333
|
|
334 if (err == NGX_ENOSPC) {
|
|
335 log->disk_full_time = now;
|
|
336 }
|
|
337
|
296
|
338 if (now - log->error_log_time > 59) {
|
278
|
339 ngx_log_error(NGX_LOG_ALERT, r->connection->log, err,
|
384
|
340 ngx_write_fd_n " to \"%s\" failed", name);
|
278
|
341
|
|
342 log->error_log_time = now;
|
|
343 }
|
|
344
|
|
345 return;
|
|
346 }
|
|
347
|
296
|
348 if (now - log->error_log_time > 59) {
|
278
|
349 ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0,
|
384
|
350 ngx_write_fd_n " to \"%s\" was incomplete: %z of %uz",
|
|
351 name, n, len);
|
278
|
352
|
|
353 log->error_log_time = now;
|
|
354 }
|
|
355 }
|
|
356
|
|
357
|
384
|
358 static ssize_t
|
|
359 ngx_http_log_script_write(ngx_http_request_t *r, ngx_http_log_script_t *script,
|
|
360 u_char **name, u_char *buf, size_t len)
|
|
361 {
|
|
362 size_t root;
|
|
363 ssize_t n;
|
|
364 ngx_str_t log, path;
|
|
365 ngx_open_file_info_t of;
|
|
366 ngx_http_log_loc_conf_t *llcf;
|
|
367 ngx_http_core_loc_conf_t *clcf;
|
|
368
|
|
369 if (r->err_status == NGX_HTTP_NOT_FOUND) {
|
|
370
|
|
371 /* test root directory existance */
|
|
372
|
|
373 if (ngx_http_map_uri_to_path(r, &path, &root, 0) == NULL) {
|
|
374 /* simulate successfull logging */
|
|
375 return len;
|
|
376 }
|
|
377
|
|
378 path.data[root] = '\0';
|
|
379
|
|
380 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
|
|
381
|
|
382 ngx_memzero(&of, sizeof(ngx_open_file_info_t));
|
|
383
|
|
384 of.valid = clcf->open_file_cache_valid;
|
|
385 of.min_uses = clcf->open_file_cache_min_uses;
|
|
386 of.errors = clcf->open_file_cache_errors;
|
|
387 of.events = clcf->open_file_cache_events;
|
|
388
|
|
389 if (ngx_open_cached_file(clcf->open_file_cache, &path, &of, r->pool)
|
|
390 != NGX_OK
|
|
391 || !of.is_dir)
|
|
392 {
|
|
393 /* no root directory: simulate successfull logging */
|
|
394 return len;
|
|
395 }
|
|
396 }
|
|
397
|
|
398 if (ngx_http_script_run(r, &log, script->lengths->elts, 1,
|
|
399 script->values->elts)
|
|
400 == NULL)
|
|
401 {
|
|
402 /* simulate successfull logging */
|
|
403 return len;
|
|
404 }
|
|
405
|
|
406 log.data[log.len - 1] = '\0';
|
|
407 *name = log.data;
|
|
408
|
|
409 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
410 "http log \"%s\"", log.data);
|
|
411
|
|
412 llcf = ngx_http_get_module_loc_conf(r, ngx_http_log_module);
|
|
413
|
|
414 ngx_memzero(&of, sizeof(ngx_open_file_info_t));
|
|
415
|
|
416 of.log = 1;
|
|
417 of.valid = llcf->open_file_cache_valid;
|
|
418 of.min_uses = llcf->open_file_cache_min_uses;
|
|
419
|
|
420 if (ngx_open_cached_file(llcf->open_file_cache, &log, &of, r->pool)
|
|
421 != NGX_OK)
|
|
422 {
|
|
423 ngx_log_error(NGX_LOG_CRIT, r->connection->log, ngx_errno,
|
|
424 ngx_open_file_n " \"%s\" failed", log.data);
|
386
|
425 /* simulate successfull logging */
|
|
426 return len;
|
384
|
427 }
|
|
428
|
|
429 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
430 "http log #%d", of.fd);
|
|
431
|
|
432 n = ngx_write_fd(of.fd, buf, len);
|
|
433
|
|
434 return n;
|
|
435 }
|
|
436
|
|
437
|
118
|
438 static u_char *
|
|
439 ngx_http_log_copy_short(ngx_http_request_t *r, u_char *buf,
|
|
440 ngx_http_log_op_t *op)
|
|
441 {
|
|
442 size_t len;
|
|
443 uintptr_t data;
|
|
444
|
|
445 len = op->len;
|
|
446 data = op->data;
|
|
447
|
|
448 while (len--) {
|
|
449 *buf++ = (u_char) (data & 0xff);
|
|
450 data >>= 8;
|
|
451 }
|
|
452
|
|
453 return buf;
|
|
454 }
|
|
455
|
|
456
|
|
457 static u_char *
|
|
458 ngx_http_log_copy_long(ngx_http_request_t *r, u_char *buf,
|
|
459 ngx_http_log_op_t *op)
|
|
460 {
|
|
461 return ngx_cpymem(buf, (u_char *) op->data, op->len);
|
|
462 }
|
|
463
|
|
464
|
|
465 static u_char *
|
|
466 ngx_http_log_connection(ngx_http_request_t *r, u_char *buf,
|
|
467 ngx_http_log_op_t *op)
|
|
468 {
|
|
469 return ngx_sprintf(buf, "%ui", r->connection->number);
|
|
470 }
|
|
471
|
|
472
|
|
473 static u_char *
|
|
474 ngx_http_log_pipe(ngx_http_request_t *r, u_char *buf, ngx_http_log_op_t *op)
|
|
475 {
|
|
476 if (r->pipeline) {
|
|
477 *buf = 'p';
|
|
478 } else {
|
|
479 *buf = '.';
|
|
480 }
|
|
481
|
|
482 return buf + 1;
|
|
483 }
|
|
484
|
|
485
|
|
486 static u_char *
|
|
487 ngx_http_log_time(ngx_http_request_t *r, u_char *buf, ngx_http_log_op_t *op)
|
|
488 {
|
|
489 return ngx_cpymem(buf, ngx_cached_http_log_time.data,
|
|
490 ngx_cached_http_log_time.len);
|
|
491 }
|
|
492
|
|
493
|
|
494 static u_char *
|
|
495 ngx_http_log_msec(ngx_http_request_t *r, u_char *buf, ngx_http_log_op_t *op)
|
|
496 {
|
|
497 ngx_time_t *tp;
|
|
498
|
|
499 tp = ngx_timeofday();
|
|
500
|
|
501 return ngx_sprintf(buf, "%T.%03M", tp->sec, tp->msec);
|
|
502 }
|
|
503
|
|
504
|
|
505 static u_char *
|
|
506 ngx_http_log_request_time(ngx_http_request_t *r, u_char *buf,
|
|
507 ngx_http_log_op_t *op)
|
|
508 {
|
298
|
509 ngx_time_t *tp;
|
|
510 ngx_msec_int_t ms;
|
|
511
|
|
512 tp = ngx_timeofday();
|
118
|
513
|
346
|
514 ms = (ngx_msec_int_t)
|
|
515 ((tp->sec - r->start_sec) * 1000 + (tp->msec - r->start_msec));
|
298
|
516 ms = (ms >= 0) ? ms : 0;
|
118
|
517
|
298
|
518 return ngx_sprintf(buf, "%T.%03M", ms / 1000, ms % 1000);
|
118
|
519 }
|
|
520
|
|
521
|
|
522 static u_char *
|
|
523 ngx_http_log_status(ngx_http_request_t *r, u_char *buf, ngx_http_log_op_t *op)
|
|
524 {
|
|
525 return ngx_sprintf(buf, "%ui",
|
|
526 r->err_status ? r->err_status : r->headers_out.status);
|
|
527 }
|
|
528
|
|
529
|
|
530 static u_char *
|
122
|
531 ngx_http_log_bytes_sent(ngx_http_request_t *r, u_char *buf,
|
|
532 ngx_http_log_op_t *op)
|
118
|
533 {
|
|
534 return ngx_sprintf(buf, "%O", r->connection->sent);
|
|
535 }
|
|
536
|
|
537
|
316
|
538 /*
|
|
539 * although there is a real $body_bytes_sent variable,
|
|
540 * this log operation code function is more optimized for logging
|
|
541 */
|
|
542
|
118
|
543 static u_char *
|
126
|
544 ngx_http_log_body_bytes_sent(ngx_http_request_t *r, u_char *buf,
|
118
|
545 ngx_http_log_op_t *op)
|
|
546 {
|
|
547 off_t length;
|
|
548
|
|
549 length = r->connection->sent - r->header_size;
|
|
550
|
|
551 if (length > 0) {
|
|
552 return ngx_sprintf(buf, "%O", length);
|
|
553 }
|
|
554
|
|
555 *buf = '0';
|
|
556
|
|
557 return buf + 1;
|
|
558 }
|
|
559
|
|
560
|
|
561 static u_char *
|
|
562 ngx_http_log_request_length(ngx_http_request_t *r, u_char *buf,
|
|
563 ngx_http_log_op_t *op)
|
|
564 {
|
212
|
565 return ngx_sprintf(buf, "%O", r->request_length);
|
118
|
566 }
|
|
567
|
|
568
|
|
569 static ngx_int_t
|
|
570 ngx_http_log_variable_compile(ngx_conf_t *cf, ngx_http_log_op_t *op,
|
|
571 ngx_str_t *value)
|
|
572 {
|
|
573 ngx_int_t index;
|
|
574
|
|
575 index = ngx_http_get_variable_index(cf, value);
|
|
576 if (index == NGX_ERROR) {
|
|
577 return NGX_ERROR;
|
|
578 }
|
|
579
|
|
580 op->len = 0;
|
|
581 op->getlen = ngx_http_log_variable_getlen;
|
|
582 op->run = ngx_http_log_variable;
|
|
583 op->data = index;
|
|
584
|
|
585 return NGX_OK;
|
|
586 }
|
|
587
|
|
588
|
|
589 static size_t
|
|
590 ngx_http_log_variable_getlen(ngx_http_request_t *r, uintptr_t data)
|
|
591 {
|
376
|
592 uintptr_t len;
|
118
|
593 ngx_http_variable_value_t *value;
|
|
594
|
|
595 value = ngx_http_get_indexed_variable(r, data);
|
|
596
|
122
|
597 if (value == NULL || value->not_found) {
|
118
|
598 return 1;
|
|
599 }
|
|
600
|
376
|
601 len = ngx_http_log_escape(NULL, value->data, value->len);
|
|
602
|
|
603 value->escape = len ? 1 : 0;
|
|
604
|
|
605 return value->len + len * 3;
|
118
|
606 }
|
|
607
|
|
608
|
|
609 static u_char *
|
|
610 ngx_http_log_variable(ngx_http_request_t *r, u_char *buf, ngx_http_log_op_t *op)
|
|
611 {
|
|
612 ngx_http_variable_value_t *value;
|
|
613
|
|
614 value = ngx_http_get_indexed_variable(r, op->data);
|
|
615
|
122
|
616 if (value == NULL || value->not_found) {
|
118
|
617 *buf = '-';
|
|
618 return buf + 1;
|
|
619 }
|
|
620
|
376
|
621 if (value->escape == 0) {
|
|
622 return ngx_cpymem(buf, value->data, value->len);
|
|
623
|
|
624 } else {
|
|
625 return (u_char *) ngx_http_log_escape(buf, value->data, value->len);
|
|
626 }
|
|
627 }
|
|
628
|
|
629
|
|
630 static uintptr_t
|
|
631 ngx_http_log_escape(u_char *dst, u_char *src, size_t size)
|
|
632 {
|
|
633 ngx_uint_t i, n;
|
|
634 static u_char hex[] = "0123456789ABCDEF";
|
|
635
|
|
636 static uint32_t escape[] = {
|
|
637 0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */
|
|
638
|
|
639 /* ?>=< ;:98 7654 3210 /.-, +*)( '&%$ #"! */
|
|
640 0x00000004, /* 0000 0000 0000 0000 0000 0000 0000 0100 */
|
|
641
|
|
642 /* _^]\ [ZYX WVUT SRQP ONML KJIH GFED CBA@ */
|
|
643 0x10000000, /* 0001 0000 0000 0000 0000 0000 0000 0000 */
|
|
644
|
|
645 /* ~}| {zyx wvut srqp onml kjih gfed cba` */
|
|
646 0x00000000, /* 0000 0000 0000 0000 0000 0000 0000 0000 */
|
|
647
|
|
648 0x00000000, /* 0000 0000 0000 0000 0000 0000 0000 0000 */
|
|
649 0x00000000, /* 0000 0000 0000 0000 0000 0000 0000 0000 */
|
|
650 0x00000000, /* 0000 0000 0000 0000 0000 0000 0000 0000 */
|
|
651 0x00000000, /* 0000 0000 0000 0000 0000 0000 0000 0000 */
|
|
652 };
|
|
653
|
|
654
|
|
655 if (dst == NULL) {
|
|
656
|
|
657 /* find the number of the characters to be escaped */
|
|
658
|
|
659 n = 0;
|
|
660
|
|
661 for (i = 0; i < size; i++) {
|
|
662 if (escape[*src >> 5] & (1 << (*src & 0x1f))) {
|
|
663 n++;
|
|
664 }
|
|
665 src++;
|
|
666 }
|
|
667
|
|
668 return (uintptr_t) n;
|
|
669 }
|
|
670
|
|
671 for (i = 0; i < size; i++) {
|
|
672 if (escape[*src >> 5] & (1 << (*src & 0x1f))) {
|
|
673 *dst++ = '\\';
|
|
674 *dst++ = 'x';
|
|
675 *dst++ = hex[*src >> 4];
|
|
676 *dst++ = hex[*src & 0xf];
|
|
677 src++;
|
|
678
|
|
679 } else {
|
|
680 *dst++ = *src++;
|
|
681 }
|
|
682 }
|
|
683
|
|
684 return (uintptr_t) dst;
|
118
|
685 }
|
|
686
|
|
687
|
|
688 static void *
|
|
689 ngx_http_log_create_main_conf(ngx_conf_t *cf)
|
|
690 {
|
|
691 ngx_http_log_main_conf_t *conf;
|
|
692
|
122
|
693 ngx_http_log_fmt_t *fmt;
|
118
|
694
|
|
695 conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_log_main_conf_t));
|
|
696 if (conf == NULL) {
|
|
697 return NGX_CONF_ERROR;
|
|
698 }
|
|
699
|
|
700 if (ngx_array_init(&conf->formats, cf->pool, 4, sizeof(ngx_http_log_fmt_t))
|
122
|
701 != NGX_OK)
|
118
|
702 {
|
|
703 return NGX_CONF_ERROR;
|
|
704 }
|
|
705
|
122
|
706 fmt = ngx_array_push(&conf->formats);
|
|
707 if (fmt == NULL) {
|
118
|
708 return NGX_CONF_ERROR;
|
|
709 }
|
|
710
|
122
|
711 fmt->name.len = sizeof("combined") - 1;
|
|
712 fmt->name.data = (u_char *) "combined";
|
118
|
713
|
122
|
714 fmt->ops = ngx_array_create(cf->pool, 16, sizeof(ngx_http_log_op_t));
|
|
715 if (fmt->ops == NULL) {
|
118
|
716 return NGX_CONF_ERROR;
|
|
717 }
|
|
718
|
|
719 return conf;
|
|
720 }
|
|
721
|
|
722
|
|
723 static void *
|
|
724 ngx_http_log_create_loc_conf(ngx_conf_t *cf)
|
|
725 {
|
|
726 ngx_http_log_loc_conf_t *conf;
|
|
727
|
|
728 conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_log_loc_conf_t));
|
|
729 if (conf == NULL) {
|
|
730 return NGX_CONF_ERROR;
|
|
731 }
|
|
732
|
384
|
733 conf->open_file_cache = NGX_CONF_UNSET_PTR;
|
|
734
|
118
|
735 return conf;
|
|
736 }
|
|
737
|
|
738
|
|
739 static char *
|
|
740 ngx_http_log_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
|
|
741 {
|
|
742 ngx_http_log_loc_conf_t *prev = parent;
|
|
743 ngx_http_log_loc_conf_t *conf = child;
|
|
744
|
|
745 ngx_http_log_t *log;
|
|
746 ngx_http_log_fmt_t *fmt;
|
|
747 ngx_http_log_main_conf_t *lmcf;
|
|
748
|
384
|
749 if (conf->open_file_cache == NGX_CONF_UNSET_PTR) {
|
|
750
|
|
751 conf->open_file_cache = prev->open_file_cache;
|
|
752 conf->open_file_cache_valid = prev->open_file_cache_valid;
|
|
753 conf->open_file_cache_min_uses = prev->open_file_cache_min_uses;
|
|
754
|
|
755 if (conf->open_file_cache == NGX_CONF_UNSET_PTR) {
|
|
756 conf->open_file_cache = NULL;
|
|
757 }
|
|
758 }
|
|
759
|
278
|
760 if (conf->logs || conf->off) {
|
|
761 return NGX_CONF_OK;
|
|
762 }
|
118
|
763
|
386
|
764 conf->logs = prev->logs;
|
|
765 conf->off = prev->off;
|
118
|
766
|
278
|
767 if (conf->logs || conf->off) {
|
|
768 return NGX_CONF_OK;
|
|
769 }
|
118
|
770
|
278
|
771 conf->logs = ngx_array_create(cf->pool, 2, sizeof(ngx_http_log_t));
|
|
772 if (conf->logs == NULL) {
|
|
773 return NGX_CONF_ERROR;
|
|
774 }
|
118
|
775
|
278
|
776 log = ngx_array_push(conf->logs);
|
|
777 if (log == NULL) {
|
|
778 return NGX_CONF_ERROR;
|
|
779 }
|
118
|
780
|
278
|
781 log->file = ngx_conf_open_file(cf->cycle, &ngx_http_access_log);
|
|
782 if (log->file == NULL) {
|
|
783 return NGX_CONF_ERROR;
|
|
784 }
|
118
|
785
|
386
|
786 log->script = NULL;
|
278
|
787 log->disk_full_time = 0;
|
|
788 log->error_log_time = 0;
|
118
|
789
|
278
|
790 lmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_log_module);
|
|
791 fmt = lmcf->formats.elts;
|
|
792
|
|
793 /* the default "combined" format */
|
|
794 log->ops = fmt[0].ops;
|
|
795 lmcf->combined_used = 1;
|
118
|
796
|
|
797 return NGX_CONF_OK;
|
|
798 }
|
|
799
|
|
800
|
|
801 static char *
|
|
802 ngx_http_log_set_log(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
|
|
803 {
|
|
804 ngx_http_log_loc_conf_t *llcf = conf;
|
|
805
|
384
|
806 ssize_t buf;
|
|
807 ngx_uint_t i, n;
|
|
808 ngx_str_t *value, name;
|
|
809 ngx_http_log_t *log;
|
|
810 ngx_http_log_fmt_t *fmt;
|
|
811 ngx_http_log_main_conf_t *lmcf;
|
|
812 ngx_http_script_compile_t sc;
|
118
|
813
|
|
814 value = cf->args->elts;
|
|
815
|
|
816 if (ngx_strcmp(value[1].data, "off") == 0) {
|
|
817 llcf->off = 1;
|
|
818 return NGX_CONF_OK;
|
|
819 }
|
|
820
|
|
821 if (llcf->logs == NULL) {
|
|
822 llcf->logs = ngx_array_create(cf->pool, 2, sizeof(ngx_http_log_t));
|
|
823 if (llcf->logs == NULL) {
|
|
824 return NGX_CONF_ERROR;
|
|
825 }
|
|
826 }
|
|
827
|
|
828 lmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_log_module);
|
|
829
|
|
830 log = ngx_array_push(llcf->logs);
|
|
831 if (log == NULL) {
|
|
832 return NGX_CONF_ERROR;
|
|
833 }
|
|
834
|
384
|
835 ngx_memzero(log, sizeof(ngx_http_log_t));
|
|
836
|
|
837 n = ngx_http_script_variables_count(&value[1]);
|
|
838
|
|
839 if (n == 0) {
|
|
840 log->file = ngx_conf_open_file(cf->cycle, &value[1]);
|
|
841 if (log->file == NULL) {
|
|
842 return NGX_CONF_ERROR;
|
|
843 }
|
|
844
|
|
845 } else {
|
|
846 if (ngx_conf_full_name(cf->cycle, &value[1], 0) == NGX_ERROR) {
|
|
847 return NGX_CONF_ERROR;
|
|
848 }
|
|
849
|
|
850 log->script = ngx_pcalloc(cf->pool, sizeof(ngx_http_log_script_t));
|
|
851 if (log->script == NULL) {
|
|
852 return NGX_CONF_ERROR;
|
|
853 }
|
|
854
|
|
855 ngx_memzero(&sc, sizeof(ngx_http_script_compile_t));
|
|
856
|
|
857 sc.cf = cf;
|
|
858 sc.source = &value[1];
|
|
859 sc.lengths = &log->script->lengths;
|
|
860 sc.values = &log->script->values;
|
|
861 sc.variables = n;
|
|
862 sc.complete_lengths = 1;
|
|
863 sc.complete_values = 1;
|
|
864
|
|
865 if (ngx_http_script_compile(&sc) != NGX_OK) {
|
|
866 return NGX_CONF_ERROR;
|
|
867 }
|
118
|
868 }
|
|
869
|
120
|
870 if (cf->args->nelts >= 3) {
|
118
|
871 name = value[2];
|
130
|
872
|
|
873 if (ngx_strcmp(name.data, "combined") == 0) {
|
|
874 lmcf->combined_used = 1;
|
|
875 }
|
|
876
|
118
|
877 } else {
|
|
878 name.len = sizeof("combined") - 1;
|
|
879 name.data = (u_char *) "combined";
|
122
|
880 lmcf->combined_used = 1;
|
118
|
881 }
|
|
882
|
|
883 fmt = lmcf->formats.elts;
|
|
884 for (i = 0; i < lmcf->formats.nelts; i++) {
|
|
885 if (fmt[i].name.len == name.len
|
|
886 && ngx_strcasecmp(fmt[i].name.data, name.data) == 0)
|
|
887 {
|
|
888 log->ops = fmt[i].ops;
|
120
|
889 goto buffer;
|
118
|
890 }
|
|
891 }
|
|
892
|
|
893 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
894 "unknown log format \"%V\"", &name);
|
120
|
895 return NGX_CONF_ERROR;
|
118
|
896
|
120
|
897 buffer:
|
|
898
|
|
899 if (cf->args->nelts == 4) {
|
|
900 if (ngx_strncmp(value[3].data, "buffer=", 7) != 0) {
|
|
901 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
902 "invalid parameter \"%V\"", &value[3]);
|
|
903 return NGX_CONF_ERROR;
|
|
904 }
|
|
905
|
384
|
906 if (log->script) {
|
|
907 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
908 "buffered logs can not have variables in name");
|
|
909 return NGX_CONF_ERROR;
|
|
910 }
|
|
911
|
120
|
912 name.len = value[3].len - 7;
|
|
913 name.data = value[3].data + 7;
|
|
914
|
|
915 buf = ngx_parse_size(&name);
|
|
916
|
|
917 if (buf == NGX_ERROR) {
|
|
918 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
919 "invalid parameter \"%V\"", &value[3]);
|
|
920 return NGX_CONF_ERROR;
|
|
921 }
|
|
922
|
|
923 if (log->file->buffer && log->file->last - log->file->pos != buf) {
|
|
924 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
925 "access_log \"%V\" already defined "
|
|
926 "with different buffer size", &value[1]);
|
|
927 return NGX_CONF_ERROR;
|
|
928 }
|
|
929
|
|
930 log->file->buffer = ngx_palloc(cf->pool, buf);
|
|
931 if (log->file->buffer == NULL) {
|
|
932 return NGX_CONF_ERROR;
|
|
933 }
|
|
934
|
|
935 log->file->pos = log->file->buffer;
|
|
936 log->file->last = log->file->buffer + buf;
|
|
937 }
|
|
938
|
|
939 return NGX_CONF_OK;
|
118
|
940 }
|
|
941
|
|
942
|
|
943 static char *
|
|
944 ngx_http_log_set_format(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
|
|
945 {
|
|
946 ngx_http_log_main_conf_t *lmcf = conf;
|
|
947
|
122
|
948 ngx_str_t *value;
|
|
949 ngx_uint_t i;
|
|
950 ngx_http_log_fmt_t *fmt;
|
118
|
951
|
|
952 value = cf->args->elts;
|
|
953
|
|
954 fmt = lmcf->formats.elts;
|
122
|
955 for (i = 0; i < lmcf->formats.nelts; i++) {
|
|
956 if (fmt[i].name.len == value[1].len
|
|
957 && ngx_strcmp(fmt[i].name.data, value[1].data) == 0)
|
118
|
958 {
|
380
|
959 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
960 "duplicate \"log_format\" name \"%V\"",
|
|
961 &value[1]);
|
|
962 return NGX_CONF_ERROR;
|
118
|
963 }
|
|
964 }
|
|
965
|
|
966 fmt = ngx_array_push(&lmcf->formats);
|
|
967 if (fmt == NULL) {
|
|
968 return NGX_CONF_ERROR;
|
|
969 }
|
|
970
|
|
971 fmt->name = value[1];
|
|
972
|
122
|
973 fmt->ops = ngx_array_create(cf->pool, 16, sizeof(ngx_http_log_op_t));
|
118
|
974 if (fmt->ops == NULL) {
|
|
975 return NGX_CONF_ERROR;
|
|
976 }
|
|
977
|
122
|
978 return ngx_http_log_compile_format(cf, fmt->ops, cf->args, 2);
|
|
979 }
|
|
980
|
118
|
981
|
122
|
982 static char *
|
|
983 ngx_http_log_compile_format(ngx_conf_t *cf, ngx_array_t *ops,
|
|
984 ngx_array_t *args, ngx_uint_t s)
|
|
985 {
|
260
|
986 u_char *data, *p, ch;
|
|
987 size_t i, len;
|
|
988 ngx_str_t *value, var;
|
|
989 ngx_uint_t bracket;
|
|
990 ngx_http_log_op_t *op;
|
|
991 ngx_http_log_var_t *v;
|
122
|
992
|
|
993 value = args->elts;
|
|
994
|
|
995 for ( /* void */ ; s < args->nelts; s++) {
|
118
|
996
|
260
|
997 for (i = 0; i < value[s].len; i++) {
|
|
998 if (value[s].data[i] != '%') {
|
|
999 continue;
|
|
1000 }
|
|
1001
|
|
1002 ch = value[s].data[i + 1];
|
|
1003
|
|
1004 if ((ch >= 'A' && ch <= 'Z')
|
|
1005 || (ch >= 'a' && ch <= 'z')
|
|
1006 || ch == '{')
|
|
1007 {
|
|
1008 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
1009 "the parameters in the \"%%name\" form are not supported, "
|
|
1010 "use the \"$variable\" instead");
|
|
1011
|
|
1012 return NGX_CONF_ERROR;
|
|
1013 }
|
|
1014 }
|
|
1015
|
118
|
1016 i = 0;
|
|
1017
|
|
1018 while (i < value[s].len) {
|
|
1019
|
122
|
1020 op = ngx_array_push(ops);
|
118
|
1021 if (op == NULL) {
|
|
1022 return NGX_CONF_ERROR;
|
|
1023 }
|
|
1024
|
|
1025 data = &value[s].data[i];
|
|
1026
|
260
|
1027 if (value[s].data[i] == '$') {
|
118
|
1028
|
|
1029 if (++i == value[s].len) {
|
|
1030 goto invalid;
|
|
1031 }
|
|
1032
|
|
1033 if (value[s].data[i] == '{') {
|
|
1034 bracket = 1;
|
|
1035
|
|
1036 if (++i == value[s].len) {
|
|
1037 goto invalid;
|
|
1038 }
|
|
1039
|
|
1040 var.data = &value[s].data[i];
|
|
1041
|
|
1042 } else {
|
|
1043 bracket = 0;
|
|
1044 var.data = &value[s].data[i];
|
|
1045 }
|
|
1046
|
|
1047 for (var.len = 0; i < value[s].len; i++, var.len++) {
|
|
1048 ch = value[s].data[i];
|
|
1049
|
|
1050 if (ch == '}' && bracket) {
|
|
1051 i++;
|
|
1052 bracket = 0;
|
|
1053 break;
|
|
1054 }
|
|
1055
|
|
1056 if ((ch >= 'A' && ch <= 'Z')
|
|
1057 || (ch >= 'a' && ch <= 'z')
|
|
1058 || (ch >= '0' && ch <= '9')
|
|
1059 || ch == '_')
|
|
1060 {
|
|
1061 continue;
|
|
1062 }
|
|
1063
|
|
1064 break;
|
|
1065 }
|
|
1066
|
|
1067 if (bracket) {
|
|
1068 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
1069 "the closing bracket in \"%V\" "
|
|
1070 "variable is missing", &var);
|
|
1071 return NGX_CONF_ERROR;
|
|
1072 }
|
|
1073
|
|
1074 if (var.len == 0) {
|
|
1075 goto invalid;
|
|
1076 }
|
|
1077
|
126
|
1078 if (ngx_strncmp(var.data, "apache_bytes_sent", 17) == 0) {
|
|
1079 ngx_conf_log_error(NGX_LOG_WARN, cf, 0,
|
|
1080 "use \"$body_bytes_sent\" instead of "
|
|
1081 "\"$apache_bytes_sent\"");
|
|
1082 }
|
|
1083
|
122
|
1084 for (v = ngx_http_log_vars; v->name.len; v++) {
|
|
1085
|
|
1086 if (v->name.len == var.len
|
|
1087 && ngx_strncmp(v->name.data, var.data, var.len) == 0)
|
|
1088 {
|
|
1089 op->len = v->len;
|
|
1090 op->getlen = NULL;
|
|
1091 op->run = v->run;
|
|
1092 op->data = 0;
|
|
1093
|
|
1094 goto found;
|
|
1095 }
|
|
1096 }
|
|
1097
|
118
|
1098 if (ngx_http_log_variable_compile(cf, op, &var) != NGX_OK) {
|
|
1099 return NGX_CONF_ERROR;
|
|
1100 }
|
|
1101
|
122
|
1102 found:
|
|
1103
|
|
1104 continue;
|
|
1105 }
|
|
1106
|
|
1107 i++;
|
118
|
1108
|
260
|
1109 while (i < value[s].len && value[s].data[i] != '$') {
|
122
|
1110 i++;
|
|
1111 }
|
118
|
1112
|
122
|
1113 len = &value[s].data[i] - data;
|
118
|
1114
|
122
|
1115 if (len) {
|
118
|
1116
|
122
|
1117 op->len = len;
|
|
1118 op->getlen = NULL;
|
118
|
1119
|
122
|
1120 if (len <= sizeof(uintptr_t)) {
|
|
1121 op->run = ngx_http_log_copy_short;
|
|
1122 op->data = 0;
|
118
|
1123
|
122
|
1124 while (len--) {
|
|
1125 op->data <<= 8;
|
|
1126 op->data |= data[len];
|
|
1127 }
|
|
1128
|
|
1129 } else {
|
|
1130 op->run = ngx_http_log_copy_long;
|
118
|
1131
|
382
|
1132 p = ngx_pnalloc(cf->pool, len);
|
122
|
1133 if (p == NULL) {
|
|
1134 return NGX_CONF_ERROR;
|
|
1135 }
|
118
|
1136
|
122
|
1137 ngx_memcpy(p, data, len);
|
|
1138 op->data = (uintptr_t) p;
|
118
|
1139 }
|
|
1140 }
|
|
1141 }
|
|
1142 }
|
|
1143
|
|
1144 return NGX_CONF_OK;
|
|
1145
|
|
1146 invalid:
|
|
1147
|
|
1148 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "invalid parameter \"%s\"", data);
|
|
1149
|
|
1150 return NGX_CONF_ERROR;
|
|
1151 }
|
|
1152
|
|
1153
|
384
|
1154 static char *
|
|
1155 ngx_http_log_open_file_cache(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
|
|
1156 {
|
|
1157 ngx_http_log_loc_conf_t *llcf = conf;
|
|
1158
|
|
1159 time_t inactive, valid;
|
|
1160 ngx_str_t *value, s;
|
|
1161 ngx_int_t max, min_uses;
|
|
1162 ngx_uint_t i;
|
|
1163
|
|
1164 if (llcf->open_file_cache != NGX_CONF_UNSET_PTR) {
|
|
1165 return "is duplicate";
|
|
1166 }
|
|
1167
|
|
1168 value = cf->args->elts;
|
|
1169
|
|
1170 max = 0;
|
|
1171 inactive = 10;
|
|
1172 valid = 60;
|
|
1173 min_uses = 1;
|
|
1174
|
|
1175 for (i = 1; i < cf->args->nelts; i++) {
|
|
1176
|
|
1177 if (ngx_strncmp(value[i].data, "max=", 4) == 0) {
|
|
1178
|
|
1179 max = ngx_atoi(value[i].data + 4, value[i].len - 4);
|
|
1180 if (max == NGX_ERROR) {
|
|
1181 goto failed;
|
|
1182 }
|
|
1183
|
|
1184 continue;
|
|
1185 }
|
|
1186
|
|
1187 if (ngx_strncmp(value[i].data, "inactive=", 9) == 0) {
|
|
1188
|
|
1189 s.len = value[i].len - 9;
|
|
1190 s.data = value[i].data + 9;
|
|
1191
|
|
1192 inactive = ngx_parse_time(&s, 1);
|
|
1193 if (inactive < 0) {
|
|
1194 goto failed;
|
|
1195 }
|
|
1196
|
|
1197 continue;
|
|
1198 }
|
|
1199
|
|
1200 if (ngx_strncmp(value[i].data, "min_uses=", 9) == 0) {
|
|
1201
|
|
1202 min_uses = ngx_atoi(value[i].data + 9, value[i].len - 9);
|
|
1203 if (min_uses == NGX_ERROR) {
|
|
1204 goto failed;
|
|
1205 }
|
|
1206
|
|
1207 continue;
|
|
1208 }
|
|
1209
|
|
1210 if (ngx_strncmp(value[i].data, "valid=", 6) == 0) {
|
|
1211
|
|
1212 s.len = value[i].len - 6;
|
|
1213 s.data = value[i].data + 6;
|
|
1214
|
|
1215 valid = ngx_parse_time(&s, 1);
|
|
1216 if (valid < 0) {
|
|
1217 goto failed;
|
|
1218 }
|
|
1219
|
|
1220 continue;
|
|
1221 }
|
|
1222
|
|
1223 if (ngx_strcmp(value[i].data, "off") == 0) {
|
|
1224
|
|
1225 llcf->open_file_cache = NULL;
|
|
1226
|
|
1227 continue;
|
|
1228 }
|
|
1229
|
|
1230 failed:
|
|
1231
|
|
1232 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
1233 "invalid \"open_log_file_cache\" parameter \"%V\"",
|
|
1234 &value[i]);
|
|
1235 return NGX_CONF_ERROR;
|
|
1236 }
|
|
1237
|
|
1238 if (llcf->open_file_cache == NULL) {
|
|
1239 return NGX_CONF_OK;
|
|
1240 }
|
|
1241
|
|
1242 if (max == 0) {
|
|
1243 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
1244 "\"open_log_file_cache\" must have \"max\" parameter");
|
|
1245 return NGX_CONF_ERROR;
|
|
1246 }
|
|
1247
|
|
1248 llcf->open_file_cache = ngx_open_file_cache_init(cf->pool, max, inactive);
|
|
1249
|
|
1250 if (llcf->open_file_cache) {
|
|
1251
|
|
1252 llcf->open_file_cache_valid = valid;
|
|
1253 llcf->open_file_cache_min_uses = min_uses;
|
|
1254
|
|
1255 return NGX_CONF_OK;
|
|
1256 }
|
|
1257
|
|
1258 return NGX_CONF_ERROR;
|
|
1259 }
|
|
1260
|
|
1261
|
118
|
1262 static ngx_int_t
|
122
|
1263 ngx_http_log_init(ngx_conf_t *cf)
|
118
|
1264 {
|
122
|
1265 ngx_str_t *value;
|
|
1266 ngx_array_t a;
|
130
|
1267 ngx_http_handler_pt *h;
|
122
|
1268 ngx_http_log_fmt_t *fmt;
|
|
1269 ngx_http_log_main_conf_t *lmcf;
|
118
|
1270 ngx_http_core_main_conf_t *cmcf;
|
|
1271
|
122
|
1272 lmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_log_module);
|
|
1273
|
|
1274 if (lmcf->combined_used) {
|
|
1275 if (ngx_array_init(&a, cf->pool, 1, sizeof(ngx_str_t)) != NGX_OK) {
|
|
1276 return NGX_ERROR;
|
|
1277 }
|
|
1278
|
|
1279 value = ngx_array_push(&a);
|
|
1280 if (value == NULL) {
|
|
1281 return NGX_ERROR;
|
|
1282 }
|
|
1283
|
|
1284 *value = ngx_http_combined_fmt;
|
|
1285 fmt = lmcf->formats.elts;
|
|
1286
|
|
1287 if (ngx_http_log_compile_format(cf, fmt->ops, &a, 0)
|
|
1288 != NGX_CONF_OK)
|
|
1289 {
|
|
1290 return NGX_ERROR;
|
|
1291 }
|
|
1292 }
|
|
1293
|
|
1294 cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module);
|
118
|
1295
|
130
|
1296 h = ngx_array_push(&cmcf->phases[NGX_HTTP_LOG_PHASE].handlers);
|
|
1297 if (h == NULL) {
|
|
1298 return NGX_ERROR;
|
|
1299 }
|
|
1300
|
|
1301 *h = ngx_http_log_handler;
|
118
|
1302
|
|
1303 return NGX_OK;
|
|
1304 }
|