Logs are very useful for statistical troubleshooting. This article summarizes nginx log-related configurations such as access_log, log_format, open_log_file_cache, log_not_found, log_subrequest, rewrite_log, error_log.
nginx has a very flexible logging mode. Each level of configuration can have its own independent access log. The log format is defined by the log_format command. ngx_http_log_module is used to define the request log format.
1. access_log command
语法: access_log path [format [buffer=size [flush=time]]];
access_log path format gzip[=level] [buffer=size] [flush=time];
access_log syslog:server=address[,parameter=value] [format];
access_log off;
Default value: access_log logs/access.log combined;
configuration section: http, server, location, if in location, limit_except
gzip compression level.
buffer sets the memory buffer size.
The maximum time that flush is kept in the cache.
Do not record logs: access_log off;
use the default combined format to record logs: access_log logs/access.log or access_log logs/access.log combined;
2. log_format command
Syntax: log_format name string …;
Default value: log_format combined “…”;
Configuration section: http
name indicates the format name, and string indicates the equivalent format. log_format has a default combined log format that does not need to be set, which is equivalent to the combined log format of apache, as follows:
log_format combined '$remote_addr - $remote_user [$time_local] '
' "$request" $status $body_bytes_sent '
' "$http_referer" "$http_user_agent" ';
If nginx is behind a load balancer, squid, or nginx reverse proxy, the web server cannot directly obtain the real IP address of the client. $remote_addr gets the IP address of the reverse proxy. The reverse proxy server can add X-Forwarded-For information in the http header information of the forwarding request, which is used to record the client IP address and the server address requested by the client. As follows:
log_format porxy '$http_x_forwarded_for - $remote_user [$time_local] '
' "$request" $status $body_bytes_sent '
' "$http_referer" "$http_user_agent" ';
The variable comments allowed in the log format are as follows:
$remote_addr, $http_x_forwarded_for record client IP address
$remote_user record client user name
$request record request URL and HTTP protocol
$status record request status
$body_bytes_sent number of bytes sent to client, excluding response header size; this variable is compatible with "%B" parameter in Apache module mod_log_config.
$bytes_sent Total bytes sent to client.
$connection The serial number of the connection.
$connection_requests The number of requests currently obtained through a connection.
$msec log write time. The unit is seconds, and the precision is milliseconds.
$pipe If the request is sent via HTTP pipeline (pipelined), the value of pipe is "p", otherwise it is ".".
$http_referer records which page link is visited from
$http_user_agent records client browser related information
$request_length length of request (including request line, request header and request body).
$request_time Request processing time, in seconds, precision milliseconds; from the first byte read into the client, until the log is written after the last character is sent to the client.
$time_iso8601 Local time in ISO8601 standard format.
$time_local local time in common log format.
The response headers sent to the client have the "sent_http_" prefix. For example $sent_http_content_range.
Examples are as follows:
http {
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'"$status" $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for" '
'"$gzip_ratio" $request_time $bytes_sent $request_length';
log_format srcache_log '$remote_addr - $remote_user [$time_local] "$request" '
'"$status" $body_bytes_sent $request_time $bytes_sent $request_length '
'[$upstream_response_time] [$srcache_fetch_status] [$srcache_store_status] [$srcache_expire]';
open_log_file_cache max=1000 inactive=60s;
server {
server_name ~^(www\.)?(.+)$;
access_log logs/$2-access.log main;
error_log logs/$2-error.log;
location /srcache {
access_log logs/access-srcache.log srcache_log;
}
}
}
3. open_log_file_cache directive
Syntax: open_log_file_cache max=N [inactive=time] [min_uses=N] [valid=time];
open_log_file_cache off;
Default value: open_log_file_cache off;
Configuration section: http, server, location
For each log record, the file will be opened, written to the log, and closed. You can use open_log_file_cache to set the log file cache (default is off), the format is as follows:
The parameter notes are as follows:
max: Set the maximum number of file descriptors in the cache. If the cache is full, the LRU algorithm will be used to close the descriptor.
inactive: Set the survival time, the default is 10s
min_uses: Set the minimum number of times the log file is used within the inactive time period, the log file descriptor is recorded in the cache, the default is 1 time
valid: Set the checking frequency, the default is 60s
off: Disable the cache
Examples are as follows:
open_log_file_cache max=1000 inactive=20s valid=1m min_uses=2;
4. log_not_found command
Syntax: log_not_found on | off;
Default value: log_not_found on;
Configuration section: http, server, location
Whether to record non-existent errors in error_log. The default is.
5. log_subrequest command
Syntax: log_subrequest on | off;
Default value: log_subrequest off;
Configuration section: http, server, location
Whether to record the access log of subrequest in access_log. By default no logging is done.
6. rewrite_log command
Provided by the ngx_http_rewrite_module module. Used to record the rewrite log. It is recommended to enable it for debugging rewrite rules. Nginx rewrite rule guide
Syntax: rewrite_log on | off;
Default value: rewrite_log off;
Configuration section: http, server, location, if
enabled, will record notice-level rewrite logs in the error log.
7. error_log command
Syntax: error_log file | stderr | syslog:server=address[,parameter=value] [debug | info | notice | warn | error | crit | alert | emerg]; default value: error_log logs/error.log error; configuration section: main, http, server, location configuration
error
log
.