preface

Nginx logs are useful for statistics and system service troubleshooting. Nginx logs are classified into two types: access_log and error_log. Through the access log, we can get the user’s IP address, browser information, request processing time and other information. The error log records information about access errors and helps us locate the cause of errors. This article describes in detail how to configure Nginx logging.

Set up theaccess_log

Access logs record client requests. Every request made by a client to the Nginx server is recorded here. The client IP, browser information, referer, request processing time, and request URL are all available in the access log. Of course, you can use the log_format directive to define exactly what information to record.

grammar

access_log path [format [buffer=size] [gzip[=level]] [flush=time] [if=condition]]; Set the access log
access_log off; # Close the access log

Copy the code
  • Path Specifies the location where logs are stored.
  • formatSpecifies the log format. The predefined one is used by defaultcombined.
  • The buffer is used to specify the size of the cache for log writing. The default is 64K.
  • Gzip Logs are compressed before being written. The compression ratio can be specified. The larger the value from 1 to 9, the higher the compression ratio is and the slower the compression rate is. The default is 1.
  • Flush Sets the cache validity period. If the time specified by flush is exceeded, the cache will be cleared.
  • If condition. If the specified condition evaluates to 0 or an empty string, the request is not written to the log.

In addition, there is a special value off. If this value is specified, all request logging in the current scope is disabled.

scope

The scope to which the access_log directive can be applied are HTTP, server, location, and limit_except. In other words, Nginx will report an error when using this instruction outside of these domains.

This is the basic syntax of the access_log directive and what the parameters mean. Let’s take a look at a few examples to further understand.

Basic usage

access_log /var/logs/nginx-access.log
Copy the code

This example specifies that the log is written to /var/logs/nginx-access.log, with the default log format of Combined.

access_log /var/logs/nginx-access.log buffer=32k gzip flush=1m
Copy the code

This example specifies that the log path is /var/logs/nginx-access.log, the log format is combined, the default log format is combined, the log cache size is 32K, the log compression ratio is 1, and the cached data is valid for 1 minute.

Use log_format to customize the log format

Nginx predefines a log format named Combined, which is used by default if the log format is not explicitly specified:

log_format combined '$remote_addr - $remote_user [$time_local] '
                    '"$request" $status $body_bytes_sent '
                    '"$http_referer" "$http_user_agent"';
Copy the code

If you don’t want to use the Nginx predefined formats, you can define them using the log_format directive.

grammar

log_format name [escape=default|json] string ... ;Copy the code
  • Name Format name. Referenced in the access_log directive.
  • escapeSet the character encoding in the variable tojsonordefaultThe default isdefault.
  • String Specifies the log format to be defined. This parameter can be multiple. You can use the Nginx variable in the argument.

Here are some commonly used variables in the log_format directive:

variable meaning
$bytes_sent Total number of bytes sent to the client
$body_bytes_sent Number of bytes sent to the client, excluding the size of the response header
$connection Connection serial number
$connection_requests The number of requests currently made over the connection
$msec Log write time, in seconds, with precision in milliseconds
$pipe The value is “p” if the request is sent through the HTTP pipeline, otherwise “.”
$request_length Request length (including request line, request header and request body)
$request_time Request processing duration, in seconds and precision in milliseconds, from the first byte read to the last character sent to the client for log writing
$status Response status code
$time_iso8601 Local time in standard format, for example, 2017-05-24T18:31:27+08:00
$time_local The local time in the common log format, for example, 24/May/2017:18:31:27 +0800.
$http_referer The referer address requested.
$http_user_agent Client browser information.
$remote_addr The client IP
$http_x_forwarded_for If there is a proxy server at the front end, set the configuration of the Web node to record the client address. This parameter takes effect only if the proxy server also has the related x_forwarded_for setting.
$request Complete original request line, such as “GET/HTTP/1.1”
$remote_user Client user name for a request with user authentication enabled
$request_uri The full request address, such as “https://daojia.com/”

Here is an example of using a custom log format:

access_log /var/logs/nginx-access.log main
log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                  '$status $body_bytes_sent "$http_referer" '
                  '"$http_user_agent" "$http_x_forwarded_for"';

Copy the code

We define a format for main using the log_format directive and refer to it in the access_log directive. If the client made a request: https://suyunfe.com/, let’s take a look at the log of a request I intercepted:

112.195.209.90 - - [20/Feb/2018:12:12:14 +0800] "The GET/HTTP / 1.1"200, 190,"-" "Mozilla / 5.0 (Linux; The Android 6.0. Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome "-"

Copy the code

$remote_user $http_referer $http_x_forwarded_for $http_referer $HTTP_x_forwarded_for

Set the error_log

Error logging is implemented in Nginx through the error_log directive. This directive records error information during server and request processing.

grammar

The path and log level of the error log file are configured.

error_log file [level];
Default:	
error_log logs/error.log error;

Copy the code

The first parameter specifies where the log is written.

The second parameter specifies the log level. Level can be any value of DEBUG, INFO, Notice, WARN, error, crit, alert,emerg. As you can see, the values are listed from lowest to highest in urgency. Logs are written to error logs only when the error level is equal to or higher than the value specified by level. The default value is Error.

Basic usage

error_log /var/logs/nginx/nginx-error.log

Copy the code

It can be configured in: Main, HTTP, mail, Stream, Server, location scopes.

In this example, the error log path is specified as /var/logs/nginx-nginx-error. log, and the default error log level is used.

open_log_file_cache

Each log record is written by opening the file, writing to the record, and then closing the log file. If your log file path uses variables such as access_log /var/logs/$host-nginx-access.log, you can use the open_log_file_cache directive to set the cache of the log file descriptor to improve performance.

grammar

open_log_file_cache max=N [inactive=time] [min_uses=N] [valid=time];
Copy the code
  • Max sets the maximum number of file descriptors in the cache, and if it is full, the LRU algorithm is used to close the descriptors.
  • Inactive Sets the cache lifetime. The default is 10 seconds.
  • Min_uses Indicates the minimum number of times the log file is used during inactive period. The log file descriptor is cached. The default value is 1.
  • Valid: Set how long it takes to check the log file name to see if it has changed. Default: 60s
  • Off: The cache is not used. The default value is off.

Basic usage

open_log_file_cache max=1000 inactive=20s valid=1m min_uses=2;
Copy the code

It can be configured in the HTTP, server, and Location scopes.

In this example, set a maximum of 1000 log file descriptors to be cached in the cache. If the log file descriptors in the cache are accessed at least twice within 20s, the cache is not closed. Check the file descriptor file name in the cache every 1 minute.

conclusion

Nginx uses the access_log and error_log directives to configure access logs and error logs. Log_format allows you to customize the log format. If variables are used in the log file path, we can use the open_log_file_cache directive to set the cache to improve performance.

In addition, there are many variables used in access_log and log_format. These variables are not listed one by one. For detailed variable information, please refer to the Nginx official documentation