Nginx Log Management and cutting

Source: Internet
Author: User

Nginx Log management and cutting

I. Classification of logs

Nginx logs are divided into two main types, access logs and error logs. Both logs can be configured in HTTP and server modules, andNginx has a very flexible logging mode. Each level of configuration can have its own independent access logs. The log format is defined by the Log_format command.

1. Access Log

Access log primary record client Access Nginx for every request

The Log_format is used to set the log format and can only be set under the HTTP module

Log_format name Name ( format name ) type ( format style )

The following is the default nginx 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";

Field Meaning:

$remote _addr The IP address of the remote client .

$remote _user Remote Client user name, if the site is set up user authentication, there will be, otherwise blank

[$time _local] the time and timezone of the visit, such as the Last "+0800" of the 18/jul/2012:17:00:01+0800 time information, indicates that the server's time zone is 8 hours after UTC .

$request record the URL and http protocol for the request

$status record the HTTP status code returned by the request .

$body _bytes_sent Record the size of the file body content sent to the client

$http _referer Records from which page links are accessed.

$http _user_agent Log client browser information

$http The real IP of the _x_forwarded_for client . When There is a proxy server in frontof Nginx, $remote _addr gets only the first level of IP, and the reverse proxy server forwards the requested HTTP Header information can be added x_forwarded_for information to record the original client's IP address and the original client's request server address,$http _x_forwarded_for Parameters are the client IP parameters that are passed at the previous level . The real IP of the client is obtained .

The access_log instruction is used to specify the storage path of the log file, which can be set in HTTP,server,location

examples are as follows

Access_log Logs/access.log Main;

If you want to close the log can be as follows

Access_log off;

2. Error log

The error log primarily records client Access the log format for Nginx error does not support customization.

The specific format specified by the directive Error_log is as follows

Error_log path ( storage path ) level ( log rank )"Debug | info | Notice | Warn | Error |crit"

If you do not specify a path, the default is under logs.

3. Common log format in production environment:

Log_format Main ' $http _host-$http _x_forwarded_for ${request_time}s-[$time _local] "$request" '

' $status $body _bytes_sent "$http _referer" $http _user_agent "$remote _addr ';

Second, log management

1.nginx Log Cutting

implementation of the idea: every day to move logs to the backup directory, and then re -reload or restart. This will generate a new log file under the original logs. ( Note: When the log file is moved to the backup directory, Nginx will still record the access request to the original logfile before restart,only wait restart After the new file is generated, it is re-recorded in the new log file )

Implementation script:

#!/bin/bash#created by fuzj  #Log  dirdir_log= "/usr/local/nginx/logs" weblog= ( www.beyond.combbs.beyond.com) date= ' date -d "Yesterday"  + "%y%m%d" '  if [ ! -d "${dir _log}/cut_log/${date} " ];then   mkdir -p ${dir_log}/cut_log/${date}fidir=" ${DIR_ Log}/cut_log/${date} "                                                                    nginx_log= "${DIR_LOG }/current " for  log in ${weblog[@]}; domv ${nginx_log}/$log   $DIRdone  kill -USR1  ' Cat /usr/local/nginx-1.2.1/logs/nginx.pid ' sleep 130find ${dir_log}/ Cut_log/* -typed -mtime +7 -exec rm -rf {} \;sleep 130 

2.nginx does not log access logs for certain files or directories

method: First Use location to define the file or directory without logging, and then use access_log off under it; you can close the log

For example:

Location ~*.\checkstatus.html {

Access_logoff;

}


This article is from the "Study-everyday" blog, make sure to keep this source http://studys.blog.51cto.com/9736817/1665922

Nginx Log Management and cutting

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.