(Transfer) Detailed explanation of nginx log configuration instructions

This article mainly introduces the detailed explanation of nginx log configuration instructions. nginx has a very flexible logging mode. Each level of configuration can have its own independent access log. Friends who need it can refer to the
log for statistical troubleshooting. of. 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
syntax: access_log path [format [buffer=size [flush=time]]];
copy the code as follows:

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 amount of time a flush has been kept in the buffer.
Do not log: access_log off;
use the default combined format to log: access_log logs/access.log or access_log logs/access.log combined;
2. log_format directive
syntax: log_format name string …;
default value: log_format combined “…”;
configuration Segment: http
name represents the format name, string represents 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 shown below: The
code is copied as follows:

log_format combined '$remote_addr - $remote_user [$time_local] '
                                   ' "$request" $status $ body_bytes_sent '
                                   ' "$http_referer" "$http_user_agent" ';
If nginx is located behind the load balancer, squid, and 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 to the HTTP header information of the forwarding request to record the client IP address and the server address requested by the client. As shown below:
Copy the code The code is 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:
Copy the code as follows:

$remote_addr , $http_x_forwarded_for records the client IP address
$remote_user records the client user name
$request records the requested URL and HTTP protocol
$status records the request status
$body_bytes_sent the number of bytes sent to the client, excluding the size of the response header; this variable is the same as Apache Compatible with "%B" parameter in module mod_log_config.
$bytes_sent The total number of bytes sent to the client.
$connection The serial number of the connection.
$connection_requests The number of requests currently made through a connection.
$msec log write time. The unit is seconds and the precision is milliseconds.
$pipe The pipe value is "p" if the request was sent via HTTP pipelined, otherwise ".


$request_length The length of the request (including request line, request headers and request body).
$request_time Request processing time, in seconds, precision in milliseconds; from the first byte read into the client until the last character is sent to the client for log writing.
$time_iso8601 Local time in ISO8601 standard format.
$time_local Local time in common log format.
Response headers sent to the client have the "sent_http_" prefix. For example $sent_http_content_range.
The example is as follows:
Copy the code The code is 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 '
                                '"$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指令
语法: open_log_file_cache max=N [inactive=time] [min_uses=N] [valid=time];
open_log_file_cache off;
默认值: open_log_file_cache off;
配置段: http, server, location
For each log record, the file will be opened first, then written to the log, and then closed. You can use open_log_file_cache to set the log file cache (default is off), the format is as follows: The
parameter comments are as follows:
max: Set the maximum number of file descriptors in the cache. If the cache is full, the LRU algorithm is used to close the descriptors.
inactive: set the survival time, the default is 10s
min_uses: set in the inactive time period, after the log file is used at least a few times, the log file descriptor is recorded in the cache, the default is 1 time
valid: set the check frequency, the default is 60s
off: The example of disabling the cache
is as follows:
Copy the code 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
Log non-existent errors in error_log. Default is.
5. log_subrequest command
syntax: log_subrequest on | off;
default value: log_subrequest off;
configuration segment: http, server, location
Whether to record the access log of the subrequest in the access_log. Not logged by default.
6. rewrite_log command
Provided by the ngx_http_rewrite_module module. Used to record the rewrite log. It is recommended to enable the debugging of rewrite rules. Nginx Rewrite Rules Guide
Syntax: rewrite_log on | off;
Default value: rewrite_log off;
Configuration section: http, server, location, if
enabled, the notice-level rewrite log will be recorded 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

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324928923&siteId=291194637