(4) Nginx: Log and timing cut Log small example

Source: Internet
Author: User

A about Nginx log

We observe the nginx.conf in the Nginx installation directory




You can see similar information like this

#access_log Logs/host.access.log Main;

This indicates that the server, its access log file is Logs/host.access.log,

Format used for "main" format.

In addition to the main format, you can customize other formats.

What is the main format?

Log_format Main ' $remote _addr-$remote _user [$time _local] "$request" '

# ' $status $body _bytes_sent ' $http _referer '

# ' "$http _user_agent" "$http _x_forwarded_for" ';

The main format is that we define a log format and name it for easy reference.

In the above example, the main type of log, record the remote_addr .... http_x_forwarded_for options.

1: Log format refers to which options are recorded

Default log format: Main

Log_format Main ' $remote _addr-$remote _user [$time _local] "$request" '

' $status $body _bytes_sent ' $http _referer '

' "$http _user_agent" "$http _x_forwarded_for";

As the default main log format, record so many items

Remote ip-remote user/User Time request method (such as get/post) request Body Body length Referer source information

Http-user-agent user agent/Spider, the original IP of the forwarded request

Http_x_forwarded_for: In the agent, the agent to add your original IP in this header information, transfer your raw IP


2 Configuration Log Format Small example

Configured as follows


Post-Visit results



2nd Log Cutting

Cutting reason: If a website visit is particularly large, the daily Access_log file has 2 G, if you want to find information from the file, the light is very slow to open,

Do log cutting, easy to maintain.

Requirement Description: Create a New Day folder for each day's log, and place the same day file in the appropriate folder by one file per minute.


The shell script (splitlog.sh) is written as follows:

#这里是每分钟切割一次日志的shell脚本
#!/bin/bash
base_path= '/usr/local/software/ngnix/install/logs ' #定义log基本路径变量
time=$ (date-d yesterday + "%y-%m-%d_%h-%m-%s")   #时间定义 (format: 2016-10-09_23-46-25)
modify_file_name= "$ Time.access.log "#修改的log文件名
today=$ (date-d today +%y%m%d"), #今天时间 20161010 
today_folder= "$base _path/$today "; #今天的文件夹
#如果今天文件夹不存在, recreate an
if [!-d "$today _folder"];
Then
  mkdir $today _folder
fi
mv $base _path/access.log $today _folder/$modify _file_name #修改名字
KILL-USR1 ' Cat/usr/local/software/ngnix/install/logs/nginx.pid ' #平滑关闭nginx for easy log Cutting

Timed execution of the shell

CRONTAB-E Edit Contab Command

*/1 * * * */usr/local/software/ngnix/install/script/splitlog.sh Specify execution time

Result diagram







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.