Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
nginx
GitHub Repository: nginx/nginx.org
Path: blob/main/xml/en/docs/http/ngx_http_log_module.xml
1 views
1
<?xml version="1.0"?>
2
3
<!--
4
Copyright (C) Igor Sysoev
5
Copyright (C) Nginx, Inc.
6
-->
7
8
<!DOCTYPE module SYSTEM "../../../../dtd/module.dtd">
9
10
<module name="Module ngx_http_log_module"
11
link="/en/docs/http/ngx_http_log_module.html"
12
lang="en"
13
rev="21">
14
15
<section id="summary">
16
17
<para>
18
The <literal>ngx_http_log_module</literal> module writes request logs
19
in the specified format.
20
</para>
21
22
<para>
23
Requests are logged in the context of a location where processing ends.
24
It may be different from the original location, if an
25
<link doc="ngx_http_core_module.xml" id="internal">internal
26
redirect</link> happens during request processing.
27
</para>
28
29
</section>
30
31
32
<section id="example" name="Example Configuration">
33
34
<para>
35
<example>
36
log_format compression '$remote_addr - $remote_user [$time_local] '
37
'"$request" $status $bytes_sent '
38
'"$http_referer" "$http_user_agent" "$gzip_ratio"';
39
40
access_log /spool/logs/nginx-access.log compression buffer=32k;
41
</example>
42
</para>
43
44
</section>
45
46
47
<section id="directives" name="Directives">
48
49
<directive name="access_log">
50
<syntax>
51
<value>path</value>
52
[<value>format</value>
53
[<literal>buffer</literal>=<value>size</value>]
54
[<literal>gzip[=<value>level</value>]</literal>]
55
[<literal>flush</literal>=<value>time</value>]
56
[<literal>if</literal>=<value>condition</value>]]</syntax>
57
<syntax><literal>off</literal></syntax>
58
<default>logs/access.log combined</default>
59
<context>http</context>
60
<context>server</context>
61
<context>location</context>
62
<context>if in location</context>
63
<context>limit_except</context>
64
65
<para>
66
Sets the path, format, and configuration for a buffered log write.
67
Several logs can be specified on the same configuration level.
68
Logging to <link doc="../syslog.xml">syslog</link>
69
can be configured by specifying
70
the “<literal>syslog:</literal>” prefix in the first parameter.
71
The special value <literal>off</literal> cancels all
72
<literal>access_log</literal> directives on the current level.
73
If the format is not specified then the predefined
74
<literal>combined</literal>” format is used.
75
</para>
76
77
<para>
78
If either the <literal>buffer</literal> or <literal>gzip</literal>
79
(1.3.10, 1.2.7)
80
parameter is used, writes to log will be buffered.
81
<note>
82
The buffer size must not exceed the size of an atomic write to a disk file.
83
For FreeBSD this size is unlimited.
84
</note>
85
</para>
86
87
<para>
88
When buffering is enabled, the data will be written to the file:
89
<list type="bullet">
90
91
<listitem>
92
if the next log line does not fit into the buffer;
93
</listitem>
94
95
<listitem>
96
if the buffered data is older than specified by the <literal>flush</literal>
97
parameter (1.3.10, 1.2.7);
98
</listitem>
99
100
<listitem>
101
when a worker process is <link doc="../control.xml">re-opening</link> log
102
files or is shutting down.
103
</listitem>
104
105
</list>
106
</para>
107
108
<para>
109
If the <literal>gzip</literal> parameter is used, then the buffered data will
110
be compressed before writing to the file.
111
The compression level can be set between 1 (fastest, less compression)
112
and 9 (slowest, best compression).
113
By default, the buffer size is equal to 64K bytes, and the compression level
114
is set to 1.
115
Since the data is compressed in atomic blocks, the log file can be decompressed
116
or read by “<literal>zcat</literal>” at any time.
117
</para>
118
119
<para>
120
Example:
121
<example>
122
access_log /path/to/log.gz combined gzip flush=5m;
123
</example>
124
</para>
125
126
<para>
127
<note>
128
For gzip compression to work, nginx must be built with the zlib library.
129
</note>
130
</para>
131
132
<para>
133
The file path can contain variables (0.7.6+),
134
but such logs have some constraints:
135
<list type="bullet">
136
137
<listitem>
138
the <link doc="../ngx_core_module.xml" id="user"/>
139
whose credentials are used by worker processes should
140
have permissions to create files in a directory with
141
such logs;
142
</listitem>
143
144
<listitem>
145
buffered writes do not work;
146
</listitem>
147
148
<listitem>
149
the file is opened and closed for each log write.
150
However, since the descriptors of frequently used files can be stored
151
in a <link id="open_log_file_cache">cache</link>, writing to the old file
152
can continue during the time specified by the <link id="open_log_file_cache"/>
153
directive’s <literal>valid</literal> parameter
154
</listitem>
155
156
<listitem>
157
during each log write the existence of the request’s
158
<link doc="ngx_http_core_module.xml" id="root">root directory</link>
159
is checked, and if it does not exist the log is not
160
created.
161
It is thus a good idea to specify both
162
<link doc="ngx_http_core_module.xml" id="root"/>
163
and <literal>access_log</literal> on the same configuration level:
164
<example>
165
server {
166
root /spool/vhost/data/$host;
167
access_log /spool/vhost/logs/$host;
168
...
169
</example>
170
</listitem>
171
172
</list>
173
</para>
174
175
<para>
176
The <literal>if</literal> parameter (1.7.0) enables conditional logging.
177
A request will not be logged if the <value>condition</value> evaluates to “0”
178
or an empty string.
179
In the following example, the requests with response codes 2xx and 3xx
180
will not be logged:
181
<example>
182
map $status $loggable {
183
~^[23] 0;
184
default 1;
185
}
186
187
access_log /path/to/access.log combined if=$loggable;
188
</example>
189
</para>
190
191
</directive>
192
193
194
<directive name="log_format">
195
<syntax>
196
<value>name</value>
197
[<literal>escape</literal>=<literal>default</literal>|<literal>json</literal>|<literal>none</literal>]
198
<value>string</value> ...</syntax>
199
<default>combined "..."</default>
200
<context>http</context>
201
202
<para>
203
Specifies log format.
204
</para>
205
206
<para id="log_format_escape">
207
The <literal>escape</literal> parameter (1.11.8) allows setting
208
<literal>json</literal> or <literal>default</literal> characters escaping
209
in variables, by default, <literal>default</literal> escaping is used.
210
The <literal>none</literal> value (1.13.10) disables escaping.
211
</para>
212
213
<para id="log_format_escape_default">
214
For <literal>default</literal> escaping,
215
characters “<literal>"</literal>”, “<literal>\</literal>”,
216
and other characters with values less than 32 (0.7.0) or above 126 (1.1.6)
217
are escaped as “<literal>\xXX</literal>”.
218
If the variable value is not found,
219
a hyphen (“<literal>-</literal>”) will be logged.
220
</para>
221
222
<para id="log_format_escape_json">
223
For <literal>json</literal> escaping,
224
all characters not allowed
225
in JSON <link url="https://datatracker.ietf.org/doc/html/rfc8259#section-7">strings</link>
226
will be escaped:
227
characters “<literal>"</literal>” and
228
<literal>\</literal>” are escaped as
229
<literal>\"</literal>” and “<literal>\\</literal>”,
230
characters with values less than 32 are escaped as
231
<literal>\n</literal>”,
232
<literal>\r</literal>”,
233
<literal>\t</literal>”,
234
<literal>\b</literal>”,
235
<literal>\f</literal>”, or
236
<literal>\u00XX</literal>”.
237
238
</para>
239
240
<para>
241
The log format can contain common variables, and variables that
242
exist only at the time of a log write:
243
<list type="tag">
244
245
<tag-name id="var_bytes_sent"><var>$bytes_sent</var></tag-name>
246
<tag-desc>
247
the number of bytes sent to a client
248
</tag-desc>
249
250
<tag-name id="var_connection"><var>$connection</var></tag-name>
251
<tag-desc>
252
connection serial number
253
</tag-desc>
254
255
<tag-name id="var_connection_requests"><var>$connection_requests</var>
256
</tag-name>
257
<tag-desc>
258
the current number of requests made through a connection (1.1.18)
259
</tag-desc>
260
261
<tag-name id="var_msec"><var>$msec</var></tag-name>
262
<tag-desc>
263
time in seconds with a milliseconds resolution at the time of the log write
264
</tag-desc>
265
266
<tag-name id="var_pipe"><var>$pipe</var></tag-name>
267
<tag-desc>
268
<literal>p</literal>” if request was pipelined, “<literal>.</literal>
269
otherwise
270
</tag-desc>
271
272
<tag-name id="var_request_length"><var>$request_length</var></tag-name>
273
<tag-desc>
274
request length (including request line, header, and request body)
275
</tag-desc>
276
277
<tag-name id="var_request_time"><var>$request_time</var></tag-name>
278
<tag-desc>
279
request processing time in seconds with a milliseconds resolution;
280
time elapsed between the first bytes were read from the client and
281
the log write after the last bytes were sent to the client
282
</tag-desc>
283
284
<tag-name id="var_status"><var>$status</var></tag-name>
285
<tag-desc>
286
response status
287
</tag-desc>
288
289
<tag-name id="var_time_iso8601"><var>$time_iso8601</var></tag-name>
290
<tag-desc>
291
local time in the ISO 8601 standard format
292
</tag-desc>
293
294
<tag-name id="var_time_local"><var>$time_local</var></tag-name>
295
<tag-desc>
296
local time in the Common Log Format
297
</tag-desc>
298
299
</list>
300
301
<note>
302
In the modern nginx versions variables
303
<link doc="ngx_http_core_module.xml" id="var_status">$status</link>
304
(1.3.2, 1.2.2),
305
<link doc="ngx_http_core_module.xml" id="var_bytes_sent">$bytes_sent</link>
306
(1.3.8, 1.2.5),
307
<link doc="ngx_http_core_module.xml" id="var_connection">$connection</link>
308
(1.3.8, 1.2.5),
309
<link doc="ngx_http_core_module.xml" id="var_connection_requests">$connection_requests</link>
310
(1.3.8, 1.2.5),
311
<link doc="ngx_http_core_module.xml" id="var_msec">$msec</link>
312
(1.3.9, 1.2.6),
313
<link doc="ngx_http_core_module.xml" id="var_request_time">$request_time</link>
314
(1.3.9, 1.2.6),
315
<link doc="ngx_http_core_module.xml" id="var_pipe">$pipe</link>
316
(1.3.12, 1.2.7),
317
<link doc="ngx_http_core_module.xml" id="var_request_length">$request_length</link>
318
(1.3.12, 1.2.7),
319
<link doc="ngx_http_core_module.xml" id="var_time_iso8601">$time_iso8601</link>
320
(1.3.12, 1.2.7),
321
and
322
<link doc="ngx_http_core_module.xml" id="var_time_local">$time_local</link>
323
(1.3.12, 1.2.7)
324
are also available as common variables.
325
</note>
326
327
</para>
328
329
<para>
330
Header lines sent to a client have the prefix
331
<literal>sent_http_</literal>”, for example,
332
<var>$sent_http_content_range</var>.
333
</para>
334
335
<para>
336
The configuration always includes the predefined
337
<literal>combined</literal>” format:
338
<example>
339
log_format combined '$remote_addr - $remote_user [$time_local] '
340
'"$request" $status $body_bytes_sent '
341
'"$http_referer" "$http_user_agent"';
342
</example>
343
</para>
344
345
</directive>
346
347
348
<directive name="open_log_file_cache">
349
350
<syntax>
351
<literal>max</literal>=<value>N</value>
352
[<literal>inactive</literal>=<value>time</value>]
353
[<literal>min_uses</literal>=<value>N</value>]
354
[<literal>valid</literal>=<value>time</value>]</syntax>
355
<syntax><literal>off</literal></syntax>
356
<default>off</default>
357
<context>http</context>
358
<context>server</context>
359
<context>location</context>
360
361
<para>
362
Defines a cache that stores the file descriptors of frequently used logs
363
whose names contain variables.
364
The directive has the following parameters:
365
<list type="tag">
366
367
<tag-name><literal>max</literal></tag-name>
368
<tag-desc>
369
sets the maximum number of descriptors in a cache;
370
if the cache becomes full the least recently used (LRU)
371
descriptors are closed
372
</tag-desc>
373
374
<tag-name><literal>inactive</literal></tag-name>
375
<tag-desc>
376
sets the time after which the cached descriptor is closed
377
if there were no access during this time;
378
by default, 10 seconds
379
</tag-desc>
380
381
<tag-name><literal>min_uses</literal></tag-name>
382
<tag-desc>
383
sets the minimum number of file uses during the time
384
defined by the <literal>inactive</literal> parameter
385
to let the descriptor stay open in a cache;
386
by default, 1
387
</tag-desc>
388
389
<tag-name><literal>valid</literal></tag-name>
390
<tag-desc>
391
sets the time after which it should be checked that the file
392
still exists with the same name; by default, 60 seconds
393
</tag-desc>
394
395
<tag-name><literal>off</literal></tag-name>
396
<tag-desc>
397
disables caching
398
</tag-desc>
399
400
</list>
401
</para>
402
403
<para>
404
Usage example:
405
<example>
406
open_log_file_cache max=1000 inactive=20s valid=1m min_uses=2;
407
</example>
408
</para>
409
410
</directive>
411
412
</section>
413
414
</module>
415
416