Nginx logs are useful for statistics and system service troubleshooting.
There are two main types of nginx logs:access_ Log and error_ Log (error log). By accessing the log, we can get the user’s IP address, browser information, request processing time and other information. Error logs record information about access errors, which can help us locate the cause of the errors.
This article describes in detail how to configure nginx logs.
Set access_ Log
The access log mainly records the client’s requests. Every request from the client to the nginx server is recorded here. Client IP, browser information, referer, request processing time, request URL, etc. can be obtained in the access log. Of course, you can log the specific information_ Format instruction definition.
access_log path [format [buffer=size] [gzip[=level]] [flush=time] [if=condition]]; #Set access log access_log off; #Close access log
- Path specifies the location of the log.
- Format specifies the format of the log. The predefined combined is used by default.
- Buffer is used to specify the cache size when the log is written. The default is 64K.
- Compress the gzip log before writing. The compression ratio can be specified, from 1 to 9, the higher the compression ratio, and the slower the compression speed. The default is 1.
- Flush sets the effective time of the cache. If the time specified by flush is exceeded, the contents of the cache will be emptied.
- If condition judgment. 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 logs under the current scope are closed.
Access can be applied_ The scope of the log instruction is HTTP, server, location and limit_ except。 In other words, nginx will report an error if the instruction is used outside these scopes.
The above is access_ The basic syntax of log instruction and the meaning of parameters. Let’s look at a few examples to deepen our understanding.
This example specifies that the log write path is / var / logs / nginx- access.log The log format uses the default combined.
access_log /var/logs/nginx-access.log buffer=32k gzip flush=1m
This example specifies that the log write path is / var / logs / nginx- access.log The log format uses the default combined. The cache size of the specified log is 32K. Before the log is written, gzip is enabled for compression. The compression ratio is 1 and the cache data is valid for 1 minute.
Using log_ Custom log format
Nginx defines the log format named combined. If the log format is not specified explicitly, it is used by default
log_format combined '$remote_addr - $remote_user [$time_local] ' '"$request" $status $body_bytes_sent ' '"$http_referer" "$http_user_agent"';
If you don’t want to use the nginx predefined format, you can use the log_ The format directive comes from the definition.
log_format name [escape=default|json] string ...;
- Name format name. In access_ Log instruction.
- Escape sets whether the character encoding method in the variable is JSON or default. The default is default.
- String the log format content to be defined. There can be more than one parameter. The nginx variable can be used in the parameter.
Here’s the log_ Some variables commonly used in the format instruction are:
$bytes_sent The total number of bytes sent to the client $body_bytes_sent The number of bytes sent to the client, excluding the size of the response header $connection Connection serial number $connection_requests The current number of requests made over the connection $msec Log write time, unit: second, precision: Ms $pipe If the request is sent through the HTTP pipeline, the value is "P", otherwise it is "“ $request_length Find the length (including request line, request header and request body) $request_time Request processing time, unit: seconds, precision: ms, starting from the first byte read into the client until the last character is sent to the client for log writing $status Response status code $time_iso8601 Local time in standard format, such as "2017-05-24 t18:31:27 + 08:00" $time_local Local time in general log format, such as "24 / may / 2017:18:31:27 + 0800" $http_referer The referer address of the request. $http_user_agent Client browser information. $remote_addr Client IP $http_x_forwarded_for When there is a proxy server on the current side, set the configuration of the web node to record the client address. The precondition for this parameter to take effect is that the proxy server should also perform the relevant X_ forwarded_ For settings. $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 Complete request address, such as“ https://daojia.com/ "
Here’s how to use the custom log format:
access_log /var/logs/nginx-access.log mainlog_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"';
We use log_ The format instruction defines the format of a main, and the_ It is referenced in the log instruction. If the client has initiated a request: https://suyunfe.com/ , let’s take a look at the log record of a request I intercepted:
22.214.171.124 - - [20/Feb/2018:12:12:14 +0800] "GET / HTTP/1.1" 200 190 "-" "Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Mobile Safari/537.36" "-"
We see in the final log
$http_x_forwarded_forThey all correspond to one
-Because these variables are empty.
Set error_ Log
The error log in nginx is through error_ Log instruction. This command records the error information in the server and request processing.
Configure the path and log level of the error log file.
error_log file [level]; Default: error_log logs/error.log error;
The first parameter specifies where the log is written.
The second parameter specifies the level of logging. Level can be any value in debug, info, notice, warn, error, crit, alert, emerg. It can be seen that the value range is arranged from low to high according to the emergency degree. Only the error level of the log is equal to or higher than the value specified by level. The default value is error.
It can be configured in: main, HTTP, mail, stream, server, location.
The path of the error log specified in the example is:
/var/logs/nginx/nginx-error.logThe log level uses the default error.
For each log record, the file is opened first, then the record is written, and then the log file is closed. If you use variables in your log file path, such as
access_log /var/logs/$host/nginx-access.logTo improve performance, open can be used_ log_ file_ The cache instruction sets the cache of the log file descriptor.
open_log_file_cache max=N [inactive=time] [min_uses=N] [valid=time];
- Max sets the maximum number of file descriptors in the cache. If it is full, the LRU algorithm is used to close the descriptors.
- Inactive sets the cache lifetime, which is 10s by default.
- min_ During the inactive time period, the log file is used at least several times. The log file descriptor is recorded in the cache, and the default is 1 time.
- Valid: set how long to check the log file name to see if it changes. The default value is 60s.
- Off: do not use cache. The default is off.
open_log_file_cache max=1000 inactive=20s valid=1m min_uses=2;
It can be configured in the scope of HTTP, server and location.
In the example, set the cache to cache up to 1000 log file descriptors. If the log file descriptors in the cache are accessed at least twice in 20s, they will not be closed by the cache. Check that the file name of the file descriptor in the cache still exists every 1 minute.
Access in nginx_ Log and error_ The log instruction configures the access log and error log through log_ We can customize the log format. If variables are used in the log file path, we can open the_ log_ file_ Cache instruction to set the cache to improve performance.
In addition, in access_ Log and log_ Many variables are used in the format, which are not listed one by one. For detailed information about variables, please refer to the official document of nginx: http://nginx.org/en/docs/vari…