Linux timed cut Nginx access logs and delete log records before a specified number of days

Source: Internet
Author: User
Tags time zones mkdir

Description

Operating system: CentOS

Site 1:bbs. 111cn.net

Site 2:sns. 111cn.net

Nginx Installation path:/usr/local/nginx

Nginx configuration file path:/usr/local/nginx/conf/nginx.conf

Site 1 configuration file path:/usr/local/nginx/conf/vhost/bbs.111cn.net.conf

Site 2 configuration file path:/usr/local/nginx/conf/vhost/sns.111cn.net.conf

Objective:

1, the site 1 and site 2 of the Nginx access log saved by day, log path is:

Site 1:/usr/local/nginx/logs/nginx_logs/bbs_logs

Site 2:/usr/local/nginx/logs/nginx_logs/sns_logs

2, only 30 days to keep the log records

Specific actions:

1. Create Log Store path

Mkdir-p/usr/local/nginx/logs/nginx_logs/bbs_logs

Mkdir-p/usr/local/nginx/logs/nginx_logs/sns_logs

2, set nginx logging format

vi/usr/local/nginx/conf/nginx.conf #编辑

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

' $status $body _bytes_sent ' $http _referer '

' $http _user_agent ', ' $http _x_forwarded_for ';

Cancel the comment in front of the logging format and add a row at the end, as follows:

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

' $status $body _bytes_sent ' $http _referer '

"$http _user_agent" "$http _x_forwarded_for"

' $http _host $upstream _status $upstream _addr $request _time $upstream _response_time '; #新添加的一行

: wq! #保存退出

3, set up the virtual host configuration file

Vi/usr/local/nginx/conf/vhost/bbs.111cn.net.conf #编辑, in bbs.111cn.net; Add below this line

Access_log Logs/bbs_access.log Main; #注意这里的main要和上一步中的main一致

: wq! #保存退出

Vi/usr/local/nginx/conf/vhost/sns.111cn.net.conf #编辑, in sns.111cn.net; Add below this line

Access_log Logs/sns_access.log Main; #注意这里的main is consistent with main in the previous step

: wq! #保存退出

System Yun-wei www.111cn.net Warm reminder: qihang01 original content copyright, reproduced please indicate the source and the original link

Service Nginx Reload #重新加载nginx配置文件

4. Create Nginx Log Cutting script

vi/home/crontab/cut_nginx_log.sh #编辑

#!/bin/sh

Logs_path= "/usr/local/nginx/logs/"

Days=30

MV ${logs_path}bbs_access.log ${logs_path}nginx_logs/bbs_logs/bbs_access_$ (date-d "Yesterday" + "%Y%m%d"). Log

MV ${logs_path}sns_access.log ${logs_path}nginx_logs/sns_logs/sns_access_$ (date-d "Yesterday" + "%Y%m%d"). Log

KILL-USR1 ' Cat/usr/local/nginx/logs/nginx.pid '

Find ${logs_path}nginx_logs/bbs_logs/-name "bbs_access_*"-type f-mtime + $DAYS-exec rm {};

Find ${logs_path}nginx_logs/sns_logs/-name "sns_access_*"-type f-mtime + $DAYS-exec rm {};

: wq! #保存退出

chmod +x/home/crontab/cut_nginx_log.sh #添加脚本执行权限

5, add Task plan, modify/etc/crontab

Vi/etc/crontab #在最后一行添加

0 0 * * * root/home/crontab/cut_nginx_log.sh #表示每天凌晨执行

: wq! #保存退出

6, restart Crond to make the settings effective

/etc/rc.d/init.d/crond Restart #yum install-y vixie-cron installation Scheduled Tasks, some systems may not have pre-installed

Chkconfig Crond on #设为开机启动

Service Crond start #启动

every day will be in /usr/local/nginx/logs/nginx_logs/bbs_logs and /usr/local/nginx/logs/nginx_logs/sns_logs

The directory generates a similar Bbs_access_20140126.log and Bbs_access_20140126.log the log file

And only keep the log records for the last 30 days

At this point, Linux under the scheduled cutting Nginx access log and delete the specified number of days before the completion of the log record.

Extended reading:

========================================================

Nginx Access Log parameter description

192.168.21.1--[27/jan/2014:11:28:53 +0800] "get/2.php http/1.1"-"" mozilla/5.0 (Windows NT 6.1; WOW64) applewebkit/537.36 (khtml, like Gecko) chrome/33.0.1707.0 safari/537.36 ""-"192.168.21.128 200 127.0.0.1:9000 0.119 0.119

$remote _ADDR: client address 192.168.21.1

$remote _user: Client User -

$time _local: times and time zones 27/jan/2014:11:28:53 +0800

$request: requested URL path and HTTP protocol get/2.php http/1.1

$status: http status

$body _bytes_sent: Send to client page size

$http _referer: page Jump source -

$http _user_agent: user access to Terminal mozilla/5.0 (Windows NT 6.1; WOW64) applewebkit/537.36 (khtml, like Gecko) chrome/33.0.1707.0 safari/537.36

$http _x_forwarded_for:http proxy, request-side real IP -

$http _host: user-entered URL (IP or domain name) address in the browser 192.168.21.128

$upstream _status: upstream State

$upstream _addr: back-end upstream address and Port 127.0.0.1:9000

$request _time: Total page access time 0.119

$upstream _response_time: Upstream response time in page access 0.119

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.