Nginx Log Configuration, Log Cutting

Nginx Log Configuration

Previous articles: Nginx's configuration file nginx.conf

Nginx logs are useful for statistics and system service troubleshooting.
There are two main types of Nginx logs: access_log and error_log.
By accessing the log, we can get the user's IP address, browser information, request processing time, and so on.
The error log records information about access errors and helps us locate the cause of the error.
So if you make good use of your blog, you can get a lot of valuable information.

Set access.log

grammar

Configure in the nginx.conf file:

access_log path [format [buffer=size] [gzip[=level]] [flush=time] [if=condition]]; 
#Set Access Log
access_log off;
#Close Access Log
  • path specifies where to store the log.
  • Format specifies the format of the log.Predefined combined is used by default.
  • The buffer is used to specify the size of the cache at which the log is written.The default is 64k.
  • The gzip log is compressed before it is written.The compression ratio can be specified. The larger the number 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 zero or an empty string, the request will not be written to the log.
  • There is also a special value off.If this value is specified, all request logs in the current scope are closed.

Example:

http {
   ...
	       ##The log format uses the default combined; the specified log cache size is 32k; gzip is enabled for compression prior to log writing, with a compression ratio of 1; and the cache data is valid for 1 minute.
	    access_log /var/logs/nginx-access.log buffer=32k gzip flush=1m;  
   ...
}

The access_log directive has http, server, location scopes.
The settings inside will override those outside.

log_format custom format

Default log format

log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

Detailed table of each parameter:

Show the latest appended logs:
tail -f /usr/local/nginx/logs/access.log

Set error_log

The error log is implemented in Nginx through the error_log directive.This directive records error information during server and request processing.
Customization is not supported for error logs.

grammar

error_log path [level];
  • The path parameter specifies where the log will be written.
  • The level parameter specifies the level of the log.Level can be any value in debug, info, notice, warn, error, crit, alert,emerg (ranked from low to high).
    Only if the error level of the log is equal to or higher than the value specified by level will it be written to the error log.The default value is error.

Example:

error_log  logs/error.log; 
error_log  logs/error_notice.log  notice;
error_log  logs/error_info.log  info;			
##Different error types can be stored separately

Log Cutting

Test, cut in minutes:
You can vim logcut.sh in the conf directory:

#!/bin/bash
#Set log file storage directory
LOG_HOME=/usr/local/nginx/logs/
#Standby file name
LOG_PATH_BAK="$(date -d "last_minute" +%Y%m%d%H%M)"
#Renaming the log file will delete the original file and cause the log to be lost because Nginx will not create another log file on its own
mv ${LOG_HOME}/host.access.log ${LOG_HOME}/access.${LOG_PATH_BAK}.log
mv ${LOG_HOME}/error.log ${LOG_HOME}/error.${LOG_PATH_BAK}.log
#Send a USR1 signal to the Nginx main process.The USR1 signal in Nginx is to reopen the log file
kill -USR1 `cat ${LOG_HOME}/nginx.pid`

chmod u+x logcut.sh

Configure cron:
crontab -e

*/1 * * * * /usr/local/nginx/conf/logcut.sh
#Minutes/Hours, Days, Months and Years

Enter the logs folder:

Cut by day:

In the conf directory vim logcut.sh:

#!/bin/bash
#Set log file storage directory
LOG_HOME=/usr/local/nginx/logs/
#Standby file name
LOG_PATH_BAK="$(date -d "yesterday" +%Y%m%d)"
#Renaming the log file will delete the original file and cause the log to be lost because Nginx will not create another log file on its own
mv ${LOG_HOME}/host.access.log ${LOG_HOME}/access.${LOG_PATH_BAK}.log
mv ${LOG_HOME}/error.log ${LOG_HOME}/error.${LOG_PATH_BAK}.log
#Send a USR1 signal to the Nginx main process.The USR1 signal in Nginx is to reopen the log file
kill -USR1 `cat ${LOG_HOME}/nginx.pid`

chmod u+x logcut.sh

Configure cron:
crontab -e

* * */1 * * /usr/local/nginx/conf/logcut.sh
#Time Sharing Day/January Year

Reference: Peter - Nginx Advanced - Second Edition Notes

Keywords: Nginx vim crontab

Added by elklabone on Thu, 26 Sep 2019 06:04:10 +0300