1. Access Log
1.1. http_log_module
1.1.1. log_format
Directive
Syntax: log_format name [escape=default|json|none] string ... ;
Default: log_format combined "..." ;
Context: http
Instroduction
Specify log_format. Reference variables:Nginx Alphabetical index of variables .
1.1.2. access_log
Directive
Syntax: access_log path [format [buffer=size] [gzip[=level]] [flush=time] [if=condition]];
access_log off;
Default: access_log logs/access.log combined ;
Context: http,server,location,if in location;limit_except
Instroduction
Sets the path, format, and configuration for a buffered log write. Several logs can be specified on the same level. Logging to syslog can be configured by specifying the “syslog:” prefix in the first parameter. The special value off cancels all access_log directives on the current level.
Paremeters:
- path: specify access.log path.
- format: reference name from log_format directive
- buffer: sets buffer size befor write to disk.
- gzip: compress log content in buffer,then write to disk.Default level is 1.
- flush: sets time flush buffer to disk
- if: if value is empty or equal to 0, nginx will ignore to record log.
1.2. Access Log Example
1.2.1. Nginx Configuration
log_format access '$time_local|$http_x_real_ip|$remote_addr|$http_x_forwarded_for|$upstream_addr|'
'$request_method|$server_protocol|$host|$request_uri|$http_referer|$http_user_agent|'
'$proxy_host|$status' ;
access_log /opt/logs/nginx/access.log access ;
1.2.2. Test
[root@centos-81 ~]# tail -f /opt/logs/nginx/access.log16/Feb/2019:22:26:16 +0800|-|192.168.1.81|-|192.168.1.50:8081|GET|HTTP/1.1|www.heyang.info|/|-|curl/7.29.0|test_proxy|200
16/Feb/2019:22:26:16 +0800|-|192.168.1.81|-|192.168.1.50:8081|GET|HTTP/1.1|www.heyang.info|/|-|curl/7.29.0|test_proxy|200
16/Feb/2019:22:26:16 +0800|-|192.168.1.81|-|192.168.1.50:8080|GET|HTTP/1.1|www.heyang.info|/|-|curl/7.29.0|test_proxy|200
1.3. Change Time Format
Sometime we need change time format “16/Feb/2019:22:26:16 +0800” to “yyyy-mm-dd HH:MM:SS”.The second time format is better for log statistics and analysis.1.3.1. time_iso8601
The variable $time_iso8601 is built-in variable.But the format is not well.
Nginx configuraion
log_format access '$time_iso8601|$http_x_real_ip|$remote_addr|$http_x_forwarded_for|$upstream_addr|'
'$request_method|$server_protocol|$host|$request_uri|$http_referer|$http_user_agent|'
'$proxy_host|$status' ;
access_log /opt/logs/nginx/access.log access ;
[root@centos-81 ~]# tail -1 /opt/logs/nginx/access.log
Test
2019-07-07T11:44:38+08:00|-|192.168.1.1|-|ngx_backend|GET|HTTP/1.1|www.heyingsheng.com|/|-|Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36|-|404
1.3.2. Modify source code
Change ngx_http_log_module.c
[root@centos-81 nginx-1.14.2]# vim src/http/modules/ngx_http_log_module.c
// old
{ ngx_string("time_local"), sizeof("28/Sep/1970:12:00:00 +0600") - 1,
ngx_http_log_time },
// new
{ ngx_string("time_local"), sizeof("1970-09-28 12:00:00 +0600") - 1,
ngx_http_log_time },
//old
{
return ngx_cpymem(buf, ngx_cached_http_log_time.data,
ngx_cached_http_log_time.len);
}
//new
{
return ngx_cpymem(buf, ngx_cached_err_log_time.data,
ngx_cached_err_log_time.len);
}
- Change ngx_times.c
[root@centos-81 nginx-1.14.2]# vim src/core/ngx_times.c
//old
(void) ngx_sprintf(p1, "%4d/%02d/%02d %02d:%02d:%02d",
tm.ngx_tm_year, tm.ngx_tm_mon,
tm.ngx_tm_mday, tm.ngx_tm_hour,
tm.ngx_tm_min, tm.ngx_tm_sec);
//new
(void) ngx_sprintf(p1, "%4d-%02d-%02d %02d:%02d:%02d",
tm.ngx_tm_year, tm.ngx_tm_mon,
tm.ngx_tm_mday, tm.ngx_tm_hour,
tm.ngx_tm_min, tm.ngx_tm_sec);
Nginx configuraion
log_format access '$time_local|$http_x_real_ip|$remote_addr|$http_x_forwarded_for|$upstream_addr|'
'$request_method|$server_protocol|$host|$request_uri|$http_referer|$http_user_agent|'
'$proxy_host|$status' ;
access_log /opt/logs/nginx/access.log access ;
Test
[root@centos-81 ~]# tail -3 /opt/logs/nginx/access.log
2019-07-07 12:23:33|-|192.168.1.1|-|58.213.154.120:80|GET|HTTP/1.1|www.heyang.com|/gtzz/images/flexslider.js|https://www.heyang.com/gtzz/gy/tyljls/|Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36|jsmlr|200
2019-07-07 12:23:33|-|192.168.1.1|-|58.213.154.120:80|GET|HTTP/1.1|www.heyang.com|/gtzz/images/scroll.js|https://www.heyang.com/gtzz/gy/tyljls/|Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36|jsmlr|200
2019-07-07 12:23:33|-|192.168.1.1|-|-|GET|HTTP/1.1|www.heyang.com|/favicon.ico|https://www.heyang.com/|Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36|-|404
1.3.3. Define format by lua
Reference: https://blog.csdn.net/ystyaoshengting/article/details/84951087
2. Error Log
2.1. error_log
Directive
Syntax: error_log file [level] ;
Defautl: error_log logs/error.log error ;
Context: main,http,mail,stream,server,location
Instroduction
Configures logging. Several logs can be specified on the same level (1.5.2). If on the main configuration level writing a log to a file is not explicitly defined, the default file will be used.
The first parameter defines a file that will store the log. The special value stderr selects the standard error file. Logging to syslog can be configured by specifying the “syslog:” refix. Logging to a cyclic memory buffer can be configured by specifying the “memory:” prefix and buffer size, and is generally used for debugging (1.7.11).
The second parameter determines the level of logging, and can be one of the following:debug, info, notice, warn, error, crit, alert, or emerg.
Log levels above are listed in the order of increasing severity. Setting a certain log level will cause all messages of the specified and more severe log levels to be logged. For example, the default level error will cause error, crit, alert, and emerg messages to be logged. If this parameter is omitted then error is used.
3. Log Rotate
- Shell Script:
```bash
!/usr/bin/env bash
Author: Heyingsheng
Date: 2019-01-04
export PATH=”/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin” LOG_DIR=”/opt/logs/nginx/“ PID=”/opt/logs/nginx/nginx.pid”
cd $LOG_DIR [ -e $(date +%Y) ] || mkdir $(date +%Y) mv access.log $(date +%Y)/access-$(date +%F).log mv error.log $(date +%Y)/error-$(date +%F).log
kill -s USR1 $(cat $PID)
find ./ -maxdepth 2 -type f -name “error-.log” -mtime +30 -exec rm -f {} \; find ./ -maxdepth 2 -type f -name “access-.log” -mtime +60 -exec rm -f {} \;
[ -e $(date -d “-1 years” +%Y) ] && FILE_COUNT=$(find ./$(date -d “-1 years” +%Y) -type f | wc -l) && [ $FILE_COUNT -eq 0 ] && rm -fr ./$(date -d “-1 years” +%Y) ```