Automatic logs for nginx containing variables:
Note the following when using automatic logs:
1. nginx log directory permission. Take my installation path as an example:/usr/local/nginx. Then, my log directory is in/usr/local/nginx/logs; the inclusion variable is used.
For automatic logs, nginx users in this directory must have the write permission. Otherwise, logs cannot be generated. By default, the nginx running user is nobady. My
The habit is to install a program and assign it a dedicated account.
2. the cache will not be used, because the variable is used (which is not fully understood yet)
3. For each log, open the file, write it, and close the file. In this case, you need to enable the open_log_file_cache function of nginx.
Used log file descriptor cache. Open_log_file_cache is off by default.
Open _log_file_cache max = 1000 inactive = 20 s min_uses = 2 valid = 1 m;
Max maximum number of file descriptors and maximum number of caches
Inactive will be deleted if it is not active for a period of time.
If min_uses is used more than the number defined by this parameter within the inactive time, it will be cached.
How long is valid checked.
Http {
............
Log_format main '$ remote_addr-$ remote_user [$ time_local] "$ request "'
'$ Status $ body_bytes_sent "$ http_referer "'
'"$ Http_user_agent" "$ http_x_forwarded_for "';
Access_log logs/$ server_name.access.log main;
Open _log_file_cache max = 1000 inactive = 20 s min_uses = 2 valid = 1 m;
............ Saves other rows
}
Finally, the log file name starts with the name defined in server_name. Example: svr3.labexam.com. access. log
4. About log cutting:
After completing the above three items, it is not the end of the work, but another start. Logs increase rapidly as the traffic increases. Therefore, log cutting is required!
The following is a script:
#! /Bin/bash
# Made by cheng
# Blog: myhat.blog.51cto.com
#! /Bin/bash
# Made by cheng
# Blog: myhat.blog.51cto.com
# Last_time = 2011/08/16
# Your logs store
Log_path =/data
Nginx_pid =/usr/local/nginx/logs/nginx. pid
Nginx_log =/usr/local/nginx/logs
Yesterday = 'date-d "yesterday" + % Y % m % d'
Host = 'LS-l $ nginx_log /*. access. log | awk-F "" '{print $9}' | awk-F ". access "'{print $1}' | awk-F" $ nginx_log/"'{print $2 }''
For I in $ host
Do
If [! -D $ log_path/$ I/$ yesterday]; then
Mkdir-p $ log_path/$ I/$ yesterday
Else
If [! -F $ log_path/$ I/$ yesterday. access. log]; then
Mv $ nginx_log/$ I. access. log \
$ Log_path/$ I/$ yesterday. access. log
Kill-USR1 'cat $ nginx_pid'
Else
Mv-bf $ nginx_log/$ I. access. log \
$ Log_path/$ I/$ yesterday. access. log_1
Kill-USR1 'cat $ nginx_pid'
Fi
Fi
Done
The last thing to do is to put the script in crontab and run it!
After the script is run, the home directory is automatically created based on the VM host name, and the date directory of the previous day is created under the directory, and the logs of the previous day are put in this directory. Log splitting is performed once a day.
* ****** The prerequisite for running this script is that the automatic log function is used.
This article is from the "diving into the ocean of technology" blog