Nginx Log error log and access log and log segmentation

Source: Internet
Author: User
Tags time zones

One. Nginx error log


Nginx software will log the fault information of its own operation and the logged information of user access to the specified log file.


Typically in the second line of the nginx.conf configuration file, add:
Error_log Logs/error.log error;

↓↓↓

Keyword log file log level

In fact, if you do not add this line, the default is this


About log levels:

Common levels of error logging are:
Debug | Info|notice | Warn|error|crit | Alert | Emerg

By experience, the higher the level, the less information is recorded, and the production environment is generally warn, error, crit three levels

Note: Do not configure lower levels such as info, which can result in huge disk I/O consumption


Tip: Empty the log file

[Email protected] logs]# > Error.log

The configuration of Nginx error log is so simple


Two. Nginx Access log

Nginx software will log information of each user's visit to the specified log file, for the website provider to analyze the user's browsing behavior, etc., this function by the Ngx_http_log_module module is responsible for



Let's take a look at the default installation done after Nginx has not modified nginx.conf, general access logs are placed in the httpd block that

Modify nginx.conf Add yellow section

worker_processes  1;
error_log       logs/error.log  error;
Events {
    worker_connections ;
}
http {
    include       mime.types;
    default_type  Application/octet-stream;

Log_format main ' $remote _addr-$remote _user [$time _local] "$request" '
' $status $body _bytes_sent ' $http _referer '
' "$http _user_agent" "$http _x_forwarded_for";

Sendfile on;
Keepalive_timeout 65;
Include extra/*.conf;

}



The parameters of the access log are described below:

Log_format main→ This is the definition of the log format

$remote _addr-
record the client address of the website to access


$remote _user Remote Client User name



$time _local Log user access times and time zones



$request User's HTTP request for information about its implementation



$status HTTP status code that logs the status of the request returned, for example 200 403 301



$ Body_bytes_sent response body Bytes sent by the server to the client



/td>
< Span style= "Background-color:rgb (255,255,0);" > $http _referer


< Span style= "Background-color:rgb (255,255,0);" > $http _user_agent log client access information such as browser, mobile client, etc.



$http _x_forwarded_for Set Web node record client configuration when there is a proxy server on the current side





First the master profile is modified, and then configured in each virtual host to record the user access log using the above format, www.vipdailiang.com as an example


Modify


server {
Listen 80;
server_name www.vipdailiang.com;
Location/{
Root html/www;
Index www.html;
access_log Logs/www_access.log main;

}

}


Final check configuration and reload Nginx configuration:

/app/zpy/nginx/sbin/nginx-t
/app/zpy/nginx/sbin/nginx-s Reload



The browser accesses www.vipdailiang.com and then


[Email protected] logs]# tail-f Www_access.log
192.169.21.191--[15/jul/2016:15:59:58 +0800] "get/http/1.1"-"" mozilla/5.0 (Windows NT 6.1; WOW64; rv:47.0) gecko/20100101 firefox/47.0 ""-"


Compare this with:

Log_format main ' $remote _addr-$remote _user [$time _local] "$request" '
' $status $body _bytes_sent ' $http _referer '
' "$http _user_agent" "$http _x_forwarded_for";




$remote _addr-
record access to website client address     192.169.21.191
$remote _user Remote Client user name -
$time _local Logging user access times and time zones [15/jul/2016:15:59:58 +0800]
$request The user's HTTP request has its implementation information "get/http/1.1"
$status HTTP status code to record the status of the request return, e.g. 200 403 301
$ Body_bytes_sent response body Bytes sent by the server to the client
< Span style= "Background-color:rgb (255,255,0);" > $http _referer   -
< Span style= "Background-color:rgb (255,255,0);" > $http _user_agent log client access information such as browser, mobile client, etc. "mozilla/5.0 (Windows NT 6.1; WOW64; rv:47.0) gecko/20100101 firefox/47.0 "
$http _x_forwarded_for When there is a proxy server on the current side, set the Web node record client configuration -

At this point nginx Access log collection completed ...

But we have a new problem, a long time the access log has been written to the inside will cause the log is too large, not conducive to the segmentation and processing of the log, it is necessary to partition Nginx log, I still prefer to use the day division



Three. Nginx Log Segmentation

Implemented by script:

1) Rename the Nginx log (Www_access.log) that is being written to a date-formatted file (20160715_www_access.log)

Then reload Nginx to generate a new Nginx log (Www_access.log)

2) Add this script to Crontab


#!/bin/bash
date01= ' Date +%y%m%d '
Basedir= "/app/zpy/nginx"
Logdir= "$BASEDIR/logs"
[-D $LOGDIR] && CD $LOGDIR | | Exit 1
[-F Www_access.log] | | Exit 2
/BIN/MV Www_access.log Www_access_$date01.log
$BASEDIR/sbin/nginx-s Reload

Comments:

[Email protected] scripts]# date +%y%m%d
20160715
[Email protected] scripts]# date +%y/%m/%d
2016/07/15
-D to determine if the directory exists

-F to determine if a file exists

[] There are spaces inside.


Add crontab (to be executed daily)

Vim/etc/crontab add

XX * * * root /bin/sh/software/tools/scripts/cut_nginx_log.sh\


Comments:

Pay attention to the user, CRONTAB-E will not have to write the user

Cron service every minute not only to read all the files within/var/spool/cron, but also to read a/etc/crontab, so we configure this file can also use the Cron service to do something. The CRONTAB-E configuration is for a user, while the edit/etc/crontab is a task for the system


Finally, let's look at the effect:

-rw-r--r--. 1 root root 489 July 15:59 Www_access_20160715.log
-rw-r--r--. 1 root root 0 July 16:39 www_access.log


This article is from the "Hidden Fishing" blog, please be sure to keep this source http://dailiang.blog.51cto.com/9484865/1826778

Nginx Log error log and access log and log segmentation

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.